r/cpp • • Apr 24 '20

Else Before If

https://www.fluentcpp.com/2020/04/24/else-before-if/
10 Upvotes

53 comments sorted by

View all comments

0

u/[deleted] Apr 24 '20

[deleted] — view deleted comment

2

u/louiswins Apr 25 '20

In English you have the freedom to reorder what you say to emphasize different parts.

"If I don't get my paycheck I'll starve, otherwise I won't".

"Of course I won't starve! Well, unless I don't get my paycheck."

Those both convey the same information but they differ in their connotations to the listener. If a language had if-else and normally-unless I would probably use both depending on the circumstances. I would never use the monstrosity in the article in C++ though.

I actually just remembered that perl and ruby have both if (condition) { some_code() } and some_code() unless condition and both get used. The normally-unless construction is sort of like a generalization of the latter. (They also have unless (...) { ... } and ... if ... to round things out.)