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

Xin LI delphij at delphij.net
Thu May 13 23:16:41 UTC 2010


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 2010/05/13 14:26, Garance A Drosehn wrote:
> At 10:36 PM +0300 5/11/10, 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)
> 
> 
> I did this for awhile in my own programming, long enough ago that
> there was no cool "yoda" name connected to it.  But I found that in
> some situations it makes the code harder to read, so I don't do it
> as much any more.  I don't mind if people do it, but I do not think
> it should be an official recommendation in style(9).  Or to say it
> another way, I'd be annoyed if an otherwise-correct patch was asked
> to be rewritten just because the developer used (variable == constant)
> instead of (constant == variable).

I used to use this style of programming a lot but gave up ~7 years ago
when I found that some C++ compiler can be confused in very ugly manner
(due to type mismatch), not to mention that using this style sometimes
makes the code harder to read.

It turns out that modern compilers are more sensitive about this type of
mistakes, and, more importantly, making code more readable would usually
save debugging time.

Cheers,
- -- 
Xin LI <delphij at delphij.net>	http://www.delphij.net/
FreeBSD - The Power to Serve!	       Live free or die
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.14 (FreeBSD)

iQEcBAEBAgAGBQJL7Ig/AAoJEATO+BI/yjfBiQwH/R6nlA5DciXXHfl1XDxDMe81
fnmOCgCAbidKySiwlmKJhsfLuZgKQX/jVqO2Z28X+0cOQVqLw2bZ9hlfyZ306uGy
vwYDXzs+E805T+S9UArKe7jJdoeuQajJ7w+Z0eJfCjFBgb9KCW/KTIaPaFJJtvbS
azVB0q7tuZGd2xPj6iNmuxteeN1B0aixYNov5cGtSs3anH9rwFPFqJAntN8nRWTm
0W9ocOgi/cNYfNMuapvxSsRv2IJiAe+EpikpRhDJT2ushFnQ1ML4a2Mgld7sz+jn
YvYHUi5LAnyl7oIS5hxF7d7ADINmPuKnoBNbgiYoMLpNjsE1thAsXPFv8SWI3qs=
=ngfR
-----END PGP SIGNATURE-----


More information about the freebsd-hackers mailing list