[thelist] javascript debug (easy)

Matt Warden mwarden at gmail.com
Mon Jun 6 12:13:02 CDT 2005


Jeff,

On 6/6/05, Jeff Howden <jeff at jeffhowden.com> wrote:
> > 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.

Except if your employer adopts a coding standard, which most seem to
do. My point is just that one might decide to get into the habit of
coding in a style that seems to be pretty standard across style
requirements... even if you disagree with the reasoning behind it.

Which, btw, I don't.

For good measure, I guess I should mention that code like John
suggested is often advised against in coding standards as well:

> function openNewWindow( prompt, url )
>  return confirm(prompt) ? window.open(url) : false;
> }

It might take up less lines, but in the end you're saving a few
carriage returns and decreasing readability drastically over the
equivalent:

function openNewWindow( prompt, url )
  var agree = confirm(prompt);
  if (agree) 
  {
    window.open(url)
  } // end if
  
  return agree;
}

(Btw, thanks for catching my error with the returned value.)

The latter version does not require prior knowledge of what confirm()
returns and it is much more clear that the window is only opened if
the user agrees to the prompt. Further, it is clear that what is being
returned is the user response.

These aren't so much to protect 'newbies', but any knowledge that is
not used frequently requires longer time to retrieve. I am certainly
well aware of how confirm() works, but I rarely use it. If I had to
retrieve such information, it would slow down the amount of time it
takes to comprehend your code.

It's all just another cost-benefit analysis. It doesn't make sense to
drastically reduce readability to save a couple of keystrokes.

-- 
Matt Warden
Miami University
Oxford, OH, USA
http://mattwarden.com


This email proudly and graciously contributes to entropy.


More information about the thelist mailing list