-
-
Save isaacs/357981 to your computer and use it in GitHub Desktop.
// See comments below. | |
// This code sample and justification brought to you by | |
// Isaac Z. Schlueter, aka isaacs | |
// standard style | |
var a = "ape", | |
b = "bat", | |
c = "cat", | |
d = "dog", | |
e = "elf", | |
f = "fly", | |
g = "gnu", | |
h = "hat", | |
i = "ibu"; | |
// comma-first style | |
var a = "ape" | |
, b = "bat" | |
, c = "cat" | |
, d = "dog" | |
, e = "elf" | |
, f = "fly" | |
, g = "gnu" | |
, h = "hat" | |
, i = "ibu" | |
; | |
// error in standard style | |
var a = "ape", | |
b = "bat", | |
c = "cat", | |
d = "dog" | |
e = "elf", | |
f = "fly", | |
g = "gnu", | |
h = "hat", | |
i = "ibu"; | |
// error in comma-first style | |
var a = "ape" | |
, b = "bat" | |
, c = "cat" | |
, d = "dog" | |
e = "elf" | |
, f = "fly" | |
, g = "gnu" | |
, h = "hat" | |
, i = "ibu" | |
; | |
// Objects: | |
// JSON.stringify style | |
var o = { | |
a : "ape", | |
b : "bat", | |
c : "cat", | |
d : "dog", | |
e : "elf", | |
f : "fly", | |
g : "gnu", | |
h : "hat", | |
i : "ibu" | |
}, | |
a = [ | |
[ "ape", "bat" ], | |
[ "cat", "dog" ], | |
[ "elf", "fly" ], | |
[ "gnu", "hat" ], | |
[ "ibu" ] | |
]; | |
// comma-first | |
var o = | |
{ a : "ape" | |
, b : "bat" | |
, c : "cat" | |
, d : "dog" | |
, e : "elf" | |
, f : "fly" | |
, g : "gnu" | |
, h : "hat" | |
, i : "ibu" | |
} | |
, a = | |
[ [ "ape", "bat" ] | |
, [ "cat", "dog" ] | |
, [ "elf", "fly" ] | |
, [ "gnu", "hat" ] | |
, [ "ibu" ] | |
]; | |
// errors in objects: | |
// JSON.stringify style | |
var o = { | |
a : "ape", | |
b : "bat", | |
c : "cat", | |
d : "dog" | |
e : "elf", | |
f : "fly", | |
g : "gnu", | |
h : "hat", | |
i : "ibu" | |
}, | |
a = [ | |
[ "ape", "bat" ], | |
[ "cat", "dog" ], | |
[ "elf", "fly" ] | |
[ "gnu", "hat" ], | |
[ "ibu" ] | |
]; | |
// comma-first | |
var o = | |
{ a : "ape" | |
, b : "bat" | |
, c : "cat" | |
, d : "dog" | |
e : "elf" | |
, f : "fly" | |
, g : "gnu" | |
, h : "hat" | |
, i : "ibu" | |
} | |
, a = | |
[ [ "ape", "bat" ] | |
, [ "cat", "dog" ] | |
, [ "elf", "fly" ] | |
[ "gnu", "hat" ] | |
, [ "ibu" ] | |
]; | |
// Addendum: effects on the return statement. | |
// It does not break. | |
return [ 1 | |
, 2 | |
, 3 | |
] // returns [1,2,3] | |
return { a : "ape" | |
, b : "bat" | |
} // returns {a:"ape",b:"bat"} | |
// even just separating two values by commas is fine, | |
// though a bit silly | |
return 1 | |
, 2 | |
, 3 | |
, 4 // returns the last value, 4 | |
// this, however is wrong: | |
return | |
1 | |
, 2 // returns undefined, because of semicolon-insertion. | |
// so is this. otb == fail. | |
return | |
{ a : "ape" | |
, b : "bat" | |
} // returns undefined, | |
// then creates a block with two named statements. | |
// this is ok: | |
return ( 1 | |
, 2 | |
) // returns 2 | |
// so is this: | |
return ( | |
{ a : "ape" | |
, b : "bat" | |
} | |
) // returns {a:"ape",b:"bat"} | |
// Addendum 2: A function call | |
doSomething( aPrettyLongVariableName | |
, "A string, which has some useful information" | |
, "If you put these all together, it'd be too long" | |
, { a: "is for antelope", b: "is for bat" } | |
, 42 | |
) | |
// Addendum 3: More realistic error in standard style: | |
// leaks FIVE globals! | |
var a = "ape eat banana", | |
b = "bat, allowed to fly", | |
c = "cat toy", | |
d = "dog chasing the mailman," | |
e = "elf lord", | |
f = "fly through the air", | |
g = "gnu is not unix", | |
h = "hat goes on your head", | |
i = "ibu isn't a cow"; | |
// Error: Can't call method 'forEach' of undefined. | |
// not passing in undefined as an argument!?? | |
mergeLists([ apple, [ penelope, granger ] ], | |
[ fun ], | |
[ 1, 2, 3, 4, 5, 6, 7, 8 ] | |
[ "mary's store has many pies, and cookies, and eggs," ] | |
[ function() { doSomething() } ]); |
@NoelBaron, linguistically, a compound-sentence has a comma so you'll pretty much know when to halt for a breather, but on your sample text, inserting a line-break before the comma (creating a comma-first look) also implies a full-stop or a new paragraph, comma on code has no linguistics value, and one should not be looking for such. On inventing a new program-language, we might as well use pipeline, hash-tag or any other characters to separate code-blocks, which do not have analogous meaning in English language-composition thus, not creating this confusion.
Most of pro-comma-first developers usually mention their lack of forgotten commas at the end of a long line of code, or even the OCD relief of same-column-straightening of characters ;)
Anyway, the comma-first convention is gaining quite a lot of popularity among users lately,
I can say that software-developers at Google (Israel) are using it on most of the new projects, due to its superb readability.
I give a star here since I discovered this place lol.
I'm using commas-first style almost since I started JS around 6 years ago now.
Guys you also missed something, commas-first is essential when you use multilines keyboard shortcut.
In VS or Sublime, CTRL + D for few entry in a JSON then END then add somehting, paste, or select a value. You don't have to deal wth the comma at the end.
Also CTRL + SHIT + TOP or DOWN in VS will make a multi-line cursor, then SHIFT + END and you selected all values excepted commas (or just select the keys, or the values, whatever).
I use this everyday everytime and each times I show this to a developer that is not using these shortcuts (and not using commas-first) he reconsider his position lol.
As a general rule, you should split your statement at the highest place possible in your syntax tree. Usually at an operator.
Since that operator is the pivot between 2 lines, it should be put prominently at the begin of the line.
Therefore the comma, or any other operator, +, &&, should start the line.
Use ternjs / eslint and get a life.
Putting the least important characters of the code up front is ridiculous.
actually, I love it and have been using it for years.
the comma on the left just looks like a bullet list.
ever since I stared using it - I never forgot a comma or struggled with braces again, even when working with nano on remote files.
but - I'm working with a team now that will agree to this style only if it will be supported by the lindter.
so - is there an eslint rule I can use to enforce this style, especially for arrays and objects?
Is there an eslint rule that will not choke when you do something like this? My eslint configuration complains about Project
being indented by 2 chars.
const {
Project
, Dimension
, Task
, View
, Layout
, User
, Group
, Organization
, CustomField
} = db.model;
It's too bad so few people use comma (and operator) first.
There are so many bad examples here.
Comma and operator first is really useful when you nest complex data structures or code.
Here is a case where it is good at:
x = {
a : ( b + 27 * (
foo1(
b
+ ( (c + 232312)
* ( foo2(
(d + 3423423 * foo34(34))
* (y+3243)
)
+ foo3(
(234 * sdf + 23 * dfdse )
, 23423 + foo45(23)
)
)
* ( 23423 / 34)
)
+ ( 234234 / sdf + 23423)
+ ( foo4(234234)
/ foo5(
foo34(
( ( fo345(3434 + 2323423)
+ 23423
)
/ 234
)
, [ 23234, 23432, 234234]
)
/ 349
)
)
)
))
, b : 234
, c : [
34534
, 534634
, 98
]
, d : {
x1 : [
23423
, 232
, 43+343*(
343
+ 324
)
, 23
]
, x2: 234
, x3: foo(2323 + (
( foo56(234) + 3434)
/ (2342 + foo6(2323))
))
, x4: 34534
}
}
I don't know how I got there, probably about the trailing comma debate I had with a colleague, but honestly this hurts my eyes so much. You don't do comma-first because firstly it's ugly and unintuitive. Javascript was designed like any coding language to be human-friendly. The creators knew how to read and write english, meaning that you should respect the comma position as you would in an non programming language.
Now if you write like this ,you'll see that this is by far the dumbest way to say "Hey I'm a hipster I write code with comma first" ,that's not how you're supposed to do things and you'll be the only one to do this in a team that would dare to hire you.
Comma is not a operator so I don't see the point how saying it should be the first thing in a line and in english (or any other language), a comma is attached to the previous word and a space (or a new line) must be put after this comma.
I really think I'm in the flat earth conference.
Not a chance @bhubr, some of us like our "comma-style":[2,"first"]
rule ;)
The only place I've seen it cause issues is with JSDOC blocks not being identified correctly by the IDE.
A lot of the examples in this thread are using variables values of same/same character lengths. When line length is random it becomes much more obvious that comma-first adds value.
So far I've not seen a single sound argument here pro comma last. All mentioned here is just comma-first is ugly or stupid.
Those in favour of comma-first mentioned some clear advantages though.
I code comma-first as well, looking for pro comma last, as it seems lots of tools and big names favour comma last. Any objective pro comma-last arguments that I might consider?
English and code are two different things. You cannot justify coding style with English text conventions.
Or we should write in paragraphs, indent the first line, capitalize the first word and justify the rest left and right.
X = {a : (b + 27 * (foo1(b + ((c + 232312) * (foo2 ((d +
3423423 * foo34 (34)) * (y+3243)) + foo3 ((234 * sdf + 23 *
dfdse), 23423 + foo45 (23))) * (23423 / 34)) + (234234 / sdf
+ 23423) + (foo4 (234234) / foo5 (foo34 (((fo345 (3434 +
2323423) + 23423) / 234), [23234, 23432, 234234]) /
349))))), b : 234, c : [34534, 534634, 98 ], d : {x1 :
[23423, 232, 43+343*(343 + 324), 23 ], x2 : 234, x3 : foo
(2323 + ((foo56(234) + 3434) / (2342 + foo6(2323)))), x4 :
34534}}.
And end with a period.
Gosh are people still having this debate in 2024??!? 😆😆😆
People, look around you, the whole industry is using Prettier and nobody cares about this nonsense. It's not 2010 anymore
@happyteque actually surprisingly its still relevant, no matter which language you use.I don't code in JS much anymore, but it's honestly sad this style isn't more prevalent, for one reason: git commits.
Commas are just noise - if you add a line, they are the reason you have two lines changed, not one, or you have random lines added in the middle of your array.
As for reading code, you can go screw your eyeballs and your dipshit opinions both - nobody reads code anymore, and it barely makes a difference. You are just an intolerant twat if you can't stand the difference. Grow the f* up. Everyone else did.
If "growing up" involves insulting strangers on the internets over something as petty as this, no, i'd rather not
Everything alright bro?
@happyteque Nah that part wasn't directed at you.
There are some individuals from ~2019 I'm more responding too, that seem to have had some pretty trash opinions.
"Growing up" means accepting that, normally, this stuff isn't important. And caring about objective stuff, not trash like your arrays looking you want them to.
"So if you want a single reason why not to use this crap, here it is: no one in his right mind does it, and no one wants to hear your explanation about why it's so brilliant,"
Touche. Maybe you should follow your own advice, lol.
So far I've not seen a single sound argument here pro comma last
@KoJaMan Lack of reading comprehension and/or willful ignorance does not an argument make.
The only comment you really need to read about this is mine, right here.
Good day.
@KoJaMan
@waynebloss
"Whoever said "use better tools" was right. (And there is a wrong here.)"
Partially correct.
As you said, the commas should really disappear, what we are really after are record separators not commas. I couldn't give a flyint hoot what you use, as long as my tooling can differentiate.
The tooling should be handling the job of presentation.
I suppose you could argue the git tooling could also be better, but two wrongs do not make a right. Building one tool to format everything wrong and another to hide that mistake is wasteful.
Besides, what happens when you want to see the commas? (For various reasons in different languages). Crying that your bad tool is prevalent is not a good reason to continue using bad tooling and bad formatting...
Which brings us to the real truth - there is just a lot of crappy code and crappy tooling that's prevalent. You learn to deal with it.
Thats being said, there are objective reasons to not promulgate the mess. Is this an issue I particularly care about? No. But I do care about people pretending its a settled issue instead of a papered over problem. It takes time, but we can always fix things (maybe in a thousand years?)
@waynebloss That comment mostly claims in a dramatic way that you dislike comma-first very much, and implies it is stupid. If it is about "punctuation should disappear", well, I disagree. When I see a new line I like to know why. I don't want to assume that there is probably a comma at the end of the previous line. I can navigate a complex expression faster if the delimiters, the symbols giving structure to the expression, are at the left. People work in different ways I guess.
I couldn't unsubscribe earlier because the mobile UI doesn't have that button. Finally I'm at my desk and can unsubscribe from this inane conversation. You people are crazy. This is all so utterly pointless.
"People work in different ways I guess."
Huzzah for someone who gets it, lol.
@fischerflorian
@waynebloss
@KoJaMan
To be clear, I do think punctuation is important.
This is one reason the prior comma format is (better charactersets non-withstanding), plainly superior.
How that is represented is of course partially up to the tooling, however the plain format having built in recognizable record delimiters is more graceful than relying on your IDE. The folks this bothers could easily move the comma to the end as a matter of visual representation.
The claim this doesn't matter is entirely stupid and shortsighted.
In the realm of civics and politics, paradoxically, the irrational choice is often the rational choice.
The rational reason to use comma-last formatting is the prevalence of irrational reasons for using comma-last formatting. Those unreasonable reasons are very widespread, and encoded in tools and expectations and other peoples' behaviors. You can scream from the mountaintops (or from the comment section of a gist that's old enough to be in high school at this point), but you'll probably never boil that ocean.
So go along get along, do the thing that is objectively worse, because in context, it is objectively better.
In the realm of civics and politics, paradoxically, the irrational choice is often the rational choice.
Which is why I barely care about this/think it will take thousands of years to fix (possibly).
The fun thing about civics and politics, are, that the winds do change from time to time. We can wear the fashions of the day and still be keepers of the truth(s).
It is never wrong to want to know the most objective truth, even if you will not today follow it, @isaacs .
I wish I was better at distinguishing AI so I could really understand whether this is humans waisting each others time, or computers doing an elaborate dance of bits.
Also, I think my comment of almost ten years ago has aged well...
Arguments like "if you think X you need to grow up" are just dismissive and not constructive.
When you say "@isaacs made an effort to explain why this particular style works for him."
- "made an effort to explain" suggests he didn't really explain.
- "works for him" (with emphasis on "him") suggests he is the only person to think like that.
No, isaacs explained why this style is preferred by him and a bunch of other people.
And "Can we all just go back to debating which Doctor Who is better?" is yet another appeal to ridicule. As actually your whole post is.
I don't think this comment needed to be repeated.
@Potherca "I wish I was better at distinguishing AI" lol real concern. But nope I'm real... (maybe a bit bored and this landed top in my inbox for whatever reason - that was probably an AI's doing).
I agree with @fischerflorian, your whole post remains as ridiculous as it was 10 years ago, and is wrong to boot:
"It makes about as much sense to debate if a painter should hold a brush in their left or right hand."
Yes, actually, it turns out some people are left handed. So, actually, it does matter for people, since they have different ways of working.
That doesn't mean some of the other points aren't untrue; but people with disabilities etc. find it much harder to exist in an intolerant, dismissive society. Which, TBH, a lot of folk here seem to be some flavor of.
On top of that, its not actually clickbait, if there are objective reasons this is better. Balanced by, how much of a gain will someone get from following this today:
- Start your own project and enforce this rule - good job! Well you probably pissed some people off. You can't make everyone happy.
- Existing project with opposite rule - probably not worth starting a fight over. But might be worth seeing if people are willing to switch. There are mostly only positives.
@fischerflorian The sentence "made an effort to explain" is meant to congratulate the author on actually making an effort, as in, taking the time and energy to define and explain why comma-first can be superior to conventional comma notation. It is not meant as dismissive. I am sorry my intent is not clear, and that you should read that into such a sumple statement.
The same goes for "works for him" (with emphasis on "him") which is to emphasise that this is something that works for the author (inclusive) rather than only for them (exclsive)
Regarding:
And "Can we all just go back to debating which Doctor Who is better?" is yet another appeal to ridicule. As actually your whole post is.
As the actual author of the post, and thus being aware of my own intent, I can assure you my intent is not to ridicule, but to assert the opinion that, where the actual use of comma-first notation has merit, the mostly repetitive discusion about it does not hold any merit.
In response to
I don't think this comment needed to be repeated.
I don't think any of the points made in previous comments need repeating. In fact, I think that almost the entire comment thread can be deleted without much loss to humanity as a whole, but regardless of our opinions, here we are.
I do feel you might be trying to read my comment as negative on purpose. Please note, nowhere do I state that I disagree with the author. Just with people who seriously feel this is a viewpoint that needs to be either defended or attacked. It is neither wrong nor right. There are no sides. It either works for people or not. Debating it as if there is some rational to this that works for everyone is just another thinly veiled semi-religious timewaster.
@smaudet Good to hear! Human here too.
Yes, actually, it turns out some people are left handed. So, actually, it does matter for people, since they have different ways of working.
That's more or less my point. Some people are left-handed. Correct. But how much discussion do we have about that fact? Remember educational history, when people were not allowed to be right handed? Much misery ensued. Discussing the fact whether left-handed people should be left-handed or not is senseless. Some people are left-handed. End of debate. For me, comma-first falls under the same no-brainer. Some people prefer it. Let them. It comes down to wiring more than preference. Why discuss it (at length)?
people with disabilities etc. find it much harder to exist in an intolerant, dismissive society.
Which, TBH, a lot of folk here seem to be some flavor of.
That I agree with wholeheartedly. That was (and is) also the reason for me to complain in general about the state of comments and their tone.
Regarding whether the title "A better coding convention for lists and object literals in JavaScript" is clickbait or not... To me it is. It doesn't say what the improvement is (comma-first) so I have to click a link to go here and read it. That signals clickbait to me. But hey, opinions, right?
I've used the comma-first notation for years, even before my original comment. Not even just in JS but also PHP projects. I've never had much complaint or trouble. Most folks I worked with didn't need any convincing. They saw the merit immediatly.
For the last five to seven years, I've not used it at all. Everyone just uses an editor or IDE and fixes the squigly red marks that appear when comma's go awry. At this point I no longer have a preference... But I still think it is silly discusssing the topic at this length.
Not that I am above sillyness myself 😼
Reading back the comment, my current favorite is:
If the IDE can tell where commas ought to be, why doesn't it just put them there? Why the red flag dance?
As in 2024, GitHub Co-Pilot does precisely that. How far we have come... 🥲
For nearly 16 years of our lives, we write essay after essay using commas. On top of that, we write billions of emails. With all the variables and preferences in our shared environments, shouldn't we at least adhere to our life-long habit of left-to-right comma interpretation?
In my humble opinion, I prefer reading code that isn't totally opposite our natural tendencies.
This doesn't feel natural at all, although technically it is the same text: