|
Re: JavaScript formatter and comma separated vars [message #716317 is a reply to message #715805] |
Wed, 17 August 2011 03:09 |
|
On 8/15/2011 11:45 AM, Martin wrote:
> I was wondering whether there's a way (I can't find one) of getting the
> JavaScript formatter to line-break after commas in multiple-variable
> declarations?
>
> Currently:
>
> var myVar = 'test', hisVar = 'real', herVar = 'live';
>
> Ideally:
>
> var myVar = 'test',
> hisVar = 'real',
> herVar = 'live';
>
> This would then conform with the standard JSLint practice of declaring
> all at the front, and fit in nicely with the JavaScript parser's
> predilection for grabbing all the variables in the function and then
> simply declaring them first anyway!
>
> If there's no way of doing it, how would I go about putting it in as a
> feature request?
You can enter a feature request here:
https://bugs.eclipse.org/bugs/enter_bug.cgi?classification=WebTools&product=JSDT&component=General&bug_severity=enhancement
Although that particular combination, one var keyword with multiple
lines, doesn't seem like it would be that popular.
--
Nitin Dahyabhai
Eclipse WTP Source Editing and JSDT
IBM Rational
_
Nitin Dahyabhai
Eclipse Web Tools Platform
|
|
|
Re: JavaScript formatter and comma separated vars [message #716481 is a reply to message #716317] |
Wed, 17 August 2011 14:26 |
Martin Messages: 3 Registered: August 2011 |
Junior Member |
|
|
Thank you for that Nitin. The reason for it is to do with JavaScript's scoping. I'll just quote the infamous Douglas Crockford, instead of explaining myself:
Quote:In many languages, a block introduces a scope. Variables introduced in a block are not visible outside of the block.
In JavaScript, blocks do not introduce a scope. There is only function-scope. A variable introduced anywhere in a function is visible everywhere in the function. JavaScript's blocks confuse experienced programmers and lead to errors because the familiar syntax makes a false promise.
[...]
In languages with block scope, it is usually recommended that variables be declared at the site of first use. But because JavaScript does not have block scope, it is wiser to declare all of a function's variables at the top of the function. It is recommended that a single var statement be used per function. This can be declined with the vars option.
Hence why people prefer to have a single var, with multiple lines to make it more readable. Plenty of discussion on StackOverflow on a similar theme, if anyone's interested, e.g. use bit.ly/oza1yZ (can't post links yet)
Anyway, I'll go off and make that feature request when I get some time
|
|
|
Re: JavaScript formatter and comma separated vars [message #716592 is a reply to message #716481] |
Wed, 17 August 2011 19:29 |
|
On 8/17/2011 10:26 AM, Martin wrote:
> Hence why people prefer to have a single var, with multiple lines to
> make it more readable. Plenty of discussion on StackOverflow on a
> similar theme, if anyone's interested, e.g. use bit.ly/oza1yZ (can't
> post links yet)
>
> Anyway, I'll go off and make that feature request when I get some time :)
Oh, I completely understand why he urges variables to be declared at the
top of functions, it's the splitting of lines in conjunction with that
that I find dubious. Formatting in that way makes it more difficult for
me to spot check that the lines are separated by commas rather than
semicolons (my eyesight is _very_ poor), the accidental presence of one
semicolon on any of those lines being potentially disastrous.
--
Nitin Dahyabhai
Eclipse WTP Source Editing and JSDT
IBM Rational
_
Nitin Dahyabhai
Eclipse Web Tools Platform
|
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04677 seconds