Timeline for Is this a case where 'else' is inevitable?
Current License: CC BY-SA 4.0
20 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Feb 10, 2022 at 2:45 | comment | added | jaskij | This particular case looks fine. But personally, I've always struggled to make the name of the function meaningful and correct while using booleans. If it wasn't a test, but just setting the lights on/off, would you have SetLights(true)? That's much less intuitive. | |
| Feb 9, 2022 at 16:11 | answer | added | Jon Raynor | timeline score: 0 | |
| Feb 8, 2022 at 23:56 | review | Close votes | |||
| Feb 13, 2022 at 3:05 | |||||
| Feb 8, 2022 at 22:20 | comment | added | Filip Milovanović | Assuming actual production code looks like this in terms of length and complexity and naming, this is the case where you shouldn't be bothered about if-else at all. You have well-named methods (except for the outer one - "LightTest") that together with the if-else express clearly the intent and high level behavior of the code. It's all at the same level of abstraction, and it's not a wall of text - it's just one or two lines. It's not the if-else in itself that pollutes code, it's what you do within it (expressivenes) and with it (does it cause external coupling). | |
| Feb 8, 2022 at 21:54 | history | became hot network question | |||
| Feb 8, 2022 at 19:02 | history | edited | Christophe | edited tags | |
| Feb 8, 2022 at 18:59 | comment | added | Christophe | Looking at the many constructive reactions, but also at the many readers of this question (more than 130 in a couple of hours), I think that you hit a point of great interest ! | |
| Feb 8, 2022 at 15:44 | comment | added | Bart Friederichs | @ErikEidt in the current code, they call it with constants. I do use it parametrised in my test code though. | |
| Feb 8, 2022 at 15:43 | comment | added | Bart Friederichs | Thanks all. I have to note I do not want to dogmatically follow this rule, but just have it in my head to trigger where I can improve (clean) my code. My question is exactly about when not to use it. Forgive me if I have given you the impression that I blindly follow this kind of rules (I tend to not). I strive for understanding them and sites like this help me in getting a better software engineer. | |
| Feb 8, 2022 at 15:39 | vote | accept | Bart Friederichs | ||
| Feb 8, 2022 at 15:38 | comment | added | Erik Eidt | It might be cleaner to have LightTestOn and LightTestOff. How do callers use this? Do they pass constants: LightTest(true); & LightTest(false); or do they pass a variable as the actual argument? | |
| Feb 8, 2022 at 15:18 | answer | added | Christophe | timeline score: 14 | |
| Feb 8, 2022 at 14:53 | comment | added | Flater | Maybe a better way to express @PhilipKendall's point: it would be more productive, instead of trying to figure out where the line is for this particular advice, to address why you're trying to dogmatically adhere to it in the first place. In the end, the only goal is "is this readable? Can it be better?", not "Was this else really necessary?". To that extent, the question you posted here focuses on the wrong approach. Even if we answer your direct question, it doesn't help with any and all other advice that you receive and dogmatically try to apply, yet the real answer is always the same. | |
| Feb 8, 2022 at 14:34 | comment | added | Bart Friederichs | @Christophe I added two references to questions about this on this site. | |
| Feb 8, 2022 at 14:33 | history | edited | Bart Friederichs | CC BY-SA 4.0 | added 241 characters in body |
| Feb 8, 2022 at 14:22 | comment | added | Christophe | Can you provide a reference to the book or blog that claims that else i to be avoided? | |
| Feb 8, 2022 at 14:04 | comment | added | Philip Kendall | You're asking the wrong question IMO. It shouldn't be "some guy in a book said else is not clean code, can I ignore the rule?", it should be "is this code easy to understand and maintain?" | |
| Feb 8, 2022 at 13:58 | comment | added | Bart Friederichs | @PhilipKendall hence my question, to find out where the borders of this rule are. To add some mindfulness if you want :-). | |
| Feb 8, 2022 at 13:56 | comment | added | Philip Kendall | "I want this code to be clean, so I try to keep the use of if/else to a minimum." If applied mindlessly, this is a silly rule. | |
| Feb 8, 2022 at 13:54 | history | asked | Bart Friederichs | CC BY-SA 4.0 |