tags:

views:

351

answers:

12

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
              )
+3  A: 

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.

buti-oxa
+1  A: 

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.

stephenbayer
and given how easy todays ides make duplicating the last line, it can be a gem. (netbeans ctrl+shift+down)
Midhat
+3  A: 

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.

Daniel Spiewak
A: 

I know when I wrap and's in a sql or if statement I try to make sure the and is the start of the next line.

If A and B
and C

I think it makes it clear the the C is still part of the if. The first format you show may be that. But as with most style questions the simple matter is that if the team decides on one style then it should be adhered to.

osp70
+1  A: 

Seems to me like a personal choice.

Pradeep
+1  A: 

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;
Max Lybbert
+15  A: 

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.

BradC
The second bit applies to any language that has a "comment to end of line" marker. That includes C++, C#, Java, JavaScript, VB, etc., etc.
AaronSieb
... any language that can't tolerate a dangling ',' at the end of the list
Javier
Well, VB and VB.Net won't let you arbitrarily split statements over multiple lines, so this technique won't work there.
BradC
+1  A: 

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
              )
Judah Himango
+2  A: 

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.

James Curran
+1  A: 

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.

matt b
+1  A: 

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.

Stephen Denne
A: 

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.

Ferruccio