1

i actually just started learning C++ and i was used to writing (var==0) like this in C but my C++ sir told me 0==var is more better way when i asked him why he told me you find out yourself so igoogled this thing but didnt find any info regarding this so im here to ask it anyone can help?

  • depends what is `var` i think – Alberto Sinigaglia Jul 31 '20 at 12:07
  • 3
    Some people refer to this as a ["yoda condition"](https://en.wikipedia.org/wiki/Yoda_conditions), which is why it might feel unnatural. It's to discourage accidentally writing an assignment expression instead. A proper linter should warn you anyway if you accidentally put an assignment in a conditional expression, so really this is more up to personal preference than it is a best practice. – Patrick Roberts Jul 31 '20 at 12:09
  • 2
    Adequate compilers, with adequate options, make it absolutely 100% equivalent. `if (0 == var)` looks awkward IMHO – pmg Jul 31 '20 at 12:16
  • @PatrickRoberts *A proper linter should warn you anyway if you accidentally put an assignment in a conditional expression* And that's why putting any assignments at all into a conditional expression is always **BAD**. Because when you do it accidentally, the one warning among several hundred or more deliberate uses will be missed and you will have created an almost-impossible-to-find bug. Writing bug-free code is hard enough. I have no idea why way too many C programmers insist on using a style that makes creating hard-to-find bugs easier to do. "Brevity of code" is cargo-cult BS here – Andrew Henle Jul 31 '20 at 12:18
  • 2
    For better readability you should use the following: ***Left-hand side:** The expression “being interrogated,” whose value is more in flux. **Right-hand side:** The expression being compared against, whose value is more constant. This guideline matches English usage—it’s pretty natural to say, “if you make at least $100K/year” or “if you are at least 18 years old.” It’s unnatural to say, “if 18 years is less than or equal to your age.”* - [The Art of Readable Code (Theory in Practice)](https://isbnsearch.org/isbn/9780596802295) – 3limin4t0r Jul 31 '20 at 12:23
  • 1
    @AndrewHenle At least with gcc, the warning can be silenced by using an extra set of parenthesis, i.e. `if ((var = 0))`, so that way you're not left with any warnings you "know" are fine that you have to filter out. – dbush Jul 31 '20 at 12:26
  • @AndrewHenle depending on situation it may well be utterly essential to put the comparison in the expression. Memory limited applications for example where creating a temporary boolean to contain the result of the comparison is impossible. And of course there's always the idea that being more concise is a great way to reduce potential bugs, and that includes cutting out such temporary placeholder variables in many situations. – jwenting Jul 31 '20 at 12:42
  • @jwenting *being more concise is a great way to reduce potential bugs* Human minds can accurately keep track of only 3-5 things at once, so cramming too much into a single line of code is **not** going to reduce bugs. Doing an assignment in a conditional expression uses up one of those precious items in the human mind. Most of the time completely needlessly. *cutting out such temporary placeholder variables* That's a totally nonsensical statement in the context of doing assignments in a conditional expression. You can't do an assignment **without** a variable. – Andrew Henle Jul 31 '20 at 14:47
  • @jwenting At least you're thinking about code styles with the purpose of reducing bugs in mind. I wonder how many readers of this have never even thought about how their personally-preferred style either contributes to or reduces the creation of bugs. – Andrew Henle Jul 31 '20 at 14:51
  • I wouldn’t prefer either. I'd use `!var` – M. Nejat Aydin Jul 31 '20 at 19:12

0 Answers0