Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Tuesday, February 9, 2016

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

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


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.

Answer by koenpeters for Why do lots of programmers move commas to the next line?


This is because the comma belong to the new line next statement and not the previous one. For example:

If you have this:

a,  b,  c  

If you need to remove the c then you need to delete two things: de c and the comma on the previous line. If you do this:

a  ,b  ,c  

now you only need to delete the ,c line. It makes more sense this way, because the comma behind the b in the first example only is needed because of the c. It does look worse this way though. It's a trade off between maintainability of your code and the way it looks.

Answer by Dave Newton for Why do lots of programmers move commas to the next line?


It's one way to make sure you don't forget the comma when adding a new item to a collection, and don't accidentally leave on a trailing comma in collections.

By putting it on the new line it's visually obvious.

I don't care for it, but I understand why people would.

Answer by Shelby Melban for Why do lots of programmers move commas to the next line?


It is easier to just look at your code to verify you have a comma where needed. If you had to scan the end of each line of code the missing commas wouldn't just jump out like they do when they are lined up on the left hand side.

Answer by Michael Berkowski for Why do lots of programmers move commas to the next line?


This offers a little bit of protection in languages which don't accept trailing commas from accidentally introducing syntax errors with trailing commas

In SQL, trailing commas will cause syntax errors. In JavaScript, it will be accepted most places, but will fail with a cryptic error in some Internet Explorer versions, for example.

JS works in most browsers, but fails in some

var thing = {   a: 1,   b: 2,   // trailing comma   c: 3,  };  

Syntax error in SQL

SELECT    col1,    col2,    -- Syntax error in SQL    col3,  FROM table  

Answer by archil for Why do lots of programmers move commas to the next line?


Maybe because removing or adding line and its commas is simpler with second example

Answer by Protron for Why do lots of programmers move commas to the next line?


If you have an extra comma in the end of the last line it will work in some browsers but not in all browsers. Making the error harder to detect (fail-fast).

Plus, having the comma at the beginning of the line, make it simpler to add a line at the end and you will have to touch only that line (you will not need to add the comma in the line before).

This thing about touching only that line has more impact if you are using version control and viewing the diff.

Someone can argue that adding a line at beginning of the array or object will need the same extra work of touching 2 lines (if you use commas at the beginning), but I guess everyone will agree that inserting a line at the beginning is much less likely that inserting a line at the end of it.

Answer by nwellcome for Why do lots of programmers move commas to the next line?


You might be looking at generated code, for instance, when writing a loop to generate an SQL select statement sometimes I will write it like:

sql =  "SELECT";  sql += "  table.id"; // or some field that will always be in the query  for (var i = 0; i < 10; i++;) {    sql += ",  table.field" + i;   }  sql += "FROM table" // etc  

Instead of adding the comma at the end and then having a condition to omit it on the last iteration of the loop or doing:

sql =  "SELECT";  for (var i = 0; i < 10; i++;) {    sql += "  table.field" + i + ",";   }  sql += "  table.id";  sql += "FROM table" // etc  

Which is functionally equivalent, but then the ID doesn't appear where I usually want it.

Answer by timrwood for Why do lots of programmers move commas to the next line?


I think it's done so that it's easier to spot a missed comma.

var something = 0,      foo = "a string",      somethingElse = []      bar;    var something = 0    , foo = "a string"      somethingElse = []    , bar;  

Answer by YoYoYonnY for Why do lots of programmers move commas to the next line?


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


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.