[thelist] Proper commenting and code maintainance

Matt Warden mwarden at gmail.com
Fri Dec 15 09:09:38 CST 2006


On 12/15/06, Brooking, John <John.Brooking at sappi.com> wrote:
> 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.

Agreed. Perhaps an exception (no pun intended): In JavaScript it's
common to see this:


foo: function(arg1, arg2)
{
   if (!document.getElementById) return false;
   if (!arg1) return false;
   if (!arg2) return false;

  // ... real code here ...

}

which is an almost self-documenting way of coding preconditions for
the method. (Not to say documentation isn't necessary, just that the
code is highly readable.)

-- 
Matt Warden
Cleveland, OH, USA
http://mattwarden.com


This email proudly and graciously contributes to entropy.



More information about the thelist mailing list