Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do lots of programmers move commas to the next line? [closed]

Tags:

Tell me please, what is the sacred power of the style below:

var javascript = new Language(   'Brendan Eich' , new Date(1995, 0, 1) , ['C', 'Java', 'Scheme'] ); 

Why do lots of programmers use that style? What benefits does it have? For example,

var javascript = new Language(   'Brendan Eich',   new Date(1995, 0, 1),   ['C', 'Java', 'Scheme'] ); 

I like much more than previous. Thanks.

like image 939
ValeriiVasin Avatar asked May 07 '12 14:05

ValeriiVasin


People also ask

What is a dangling comma?

A trailing comma, also known as a dangling or terminal comma, is a comma symbol that is typed after the last item of a list of elements. Since the introduction of the JavaScript language, trailing commas have been legal in array literals. Later, object literals joined arrays.

Why does CODE end with semi colon?

Semicolons are end statements in C. The Semicolon tells that the current statement has been terminated and other statements following are new statements. Usage of Semicolon in C will remove ambiguity and confusion while looking at the code.

What is at the end of a line of code?

Newline (frequently called line ending, end of line (EOL), next line (NEL) or line break) is a control character or sequence of control characters in character encoding specifications such as ASCII, EBCDIC, Unicode, etc.

Are trailing commas good?

Trailing commas (sometimes called "final commas") can be useful when adding new elements, parameters, or properties to JavaScript code. If you want to add a new property, you can add a new line without modifying the previously last line if that line already uses a trailing comma.


1 Answers

Lots of great answers already. Allow me to give you my own one, to make things as clear as possible.

I personally call this way of writing code 'Haskel style', since it's a common style to use in Haskell. Let me give you a Haskell example first:

data Settings = -- The user settings     { has_sound     :: Bool   -- Determines if the user has sound     , has_power     :: Bool   -- Determines if the user has electricity     , has_graphics  :: Bool   -- Determines if the user has graphics     , user_name     :: String -- The name of the user     , user_password :: String -- The hashed password of the user     , user_email    :: Email  -- The email address of the user     , stylesheet    :: Style  -- The stylesheet to use     } 

And a Javascript snippet from one of my projects:

var events // Holds the events to generate a event handler for.   , var2   // Quick description for var2.   , var3   // Quick description for var3.   , ...    // ...   ; events = // Event handlers will be generated for the following events:     [ "onmousedown"  // Works outside of the window element     , "onmouseup"    // Works outside of the window element     , "onmousemove"  // Works outside of the window element     , "onmousewheel" // This will handle DOMMouseScroll aswell     ]; 

Benefits of 'Haskell style'

Easy to read

'Haskell style' takes advantage of the column style layout. This column style makes your code more readable. In fact, it makes your code so much more readable that you use it all the time. Imagine writing code without tabs or leading spaces!

By taking advantage of column style layout, variable names, types, etc. are easier to read aswell. By grouping up variables by prefix, our future reader will easily find what he is looking for, without using a advanced search query.

Easy to document

Column style layout has more advantages. By grouping up our code we can add a column reserved for comments. Now you can read your code without even needing color highlighting, and adding information to your comment is as easy as finding the right column and modifying it. Besides, this column-like style of documenting your code is pretty much what you get after using a documentation generator like Doxygen, removing the necessity for this kind of tool.

Easy to notice mistakes

Noticing a missing comma is a piece of cake using this style of coding. Simply look for a line that doesn't start with it! On the other side of the spectrum, we have the comma's at the end of the line. We missed one? Nope, because it is the last element, or because the expression continues on the next line. And finding the first element in a list is as easy as could be. When dealing with long lines, the first element is easily overlooked, but by placing the first element on it's own line and puting a [ or { instead of a , right in front of it, it's easy to spot.

Easily scalable

You might say "But this layout style will get imposible to handle once the expression gets big!", which is quite true, but is this any different for the rest of your code? I think that by using column style you will at least keep your code readable, which in the long run is worth more than the struggle you might have to fit it into a column layout.

All in one example!

var scalable = // This is a example variable     [         [ new MyObject // This is how I would style Object Allocation               ( "11"               , "This is the first element"               , function // This is a very secret function...                   ( secret   // ..with secret..                   , variable // ..variable..                   , names    // ..names!                   )                 {                     // <-- Use spaces, not tabs :)                 }               )         , "12"         ]     ,         [ { id:   21                          // Where's 20?           , name: "This is the third element" // It sure is           , func: function() { /* My body feels empty :c */ }           }         , "22" // Notice how 21 is a integer, not a string. Sneaky!         ]     ]; 

TL; DR

This style of placing comma's, 'Haskell style', has a couple of advantages:

  • Easy to read
  • Easy to document
  • Easy to notice mistakes
  • Easily scalable
like image 55
yyny Avatar answered Oct 15 '22 23:10

yyny