Timeline for Developer insists if statements shouldn't have negated conditions, and should always have an else block
Current License: CC BY-SA 3.0
11 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Oct 5, 2017 at 22:21 | comment | added | radarbob | So... you could say that double negative is a no-no? Well, no, not always; but yes, it is not encouraged by most, but apparently it is true, and I did not know this, that not putting anything in an if block is a good thing. So ... yes. | |
| Jun 13, 2017 at 14:48 | comment | added | Peter M | With regards to your door example, the two example names are not necessarily equivalent. In the physical world doorNotOpen is not the same as doorClosed as there is a transitional state between being open and closed. I see this all the time in my industrial applications were boolean states are determined by physical sensors. If you have a single sensor on open side of the door then you can only say the door is open or not open. Likewise if the sensor is on the closed side, you can only say closed or not closed. Also the sensor itself determines how the logic state is affirmed. | |
| Jun 13, 2017 at 10:10 | comment | added | crizzis | I'd argue having an else would be much clearer in the first example. Why should I have to read the contents of the if block to see whether it falls through or not? Having an explicit else clearly indicates there are two ways of handling things, returning or not. An implicit else leaves room for confusion. | |
| Jun 9, 2017 at 21:16 | comment | added | David Schwartz | Clearly, if (!doorNotOpen) is awful. So names should be as positive as possible. if (! doorClosed) is perfectly fine. | |
| Jun 8, 2017 at 21:03 | comment | added | Barmar | Regarding taking longer to type, he might use a template in his editor/IDE to create the if/else block in one keystroke. | |
| S Jun 8, 2017 at 20:26 | history | suggested | Deduplicator | CC BY-SA 3.0 | added syntax-highlighting |
| Jun 8, 2017 at 19:02 | comment | added | Bernhard Barker | I feel like not many people would agree about how if-else with returns should be handled, and some might even say it shouldn't be handled the same in every scenario. I don't have a strong preference myself, but I can definitely see the argument behind many other ways of doing it. | |
| Jun 8, 2017 at 18:21 | review | Suggested edits | |||
| S Jun 8, 2017 at 20:26 | |||||
| Jun 8, 2017 at 15:24 | comment | added | David Arno | @Patsuan, very clever. I like that. :) | |
| Jun 8, 2017 at 15:20 | comment | added | Patsuan | So... you could say that double negative is a no-no? ;) | |
| Jun 8, 2017 at 13:54 | history | answered | David Arno | CC BY-SA 3.0 |