Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the advantage of this peculiar formatting?

Tags:

coding-style

I've seen this format used for comma-delimited lists in some C++ code (although this could apply to any language):

void function(  int a
              , int b
              , int c
             )

I was wondering why would someone use that over a more common format such as:

void function (int a,
               int b,
               int c
              )
like image 733
Ferruccio Avatar asked Oct 23 '08 18:10

Ferruccio


11 Answers

That's a pretty common coding style when writing SQL statements:

SELECT field1
     , field2
     , field3
--   , field4
     , field5
FROM tablename

Advantages:

  • Lets you add, remove, or rearrange fields easily without having to worry about that final trailing comma.
  • Lets you easily comment out a row (TSQL uses "--") without messing up the rest of the statement.

I wouldn't think you'd want to rearrange parameter order in a function as frequent as you do in SQL, so maybe its just somebody's habit.

The ability to comment one of them out will depend on the specific language being used. Not sure about C++. I know that VB.Net wouldn't allow it, but that's because it requires a continuation character ( _ ) to split statements across lines.

like image 64
BradC Avatar answered Oct 05 '22 14:10

BradC


It is easier to add a parameter at the end starting by duplicating previous parameter (line).

Make sense when you are sure that first parameter will never change, which is often the case.

like image 38
buti-oxa Avatar answered Oct 05 '22 13:10

buti-oxa


Malice?

Seriously though, it's hard to account for formatting style sometimes. It's largely a matter of personal taste. Personally, I think that both forms are a little nasty unless you're seriously restricted in terms of line-length.

like image 44
Daniel Spiewak Avatar answered Oct 05 '22 14:10

Daniel Spiewak


Another advantage is that in the first example you could comment-out either the B or C lines, and it will stay syntactically correct. In the second example, if you tried to comment out the C line, you'd have a syntax error.

Not really worth making it that ugly, if you ask me.

like image 40
James Curran Avatar answered Oct 05 '22 15:10

James Curran


The only benefit I would see, is when you add a parameter, you just have to copy and paste the last line, saving you the extra couple key strokes of editing comma position and such.

like image 33
stephenbayer Avatar answered Oct 05 '22 13:10

stephenbayer


Seems to me like a personal choice.

like image 21
Pradeep Avatar answered Oct 05 '22 15:10

Pradeep


No reason, I suspect it's just a matter of personal preference.

I'd personally prefer the second one.

void function (int a,
               int b,
               int c
              )
like image 27
Judah Gabriel Himango Avatar answered Oct 05 '22 14:10

Judah Gabriel Himango


The only benefit I would see, is when you add a parameter, you just have to copy and paste the last line, saving you the extra couple key strokes of editing comma position and such.

The same goes for if you are removing the last parameter.

like image 43
matt b Avatar answered Oct 05 '22 15:10

matt b


When scanning the file quicky, it's clear that each line that begins with a comma is a continuation of the line above it (compared to a line that's simply indented further than the one above). It's a generalization of the following style:

std::cout << "some info "
    << "some more info " << 4
    + 5 << std::endl;

(Please note, in this case, breaking up 4 + 5 is stupid, but if you have a complex math statement it may be necessary).

I use this a lot, especially when dealing with conditionals such as if, for, and while statements. Because it's also common for one-line conditionals to omit the curlies.

 std::vector<int> v = ...;
 std::vector<int> w = ...;
 for (std::vector<int>::iterator i = v.begin()
       , std::vector<int>::iterator j = w.begin()
       ; i != v.end() && j != w.end()
       ; ++i, ++j)
           std::cout << *i + *j << std::endl;
like image 31
Max Lybbert Avatar answered Oct 05 '22 14:10

Max Lybbert


When you add another field to the end, the single line you add contains the new comma, producing a diff of a single line addition, making it slightly easier to see what has changed when viewing change logs some time in the future.

like image 45
Stephen Denne Avatar answered Oct 05 '22 14:10

Stephen Denne


It seems like most of the answers center around the ability to comment out or add new parameters easily. But it seems that you get the same effect with putting the comma at the end of the line rather than the beginning:

function(
          int a,
          int b,
//          int c,
          int d
        )

You might say that you can't do that to the last parameter, and you would be right, but with the other form, you can't do it to the first parameter:

function (
//             int a
           , int b
           , int c
           , int d
          )

So the tradeoff is being able to comment out the first parameter vs. being able to comment out the last parameter + being able to add new parameters without adding a comma to the previous last parameter.

like image 33
Ferruccio Avatar answered Oct 05 '22 14:10

Ferruccio