proposed change to style(9): require yoda style if statements

Max Laier max at love2party.net
Tue May 11 20:33:28 UTC 2010


On Tuesday 11 May 2010 21:36:11 Eitan Adler wrote:
> My proposal is simple:
> require that any if statement that compares a constant to a mutable
>  variable be written as
> if (constant == variable)
> instead of
> if (variable == constant)
> 
> this prevents an extremely common programming error
> if (variable = constant)
> 
> While this is almost always found in testing sometimes thing can slip
> through and writing it using the former method will generate a compiler
> warning.

With proper -W flags, "if (var = const)" also yields a warning:

  warning: suggest parentheses around assignment used as truth value

and style already tells you to avoid unnecessary parentheses.

Chaning style(9) in such a fundamental way almost, always isn't a good idea.  
It's simply unrealistic to change all current code to comply and the 
difference between old and new code will just add to the confusion.

Best,
  Max


More information about the freebsd-hackers mailing list