[thelist] javascript debug (easy)

Jeff Howden jeff at jeffhowden.com
Mon Jun 6 10:39:16 CDT 2005


Matt,

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> From: Matt Warden [mailto:mwarden at gmail.com] 
> 
> > On 6/5/05, Jeff Howden <jeff at jeffhowden.com> wrote:
> > 
> > Curly braces are only required when there is more than
> > one statement within an if/else block.  They are
> > therefore needed for the if() block above, but not the
> > else block.
> 
> True. Just a note, though, that almost all published
> coding standards frown upon conditionals that omit
> braces when they are not required. [...]
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

In my opinion, this is one of those coding standards that's strictly for
newbies and those uncomfortable with script syntax.  Once you become
comfortable with scripting, there's no reason to include curly braces in
situations where they are optional.

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> [...] The main reason for this is that omitting braces
> increases the likelihood of bugs cropping up if someone
> were to add another line to the condition's execution
> block. [...]
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

Sure, but, in my opinion, that is a newbie mistake.

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> [...] e.g.:
> 
> if (foo)
>     bar();
> else
>     foobar();
> 
> edited to:
> 
> if (foo)
>     bar();
> else
>     foobar();
>     barfoo();
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

Of course the indentation indicates that barfoo() is supposed to be part of
the else conditional.  Therefore curly braces must be added.  If they are
not, then the indentation should be modified to more accurately indicate
program flow:

if (foo)
    bar();
else
    foobar();
    barfoo();

I know you already know all this, but felt it necessary to expand on it for
those not so knowledgable on the matter.

 [>] Jeff Howden
     jeff at jeffhowden.com
     http://jeffhowden.com/



More information about the thelist mailing list