Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Someone somewhere around here also ranted about a block of code with multiple return points being hard to understand and debug.
...
Just avoid code smell. You can't hide it if it's there.
And avoid using ellipsis as often as I do. -
@metrobug Defensive programming means assert your assumptions at the outset. While that's one application of this idea, it's not the only one. The core idea is just shaking your logic tree out so that it requires less indentation, if you can do so cleanly.
-
Ederbit7387yNot really sure if you mean use early exits or whatever theyre called or to seperate logic or validation in seperate methods.
Either way, I try to avoid multiple returns spread across a method. It makes it harder to read for me. -
Ederbit7387y@aaxa I precisely mean what @AndSoWeCode said. But I didn't mean to leave out defensive programming. :)
-
Brolls31557yI don’t mind early exit or multiple return, in fact in a language like F# multiple return is largely a given.
I think it’s possible to get it horribly wrong and go over the top though.
I do however always try to invert conditionals if it means I can avoid nesting.
Listen. Use invariants. If you can do
if(!x) {
return foo;
}
...rest of function logic...
Instead of
if(x) {
...some long branch with more tests...
} else {
return foo;
}
Please do. It's so much easier to read when all of your conditions are tested in a line at the top instead of nested 8 layers deep in if-else blocks.
Thanks
rant