-
25 May 2007 11:37 AM #1
Leading comma or Trailing comma: that is the question.
Leading comma or Trailing comma: that is the question.
As you may have noticed from code fragments I've posted on this forum that I'm using leading commas in my code. People keep asking me about reasons why I have decided so and if there are any positive or negative consequences of this so I have decided to write this article where I would explain my whys. What I would like to state at the beginning is that I do now want this thread to become a war of friends and enemies like some Windows vs. Linux, or Intel vs. AMD, discussions. I do not want anybody to change their coding practices because: The truth for you is that what works for you.
What works for me are leading commas. Wky?
First, compiler or script interpreter doesn't care if I've written leading or trailing commas. The only who cares is myself and other human beings that will read, understand, modify and debug the code.
So first comes reading and understanding. Lets take the following examples:
In this example, if I want to check if I haven't forgotten a comma or if I don't have an extra comma there (infamous IE extra comma error) I have to look at the ends of all five lines with various lengths to see if comma is there and, in the case of the last line, to check that comma is not there.PHP Code:var o = {
id: 2,
useAccessibilityFeatures: true,
autoLoad: true,
name: 'demo',
translateHelpTexts: true
};
In this example I just look to the code as a whole if it has the expected apperance (first line w/o comma to the left and then column of commas).PHP Code:var o = {
id:2
, useAccessibilityFeatures:true
, autoLoad:true
, name:'demo'
, translateHelpTexts:true
};
You know, one line will always be different because number of commas is one less than number of properties.
The modifying of the code or putting some leading comments before the properties to temporarily disable them is pretty same in both cases. What makes bigger difference is a handling of errors I may have done in the code by browser. Let's make an error in the examples:
What happens here? Please-help-my-app-works-in-FF-but-not-in-IE help forum query, waiting for some hours for reply and then not believing: How could I be that stupid?PHP Code:var o = {
id: 2,
useAccessibilityFeatures: true,
autoLoad: true,
name: 'demo',
// translateHelpTexts: true
};
And what happens here? Nice fat syntax error in both Firefox and IE with line number. Finding the line in the code and fix it is the matter of seconds and I can continue with some more important things.PHP Code:var o = {
// id:2
, useAccessibilityFeatures:true
, autoLoad:true
, name:'demo'
, translateHelpTexts:true
};
I have better things to do as to hunt a stray extra trailing comma in the code for hours. And you?Jozef Sakalos, aka Saki
A lot of valuable info at:
Saki's Extensions and Plugins
Saki's Extensions and Plugins Docs
Saki's Examples, Latest: Grid in Card Layout
Saki's Blog, Featured: Writing a Big Application in Ext, Latest: Grid MultiSearch Plugin Video
-
25 May 2007 12:36 PM #2Sencha - Community Support Team
- Join Date
- Mar 2007
- Location
- Frederick MD, NYC, DC
- Posts
- 16,169
- Vote Rating
- 28
I read the perl best practices book a long while ago and it's transformed my programming style a lot.
It's all preference. I choose commas at the end but concatenation in the front.
PHP Code:
myobj = { x : '1',
y : '2',
z : '3'
};
// PHP/perl
$var = "data"
. $row["data1"]
. $row["data2"]
. "etc";
//JavaScript
var myVar = 'data'
+ row['data1']
+ row['data2']
+ 'etc';

Jay Garcia @ModusJesus || Modus Create co-founder
Ext JS in Action author
Sencha Touch in Action author
Get in touch for Ext JS & Sencha Touch Touch Training
We are also working on Video-based Sencha Touch training: Check it out here.
-
25 May 2007 12:43 PM #3
I was think of switching to using leading commas in my code. I already do it with sql statements, why not with javascript? Here's a sample of my sql formatting:
Code:SELECT field1 , field2 , field3 FROM table1 WHERE field1 = somevalue1 AND field2 = somevalue2 AND field3 = somevalue3
-
25 May 2007 12:50 PM #4
Jozef Sakalos, aka Saki
A lot of valuable info at:
Saki's Extensions and Plugins
Saki's Extensions and Plugins Docs
Saki's Examples, Latest: Grid in Card Layout
Saki's Blog, Featured: Writing a Big Application in Ext, Latest: Grid MultiSearch Plugin Video
-
25 May 2007 12:52 PM #5Sencha - Community Support Team
- Join Date
- Mar 2007
- Location
- Frederick MD, NYC, DC
- Posts
- 16,169
- Vote Rating
- 28
I think the leading commas just don't look that pleasing to the eye. that's just me though


Jay Garcia @ModusJesus || Modus Create co-founder
Ext JS in Action author
Sencha Touch in Action author
Get in touch for Ext JS & Sencha Touch Touch Training
We are also working on Video-based Sencha Touch training: Check it out here.
-
25 May 2007 1:01 PM #6
Jozef Sakalos, aka Saki
A lot of valuable info at:
Saki's Extensions and Plugins
Saki's Extensions and Plugins Docs
Saki's Examples, Latest: Grid in Card Layout
Saki's Blog, Featured: Writing a Big Application in Ext, Latest: Grid MultiSearch Plugin Video
-
25 May 2007 1:02 PM #7Jozef Sakalos, aka Saki
A lot of valuable info at:
Saki's Extensions and Plugins
Saki's Extensions and Plugins Docs
Saki's Examples, Latest: Grid in Card Layout
Saki's Blog, Featured: Writing a Big Application in Ext, Latest: Grid MultiSearch Plugin Video
-
25 May 2007 3:09 PM #8
I had started using leading commas about a year ago for the same reasons that Saki mentioned in the lead of this post. It is just so much easier to debug. Although, I hadn't though of using the leading commas in SQL, and leading operators, but you can bet I will now.
Just another reason this forum has become my favorite.
As an aside, I have to say that I've been in the extjs.com forum (and originally the forum as it existed on jackslocum.com before Ext went final). I've screened through many posts that have rocketed my understanding of javascript and sharpened my coding practices. But most recently, I've noticed that Saki has contributed a wealth of information and takes his time to make some of the most informative posts I've seen in here. I wish there was an award we could give to Saki for the time that he takes to contribute to the forums here!
Thank you Saki!
-- W.G.
-
25 May 2007 3:15 PM #9
IMO, leading commas make code difficult to read. Notice in your first example how each "block" lines up perfectly. This means you can look at code and easily scan for blocks. This is critical for someone like me who likes to "scan" code. I can digest a lot of code quickly by looking at it and following the flow. With leading commas (or by omitting braces), I wouldn't be able to do that because I can't quickly decipher where each block begins and ends because they don't line up. Just my opinion.

-
25 May 2007 3:22 PM #10
Jozef Sakalos, aka Saki
A lot of valuable info at:
Saki's Extensions and Plugins
Saki's Extensions and Plugins Docs
Saki's Examples, Latest: Grid in Card Layout
Saki's Blog, Featured: Writing a Big Application in Ext, Latest: Grid MultiSearch Plugin Video


Reply With Quote