[thelist] Proper commenting and code maintainance [was: Why does this JS code work the way it does???]
tim
tim at hyperlinkage.com
Wed Dec 13 16:48:20 CST 2006
VOLKAN ÖZÇELİK wrote:
> Then let us create a hybrid of both views:
>
> // if there keyCode is not one of the numbers in validkeys
> // do not allow the keystroke
> // otherwise allow it:
> return (strValidKeys.indexOf(String.fromCharCode(event.keyCode)) == -1);
Exactly. My point was not to suggest that comments should be removed,
but that there were several lines of code which weren't needed.
In my opinion it is much more readable to have the return values
described on the line just before the method, rather than in the method
body. Also, I would use an extra variable to store the pressed key,
rather than calling multiple methods on one line.
My version would be:
// Returns true if the key stroke is allowed
function CheckNumericKeyStroke()
{
var validKeys = "1234567890";
var pressedKey = String.fromCharCode(event.keyCode);
return (validKeys.indexOf(pressedKey) != -1);
}
Rather than:
function CheckNumericKeyStroke()
{
var strValidKeys = "1234567890";
// if there keyCode is not one of the numbers in validkeys...
if (strValidKeys.indexOf(String.fromCharCode(event.keyCode)) == -1) {
return false; // do not allow the keystroke
}
else {
return true; // allow the keystroke
}
}
Tim
--
http://www.hyperlinkage.com/
More information about the thelist
mailing list