Using a comma to prevent the need for a curly bracket

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

For example:

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

instead:

 if (something) { b = y; c = z--; } 
+2
c coding-style
source share
7 answers

This is a really smart way to use this syntax function of most C-like languages.

Personally, I try to stay as ambiguous as possible when I code, so I always include { and } in all my if . This can save time, but I prefer clarity: it does not speed up or slow down code execution.

+12
source share

I would vote against this for several reasons:

  • It’s hard to see right away just by looking at more than one task.
  • I am in favor of always inserting curly braces, because there are times when it is convenient to return, for example. during debugging, and add code that will also be executed in this block.
  • This does not save much. The compiler is about to spit out the same code. This will not save you any noticeable compilation time. When you distribute the file, it will not be significantly compressed.
+8
source share
  • This will not save more than a few minutes of input time.

  • If you really need to significantly save screen space, you need a larger screen (or you need to launch a larger terminal window).

  • It does not matter for the generated object code. Therefore, it does not affect runtime.

  • In the debugger, it is more difficult to follow the instructions of the expression component for the comma.

I think it’s easier to read colon-separated (and therefore “anchored”) code, without significant complex input for the formatted version. (Over time, when I encoded in C and used curly braces, I would have to think about not using a comma.)

+5
source share

I think this is a very good style, but I am sure that others will not agree.

One specific use case for the comma operator is inside parts of the for statement, as in:

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

+4
source share

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

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

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

+3
source share

The more the code reader has to look / check the kernel details, the less this code is read. In this case, there are two different instructions, so the use of braces, in my opinion, is obvious.

What about

  a = b, c; 

or

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

Encoders (most) not used for this syntax might want to check the priority to ensure what value will be assigned.

We expect someone to read the source code to focus on the logic of the code, not the syntax.

+2
source share

I have never used comma syntax. But this is because I did not know that it exists, to be honest.

If I knew about this, I would gladly use it instead of annoying braces for two or three statements.

So, in my opinion, use as you wish! Just as long as you don't make this classic mistake:

 if (cond) doSomething(); doSomethingElse(); // <-- oops, unconditional statement! 
+1
source share

All Articles