|
I like you, refused to use 1TBS style braces (it's based on K&R and what JavaScript uses). I came from a C/C++ and PHP world after all. I was a real programmer. Besides, the style sucked. It's evil and ugly. Somewhere ponies will die if I used them.
However, there is a valid reason why 1TBS became so popular with JavaScript. That code on the site was simply written by a rookie to JavaScript. Check out section 3 in the following link...
Brace styles and JavaScript[^]
That one little tidbit is the only valid reason for it and it started it all. In other words, it's only way to return an object literal like that in a function. So the community just adapted this style.
Jeremy Falcon
|
|
|
|
|
Jeremy Falcon wrote: That one little tidbit is the only valid reason for it and it started it all. In other words, it's only way to return an object literal like that in a function. So the community just adapted this style.
Yup - just read about that from Nemanja Trifunovic's post.
That was a learning moment for me!
|
|
|
|
|
Oh snap, he beat me to it. It was a moment for me too man. Had it not been for that one point I'd not be using that style either. The only good news is at least you get used to it... at some point.
Jeremy Falcon
|
|
|
|
|
TOOTBS (aka K&R) is only about layout of statements. It gets confused with object literals purely because they use braces but for an entirely different purpose; same as '(' and ')' have multiple uses, as does the ','. That, combined with the lazy end of statement convention, is the only rationale for using TOOTBS. But you can use logical aligned braces even when returning objects either by making them subexpressions (using one of the alternative uses of '(' and ')') or by converting the object to a named object. Viz:
return (
{
p1: 1,
p2: 2
});
var result =
{
p1: 1,
p2: 2
};
return result;
|
|
|
|
|
Using a variable is a given as a way to dance around it. As far as your first point it solves nothing and doesn't illustrate the point we're making. Try running it in regards to what we're talking about. In fact, here's a fiddle...
Edit fiddle - JSFiddle[^]
Jeremy Falcon
|
|
|
|
|
Btw, when we say object literal in a return statement, that explicitly implies to not use a variable. Which is the entire point of what we're talking about.
What is literal? Webopedia Definition[^]
Jeremy Falcon
|
|
|
|
|
The only language I know of that gives a crap is tcl. Fortunately I don't have to write it, I only have to parse it.
|
|
|
|
|
Perhaps someone has mentioned this but haven't read *all* the responses
There is actually a bit of a caveat with JS: it terminate some lines automatically (like an invisible semi-colon)
Open your browser and go to the dev-tools (typically F12) and enter the following:
var f1 = function() {
return {
value: 'the-value'
};
}
var f2 = function() {
return
{
value: 'the-value'
};
}
Now have it execute
f1() and then
f2() . You'll quickly see the difference
It is better to stick with the "standard" with braces on the same line. For my C# code it's is quite different.
|
|
|
|
|
Generally follow the standards / style guide at your place of work.
However, with JavaScript there can be gotchas; due to semicolons being optional, as per this example:
javascript - Why does a results vary based on curly brace placement? - Stack Overflow[^]
So you have to be cautious that in such cases you use the code that works over the dictated style; and put a comment so no one decides to clean up your formatting not realising that they're introducing a bug.
If you're defining a style, I'd say it's best to pick one which avoids this issue, so going with some variant of K&R makes sense for this language, to avoid such pitfalls & any cognitive load related to spotting them.
For most other languages (i.e. where placement is solely formatting), I'd typically recommend Allman style for clarity (personal preference); though I may suggest K&R for any projects where the same developers are likely to work with JavaScript; again so they don't have to think about it (i.e. when switching languages).
Regarding my personal preference; historically I was a K&R advocate, but later realised that the Allman style allows lining up the open and close brackets, which is more obvious than lining up the code responsible for the bracket. Once you develop the habit of thinking "the line before the open brace is the one defining this block" rather than "the first line of the block defines the block & ends with the opening brace" it pays off. You do use up more screen real estate; but often (at least for developers with large monitors), that's good, since it removes clutter.
|
|
|
|
|
The obsessive compulsive in me hates the 'brace on same line' style. It
feels like you're deliberately starting something on the end of a sentence. Start
a statement/block at the start of a line, that's easiest for most of us to read. I
think.
(If that paragraph was a little harder to read than normal, my point is made).
There's a good case for the vertical compression that the K&R style offers, showing more code on screen at the same time makes it easier for the reader to take in more at once. Ideally, he/she should be able to see any function/method onscreen at once.
Conversely, if you have nested blocks, I find the Allman style far clearer in determining which brackets match with which.
Ideally the editor would just display according to the preferences of the viewer, but as per the js example above, it's probably not always feasible. I don't know if source-comparison tools are smart enough to determine "nothing changed apart from indentation/non-printing characters, so mark nothing as changed".
|
|
|
|
|
Yes, it is the "correct" style for Javascript, even though I hate using it. When I program in other languages, I always use a new line for a brace, either opening brace or closing brace.
But in Javascript, if you use the opening brace in a new line, Javascript may automatically insert a semicolon in the previous line.
Try searching the Internet about this "feature". For example, you may consult javascript - Why does a results vary based on curly brace placement? - Stack Overflow[^]
|
|
|
|
|
The simple solution is to use a text editor that can be customized. The first thing I do to when I get a source code file is use my editor's auto-format feature to reformat the file to my preferences.
|
|
|
|
|
Tel Aviv: I'm told it Israeli a fun place to go.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
bring a partner, always more fun when you kosher
Installing Signature...
Do not switch off your computer.
|
|
|
|
|
Jewish mate!
... such stuff as dreams are made on
|
|
|
|
|
Well Galilee, that was a tough one to catch. There's Eilat to be said for visiting Israel.
Ravings en masse^ |
---|
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein | "If you are searching for perfection in others, then you seek disappointment. If you are seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010 |
|
|
|
|
|
Ask for them cookies[^] and pray they don't colonize your arse.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
That one was so bad, I have Haifa mind to report it as abuse.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
|
His posts are so bad, it makes me think he has a Holon his head.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
|
That pun Gaza ginst all my principles. Jewish to repent?
If you think 'goto' is evil, try writing an Assembly program without JMP.
|
|
|
|
|
|
Aaand another one Uzis soon the drain...
|
|
|
|
|
Oh Meirs - I am Begin you to stop this. Peres the thought that this might Sharon the Netanyahus started spreading it around.
I'm pretty sure I would not like to live in a world in which I would never be offended.
I am absolutely certain I don't want to live in a world in which you would never be offended.
Freedom doesn't mean the absence of things you don't like.
Dave
|
|
|
|