tags:

views:

178

answers:

7

Sometimes, when I have a multi-case if, or a very simple for, with only two statements, I will forgo braces, instead using the comma. Is this a bad exploitation of the feature, and is it ugly and bad form? Or is it an acceptable way to save time and space?

For example:

if (something)
    b = y, c = z--;

instead of:

if (something) {
    b = y;
    c = z--;
}
+3  A: 
  • It won't save more than a few moments of typing time.

  • If you really need to save space on screen that much, you need a bigger screen (or you should run a bigger terminal window).

  • It makes no difference to the object code produced. Therefore, it has no affect on runtime.

  • It is harder to step through the component statements of a comma expression in a debugger.

I think it is easier to read the colon-separated (and hence 'braced') code, without it being significantly harder to type the braced version. (After the length of time I've been coding in C and using braces, I'd have to think hard to remember to use the comma notation.)

Jonathan Leffler
+1  A: 

I have never used the comma syntax. But this is because I didn't know it existed, to be honest.

If I knew about it, then I would have happily used it in place of annoying braces for a mere two or three statements.

So in my opinion, use at will! Just so long as you don't make that classical mistake:

if (cond)
  doSomething();
  doSomethingElse();  // <-- oops, unconditional statement!
Alexander Rafferty
+12  A: 

It's indeed a clever way to use that syntactic feature of most C-like languages.

Personally, I try to stay the least ambiguous as possible when I code, so I always include { and } in all of my if statements. It may save time, but I prefer clarity: it doesn't speed up or slow down the code execution.

Sean
I agree. I worked on some code where at some point someone had taken: if (a>0) DoSomething(); and turned it into if (a>0) DoAnotherThing(); DoSomething();, which introduced a bug that was difficult to find
Andrew
I disagree. I find that full use of curley braces makes the coder harder to read - it is visually cluttering.
Blank Xavier
Its not so bad if you include the brace on the if line.
mathepic
I would really like the python identation style in C/C++.
ruslik
+4  A: 

I consider it very good style, but I'm sure others will disagree.

One particular variant use of the comma operator is within the parts of a for statement, as in:

for (i=0, j=1; i<j; i++, j++) { ... }

R..
+1 for "correct" comma usage in an "if" statement.
John Gaughan
@John: I'm confused - what if statement are you referring to?
R..
Keep in mind only the second comma is the comma operator.
GMan
@GMan : Please enlighten me. Do you mean `,` between `i++` and `j++` is the only comma operator here? What about `,` between `i=0` and `j=1` (is it just a separator)?
Prasoon Saurav
@Prasoon: That's just declaration syntax, like `float f, g;` The comma operator takes two expressions.
GMan
@Gman: I disagree with your assessment that `i = 0, j = 0` in the `for` loop is anything other than a comma expression. C99 permits a variable declaration at that position; however, the old syntax is also supported and is not a variable declaration. For it to be a declaration, there would have to be a type named (e.g. `for (int i = 0, j = 0; i < k; i++, j++) { ... }`). Quoting the standard - C99 §6.8.5: `for ( expression<sub>opt</sub> ; expression<sub>opt</sub> ; expression<sub>opt</sub> ) statement` or `for ( declaration expression<sub>opt</sub> ; expression<sub>opt</sub> ) statement`.
Jonathan Leffler
@Jonathan : Yes I agree with what you are saying. I think @GMan missed this somehow. :)
Prasoon Saurav
@Jonathan: Ohhhh, you're right, I just read the damn thing wrong. (I'm used to an `int` there.) Yes, these are both comma operators. @Prasoon: Sorry, I read it wrong.
GMan
I would never write `int` there. Declaring your variables inside a `for` statement is the worst style violation in my book. :-) And it also inhibits functionality (using the values at the end of the loop) which in my example would likely be very important.
R..
+7  A: 

I'd vote against it for a few reasons:

  • It's hard to immediately see, just at a glance, that there's more than one assignment.
  • I'm in favor of always putting in braces, because there are times where it's convenient to go back through, e.g. during debugging, and add code that also gets executed in that block.
  • It's not really saving a lot. The compiler is going to spit out the same code. It won't save you any noticeable compile time. When distributing the file, it won't compress significantly better.
Eric Warmenhoven
I disagree, and I think you're attacking straw men (but I won't vote you down for that). The advantage of the comma operator and avoiding brace pairs is not a matter of code performance, source file compression, or anything like that. It's a matter of visual clarity and consistency. Some people like always including braces even if they're unnecessary, but if you don't like doing that, it can be awkwardly inconsistent in a string of `else if` clauses to have most of them be single statements without braces, and just one or two that need braces for 2-3 tightly coupled operations.
R..
I think my first point was directly at the point of visual clarity, and that the above comma style lacks it.
Eric Warmenhoven
Mmm - I'm not sure. Visually, the two equals signs really stand out for me. I don't think I'd mistakenly view that as a single assignment.
Blank Xavier
+3  A: 

The comma form is more useful for when you cannot use braces:

#define MY_ASSERT(expr) ((expr) || (debugbreak(), 0))

Here debugbreak() returns void, but we still wish to have 0 as an rvalue.

Matt Joiner
This example is probably an argument against the existence of the `void` return type, as opposed to an argument for the comma operator. I've gotten to the point where I make all functions return a value, even if it's just a dummy `int` with `return 0;`
R..
+1  A: 

The more a code reader has to look / check the language core details, the less that code is readable. In this case there are two distinct instructions, so the braces usage is, in my opinion, obvious.

What about

  a = b, c;

or

  a = b, c ? d, e : f, g;

Code readers (most) not used to that syntax, may want to check the , precedence to ensure of which value will be assigned.

We expect someone reading a source code to focus on the code logic, not its syntax.

ring0
In general I consider arguments that "you shouldn't do such-and-such because the person reading the code might not be familiar with the language" mostly invalid - especially when it's an operator precedence issue. If you don't know the language of the code you're reading/writing, you need to go back and learn it.
R..
@R..: in the second example, I'd veto any code review for anyone who presented that example for review. I don't care how well you know the language; it is obnoxious. And clarity of the code is more important than demonstrating one's knowledge of the arcane corners of the language. Was it Kernighan who asked "If you're as clever as you can be when you write the code, how on earth are you going to debug it?"
Jonathan Leffler
And in the first example, `a = b, c`, the variable `a` ends up with the value of `b`, *not* `c`... just in case :-)
ring0
@Jonathan: I agree the second one is ugly as written, but if the variables had reasonable names and the spacing were adjusted to make the meaning more clear, I wouldn't object to it.
R..
@R..: even if written with meaningful expressions, it is a nauseous piece of code. Unless I've goofed, it is the same as: `{ a = b; if (c) { d; e; } else { f; } g;`. That most certainly should not be compressed into a single line. (No offense to @ring0; he was deliberately using an extreme example for shock value - and makes the point admirably.)
Jonathan Leffler