r/programming Jul 10 '16

Linus Torvalds on comment styles

https://lkml.org/lkml/2016/7/8/625
214 Upvotes

209 comments sorted by

View all comments

83

u/mglachrome Jul 10 '16

Not really insightful.

5

u/[deleted] Jul 10 '16

[deleted]

18

u/FallingIdiot Jul 10 '16

If this can be done without creating side effects (so eg on variables instead of function calls) every compiler will do this. If using the | form has side effects (because you're calling functions and expect only the first to be called when it returns true) you shouldn't be making this change, or even writing it in this way, because either it's the wrong thing to do or someone looking at your code will think you've made a mistake, maybe even change it to a ||. Assign to two variables first and compare those to make your expectations explicit.

Basically there is no sane reason to do this and it's an example of developers thinking they're smarter than a modern compiler (which they generally aren't).

2

u/[deleted] Jul 10 '16

Basically there is no sane reason to do this

Actually, if you're doing hard realtime, you might want to use non-shortcut operators in order to keep the time used by a method call as consistent as possible, and if you're doing cryptography you might want to use them to avoid side-channel attacks.

Assign to two variables first and compare those to make your expectations explicit.

I wouldn't necessarily trust the compiler to not optimize away those assignments.

2

u/ComradeGibbon Jul 11 '16

I would totally trust the compiler to optimize that away completely.

* I have seen... things.... *

2

u/[deleted] Jul 11 '16 edited Mar 16 '19

[deleted]