Originally posted by acoder
Probably an inane question, but why do we use the current JS coding patterns?
Collapse
X
-
Here's what I don't like about omitting braces for single-line blocks:
[code=javascript]
var element = document.getEle mentById('theEl ement');
for( var i = 0; i < 10; ++i )
doSomething();
element.doSomet hingElse();
andSomeMoreStuf f('table');
[/code]
It's borderline-legible if indented properly; it would be a nightmare otherwise:
[code=javascript]
var element = document.getEle mentById('theEl ement');
for( var i = 0; i < 10; ++i )
doSomething();
element.doSomet hingElse();
andSomeMoreStuf f('table');
[/code]
Or occasionally I see this (*shudder*):
[code=javascript]
var element = document.getEle mentById('theEl ement');
for( var i = 0; i < 10; ++i )
doSomething();
element.doSomet hingElse();
andSomeMoreStuf f('table');
[/code]
And just to throw my hat in the stack, I absolutely *hate* it when programmers put the opening brace on the same line as the declaration.Comment
-
There is only one situation I have come across where the opening brace absolutely had to be on the same line as the opening statement...
[code=javascript]
var EventCache = function()
{
var listEvents = [];
return { // This brace must be on the same line or it errors
listEvents : listEvents,
( ... )
[/code]
I don't know why this is the case. All I do know is that if I move the brace one line lower, she throws an error.Comment
-
There's no excuse for non-indentation whatever the style.Originally posted by pbmodsHere's what I don't like about omitting braces for single-line blocks:...
Why do those who like the brace-on-new-line style always seem to hate the brace-on-same-line style? Just wondering...Originally posted by pbmodsAnd just to throw my hat in the stack, I absolutely *hate* it when programmers put the opening brace on the same line as the declaration.Comment
-
For me, it's because I'm used to following the curvy thing. It's just easier for me to line up matching braces rather than have to match it against indented text.Originally posted by acoderWhy do those who like the brace-on-new-line style always seem to hate the brace-on-same-line style? Just wondering...
It also helps spread the code out a bit and makes it look a lot cleaner to me.
Of course, I usually take it a step further and mess with parentheses, too. You'll often see a lot of this in my code:
[code=php]
$__stmt =
$db->prepareStateme nt
(
"EXEC [Some].[Stored].[Procedure] ?, ?, ?",
array
(
(int) $customerID,
(int) $offerID,
$somethingElseI Dunno
)
);
[/code]Comment
-
Oh, I know the reasons, understand them and accept them to an extent, but it still doesn't explain the absolute hatred for the on-same-line method as opposed to a simple dislike or just a preference of one over the other.Originally posted by pbmodsFor me, it's because I'm used to following the curvy thing. It's just easier for me to line up matching braces rather than have to match it against indented text.
It also helps spread the code out a bit and makes it look a lot cleaner to me.Comment
-
Oh, I don't hate it (I try never to hate anything; causes too much grief in this world). In fact, I am considering moving to it, but as a relatively new coder, I like to have my braces and brackets line up so that I do not forget to close them. It makes for easy bug fixing IMHO. Helps when you forget to open one too, or am I the only person that ever does that? =DOriginally posted by acoderOh, I know the reasons, understand them and accept them to an extent, but it still doesn't explain the absolute hatred for the on-same-line method as opposed to a simple dislike or just a preference of one over the other.Comment
-
That's OK then. At least we've got one. I'm not claiming the brackets/braces-on-the-same-line method is superior. I just feel that its supposed disadvantages seem a bit exaggerated.Originally posted by RMWChaosOh, I don't hate it (I try never to hate anything; causes too much grief in this world). In fact, I am considering moving to it, but as a relatively new coder, I like to have my braces and brackets line up so that I do not forget to close them. It makes for easy bug fixing IMHO. Helps when you forget to open one too, or am I the only person that ever does that? =DComment
-
Simply because it requires more (unnecessary) effort to scrutinise the code for missing braces, when their presence should be made clear.Originally posted by acoderThere's no excuse for non-indentation whatever the style.
Why do those who like the brace-on-new-line style always seem to hate the brace-on-same-line style? Just wondering...Comment
-
The fact that he's reiterating a convention doesn't mean that he agrees with it, just that he has to be seen to be consistent.Originally posted by acoderI'm not so sure of that. He wouldn't then include it as a coding convention, would he?Comment
-
Each to their own, I guess. I don't find it difficult as long as the code is properly indented. However, I'm not going to argue about which one is better. I might even admit that having braces on a new line is probably better from a logical point of view. If we started all over again, we probably would've had that as the de facto standard, so to speak.Originally posted by LogicianSimply because it requires more (unnecessary) effort to scrutinise the code for missing braces, when their presence should be made clear.Comment
-
remember this is not a religious forum :)) ... no, just kidding ... but to me it seems that there are two groups of developers ... the ones that includes the brackets in their (even personal) 'code-view' and the others that don't ... i have no special preference for one of them ... but we use the 'crockford'-way (bracket-on-the-same-line) at work and so i use to always use it ... like acoder said ... i find the pointed problems with that exaggerated ... the much more important point is about the indentation ... and one more point to mention :) ... long lines ... i really hate that, just to say that i hate something too ... i hate long lines > 80 chars :)
kind regardsComment
Comment