JavaScript Concern from a recent review
David Miller
justdave at bugzilla.org
Sun Apr 8 20:16:47 UTC 2007
Colin Ogilvie wrote on 4/8/07 1:46 PM:
> bugzilla-daemon at mozilla.org wrote:
>> Nit: omit braces for one-line conditional and loop blocks here and
>> elsewhere,
>> i.e.:
>>
>> for (var i = 0; i < document.forms.length; i++)
>> for (var j = 0; j < document.forms[i].elements.length; j++)
>> /* MS decided to add fieldsets to the elements array; and
>> * Mozilla decided to copy this brokenness. Grr.
>> */
>> if (document.forms[i].elements[j].tagName != 'FIELDSET')
>> document.forms[i].elements[j].onmouseover = showHelp;
>
> I'm concerned by a recent review Myk did on a JavaScript file (quoted
> above) where he says that for one-line conditionals and loop blocks you
> should omit braces (as a Nit).
>
> I'd like to suggest that this is a bad practice, and possibly going to
> cause confusion in the future, particularly in the example above. For
> clarity, it would be better written including braces.
>
> Does anyone else have thoughts on this, or is it just because I've been
> bitten by this in something I was doing at work...
Although javascript does make it optional, if it extends more than one
line to make it readable (especially like in the above example where
there's nested loops) I'd just as soon use the braces, because as you
said, it makes it much easier to follow what's looping.
--
Dave Miller http://www.justdave.net/
System Administrator, Mozilla Corporation http://www.mozilla.com/
Project Leader, Bugzilla Bug Tracking System http://www.bugzilla.org/
More information about the developers
mailing list