[C-safe-secure-studygroup] Rule 12.3 review

Martin Sebor msebor at gmail.com
Wed Oct 4 14:56:31 UTC 2017


Rule 12.3 - The comma operator should not be used

This Advisory rule prohibits the use the comma expression with
no exceptions.

The stated Rationale is that it's bad for readability and that
the same effect can usually be achieved by other means.

I'm not aware of any serious readability problems with the operator
and while I agree that the same effect can usually be achieved by
using other constructs, there are situations when there is no
alternative.  A common example is a macro that needs to expand into
sequenced expressions and yield a value, analogously to a function
call.  E.g.,

   // log and evaluate expression X
   #define LOG(x)   (log (__FILE__, __LINE__, __func__, #x), (x))

There's no comparable way to achieve the same effect.  (Wrapping
it in a do { } while (0) loop turns it into a statement, making
it unusable in contexts where an expression is required.  Using
an inline function is unsuitable when the expression can have
an arbitrary type.)

With that I recommend against adopting this rule.

Martin



More information about the C-safe-secure-studygroup mailing list