[thelist] Proper commenting and code maintainance

Brooking, John John.Brooking at sappi.com
Fri Dec 15 08:21:31 CST 2006


tim:
> 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
>     }
> }

That reminds me of another best practice I learned so long ago it's
almost subconscious, that a function should only have one return
statement. So for instance if your function could return an error number
at a variety of points, it's clearer to set a variable to the error
number, use conditionals if necessary to prevent the rest of the
function from executing if that variable is set, then return the
variable once at the end.

- John
-- 


This message may contain information which is private, privileged or confidential and is intended solely for the use of the individual or entity named in the message. If you are not the intended recipient of this message, please notify the sender thereof and destroy / delete the message. Neither the sender nor Sappi Limited (including its subsidiaries and associated companies) shall incur any liability resulting directly or indirectly from accessing any of the attached files which may contain a virus or the like.




More information about the thelist mailing list