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

Aaron Ballman aaron at aaronballman.com
Wed Oct 4 15:06:18 UTC 2017


On Wed, Oct 4, 2017 at 10:56 AM, Martin Sebor <msebor at gmail.com> wrote:
> 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.)

In addition to that compelling example, I've seen quite a few for
loops that use the comma operator when incrementing. e.g.,

for (int i = 0, j = 0; ; ++i, ++j) /* ... */

~Aaron

>
> With that I recommend against adopting this rule.
>
> Martin
>
> _______________________________________________
> C-safe-secure-studygroup mailing list
> C-safe-secure-studygroup at lists.trustable.io
> https://lists.trustable.io/cgi-bin/mailman/listinfo/c-safe-secure-studygroup



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