5 ms·
Ditch That Else
- noobermin 3y agoI learned early on to avoid nesting conditionals. I assumed this was standard practice.
- sirwhinesalot 3y agoUniversities teach the very opposite, unfortunately...
- dieselgate 3y agoFunny, before reading the article was thinking about the eslint rule - believe it's called the "no-else-return". While reading and before finishing the article was thinking "isn't that the reason why we use guard clauses?". It can be confusing but I really appreciate one-liner `unless` statements in ruby for guards edit: not a fan of unless/else though
- mvdtnz 3y ago> It can be confusing but I really appreciate one-liner `unless` statements in ruby for guards Gosh I am working on beating that out of my Ruby folks. Sure I came into the team from other languages and they're all Ruby natives, and sure this is accepted practice in the Ruby community. But it's just awful and I won't accept it.
- tremon 3y agoWhat? You've inherited a functioning team with a certain code culture, and you're establishing your dominance by making them conform to your "superior" standards? Are they still writing Ruby or will you make them switch to a different language as well?
- zogrodea 3y agoI agree. It might be hard to adjust one's practices, like when one first starts living in another community with a different culture, but it's the right thing to do in most cases.
- mvdtnz 3y ago"Functioning" is arguable. The codebase is a mess.
- zoky 3y ago…said every newly installed manager ever.
- mvdtnz 3y agoI've been there 3 years.
- matthewmacleod 3y agoSounds like the regular beatings haven’t improved morale.
- zoky 3y agoAnd your codebase is still a mess you say? Huh. Maybe try adding some `unless` statements, I've heard they really help.
- mvdtnz 3y agoIt's a 14 year old code base mate. Have you ever worked on very large codebases?
- dragonwriter 3y agoAnd you are still fighting basic Ruby language idioms from the perspective of a newcomer rather than dealing with actual problems?
- zoky 3y agoDo you make them replace `each` iterators with `for` loops as well, just because that’s “how it’s done” in your preferred language? The “fail early” principle is a widely accepted pattern, and that’s exactly what the `unless` statement facilitates, while communicating that is exactly what it’s doing.
- runarberg 3y agono-else-return is correct https://eslint.org/docs/latest/rules/no-else-return https://eslint.org/docs/latest/rules/no-else-return This rule is actually what got me into the practice of returning early from the edge case so many years back. I think it was enabled by default in the—then very popular—AirBnB coding standard.
- mvdtnz 3y agoI remember in my early days of programming one of the most errant pieces of advice I received was that every method should have one and only one return statement. Before I was told that, I had always cleanly handled edge cases at the top of the method like this article suggests, then handled the bulk of the logic. I was young and impressionable so when a senior developer advised me of this nugget of "best practice" I spent the next couple of years writing awful code, jumping through hoops to mangle the logic in my methods such that they would only ever have a single return statement at the end. Thank goodness I eventually awoke from that terrible practice. I still occasionally hear people espouse this, but not too often nowadays.
- eyelidlessness 3y agoI had a similar early experience. Amusingly, what broke me out of it was coming back to imperative languages after a good stint with Clojure. My initial instinct, coming back to an imperative language, was to try to return a single expression. You can do that, but it makes for rather unidiomatic code. Hewing more towards the idiom while trying to force functions towards a single return resulted in… a lot of statements, producing a lot of intermediate state. And it all gets much harder to follow. I’ve since embraced early edge cases and early returns, largely because it allows more idiomatic use of a functional style in an imperative context. And all of that makes the code a great deal easier to understand than it would be otherwise.
- nneonneo 3y agoI think the "errant advice" might originate with languages like C, in which an early return is an easy way to forget crucial deinitialization code, and where "goto"s can always be used to skip to the single exit point where all the deinitialization code is. In more sane languages, deinitialization happens automatically when leaving the stack frame, so following a "one return per function" rule is not necessary.
- clnq 3y agoOh yeah, I got that advice, too, in the early 00s. I thought it was quite strange then.
- 3y ago
- michaelteter 3y agoI sincerely hope that most people are not dealing with functions of that length, such that error handling is lost at the bottom of hundreds of lines of code. Whether you do return-early or nested if-else, there's little reason for having such lengthy functions. Dealing with THAT should be the priority, not the nesting vs return-early.
- eyelidlessness 3y agoYour logic has to flow somewhere. I prefer shorter functions too, but sometimes the actual “unit” is just not so concise without breaking it up in confusing ways. Handling edge cases first makes that much more digestible, IME.
- runarberg 3y agoForcefully breaking up your logic in places where it doesn’t make sense is an antipattern IMO, and makes it very easy to write spaghetti. I’ve inherited codebases with this overemphasis on very short functions and what I saw were very specific utility functions in dozens of different modules, all over the codebase, often with funky names, surrounded by dead code, and never a clear path to and from it. Sometimes a single function that spans even a couple of hundred lines, is preferable.
- jquast 3y agoI was frustrated with a memory leak in someone else’s code of a dozen or more if-if-if-if-else-else-else statements like the second example, it was probably 400 or more columns wide. It was my first real job and I began to doubt my abilities, I couldn’t follow the logic. I refactored it all as the author suggests, out of desperation to better understand it in a debugger if more code could fit on a page. To my astonishment the memory leak disappeared, so it has always since been a habit, sometimes I still rewrite code this way just to better understand it, I think it looks better.
- nickm12 3y agoThis is my preferred style by far, but it seems like most junior programmers I work with don't seem to mind the style with deeply nested ifs and far away exceptional elses. I try to avoid commenting on such stuff in code reviews unless it is egregious, but I will rework it later if I happen to be in the same code. I hope it doesn't come across as passive-aggressive on my part.
- dsauerbrun 3y agoyou should mention it. When I was a junior programmer I also wrote the nested ifs and I had a mentor show me how much cleaner the code could look which I really appreciated. I continue to use this style which is far superior than the one I started with.
- bluefirebrand 3y agoIt probably comes across as passive aggressive It's also robbing them of a learning opportunity, and it's creating more work for you. Mention it on the PR, maybe show an example of how you would write it in a code snippet, and re-review after they've taken a stab at it.
- nneonneo 3y agoEarly returns are very useful, just like break/continue. For example, I find it much easier to write for filename in os.listdir(dirname): if not filename.endswith(".png") and not filename.endswith(".jpg"): continue # process a png or jpg image than to try and have a nested `if filename.endswith(".png") or filename.endswith(".jpg")`, particularly as it drops one nesting level and is easier to read. Unfortunately, Python lacks targeted break/continue so you can't break out of two nested loops without some contortions; in such cases, I find it useful to refactor code into smaller functions just to be able to use early return from a nested context.
- Already__Taken 3y agothe early return solution to this would be to filter os.listdir before the loop
- nextaccountic 3y agoRust embraces this philosophy with the ? operator (when you just want to early return an error) or the let else construct (for more involved short-circuiting). If you type the data you're dealing with properly, this is probably enough for the majority of cases and the resulting code is super clean.
- tripdout 3y agoThis is good advice - I wish there was another article talking about when your error handling isn't as simple as just hitting one path of an if-else. But I guess that would be a specific article on error handling.
- countWSS 3y agoIt does looks simpler but unlocks several footguns: order of edge cases,edge cases deeply nested within other condition with different context, early return that doesn't reset some mutex/setting/return condition. Before blindly rewriting, have a mental picture of what is context/state at the point of early return, which at each time has to achieve the goals of function, setting background states/data points/return pointers/etc. Its easy only in simple functions.
- sirwhinesalot 3y agoAny good modern language has ways of ensuring that resources are properly released on early returns: RAII, using/with, defer, etc.
- say_it_as_it_is 3y agoYou put the condition that is most likely to be met first, not the least likely.
- nsonha 3y agoLol why? You are coding definitive rules not heuristics. The odd of the branch getting hit has no bearing here. When the edge cases get bloated, it’s time to make a validator function for your input and leave the main function with happy path only
- say_it_as_it_is 3y agoLoL! languages that support short-circuit evaluation will exit the conditional evaluation as soon as they reach a condition that is true, without evaluating subsequent conditions ... it also makes branch predictions more accurate
- LorenPechtel 3y agoThis is why having your language support nested procedures is so helpful. Having it completely separate when it's only ever going to be used from one place isn't exactly the clearest and means extra variable passing. Pulling it into a nested procedure, though, makes it very clear what's going on and much easier to follow. My general rule is no flow control other than bailout in anything more than a dozen lines or so.
- rekabis 3y agoThis is also being called a “never-nester”. As in, you never nest your own code more than three levels deep. There is one very well-made YouTube video on it, IIRC.
- silok 3y agoThe guard statement in swift is pretty much designed for this pattern, and it works well imo.
- IYasha 3y agoSometimes someone HAS to take the role of Captain Obvious and write articles like this :)