Earlier quoted context omitted.
In last 20+ years I've changed the coding style many times, usually to fit the current team style. And in my experience it takes a few weeks for the brain to rewire to the new style. First you hate it, and then you get used to it, and then you're like: wow it's great. Then you switch the standards for a new project and again, the same steps. It's just a matter of habit...
Pragmatic programming has lost. If you aren't a zealot about being pedantic, just move into the old-folks home.
Avoid Else, Return Early (2013)
381–390 of 601 posts
Re: Avoid Else, Return Early (2013)
#382Re: Avoid Else, Return Early (2013)
#383Good advice except for the last example which is brittle and unnecessary. If handleError() is ever changed this method could start returning values unexpectedly. Totally unnecessary and not a worthwhile "optimization" to save lines of code.
function(err, results) {
if (err) // …
}
It is assumed that this function will be executed asynchronously, and thus it's not possible for anything to consume the return value anyway.If you want to control the return value, simply return on a new line or use the void operator:
return void handleError(err)Re: Avoid Else, Return Early (2013)
#384Removing the braces and returning the unneeded non-result of handleError to save a couple LOC is penny wise pound foolish. Is this a common JS idiom?
Re: Avoid Else, Return Early (2013)
#385In Ruby these are idiomatic enough to have a name and are built into the linter, so it'll yell at you if it sees that you're not using guard clauses. As a further refinement, I will often take tricky conditional logic and put them into a method that consists of nothing but guard clauses and a return true or false at the bottom of the method. In Ruby you can use ? and ! at the end of method names, so I'll give the met…
Re: Avoid Else, Return Early (2013)
#386> We also generally don’t care about return values in JS I don't find that to be the case, though JS isn't my main language.
Re: Avoid Else, Return Early (2013)
#387Earlier quoted context omitted.
The irony is that Python actually has open-braces, but they're spelled ":" instead of "{". And the syntax effectively enforces K&R style. When I write Python I end every block with a "pass" statement so that emacs can auto-indent my code properly. The "pass" statement thus effectively becomes a close-brace. It drives Pythonistas into conniptions, but I never have to worry about reverse-engineering a block of code to…
Python specific IDEs will smartly indent code upon copy and paste.
Re: Avoid Else, Return Early (2013)
#388Earlier quoted context omitted.
In last 20+ years I've changed the coding style many times, usually to fit the current team style. And in my experience it takes a few weeks for the brain to rewire to the new style. First you hate it, and then you get used to it, and then you're like: wow it's great. Then you switch the standards for a new project and again, the same steps. It's just a matter of habit...
Pragmatic programming has lost. If you aren't a zealot about being pedantic, just move into the old-folks home.
At the end of the day you can do whatever you want with the code you're writing, and some pre-commit hook can run go fmt/prettier/whatever and then it'll all get standardized.
I've become a very heavy proponent of code formatters (I added prettier to the entire team) and I truly believe that talk about code formatting will never really die off, but at the end of the day you can do whatever you want on your side but still have a standardized looking codebase, and that feels extremely liberating to me.
As a PS: If you are a diehard tab proponent/different bracket placement/whatever but your team formatter configuration uses spaces instead then you can also use code formatters to do spaces -> tabs locally, say, on file open. Once you commit your file your precommit falls back to the team configuration, which removes your tabs. Everyone is happy.
Re: Avoid Else, Return Early (2013)
#389Earlier quoted context omitted.
Totally disagree with you.... it's funny though that I read LOC as "level of complexity" not "lines of code". I've been writing code for 30 years and I think it's jarring when the braces are on the next line, so much easier for me to parse that when it's on the same line. But everyone is entitled to their own opinion.
In last 20+ years I've changed the coding style many times, usually to fit the current team style. And in my experience it takes a few weeks for the brain to rewire to the new style. First you hate it, and then you get used to it, and then you're like: wow it's great. Then you switch the standards for a new project and again, the same steps. It's just a matter of habit...
Re: Avoid Else, Return Early (2013)
#390I think there's a lot of value to returning early out of functions when you hit an error condition. There's not much reason to go further if you're just completely hosed. (e.g. malformed arguments). However, I don't think that coders should shy away from built-up result values for return. It makes the code more friendly to things like copying (I can feel the boos and hisses now), block restructuring, #ifdef'ing (or s…
Guard clauses aren't only for errors. They do however usually involve certain kinds of asymmetry. Typically: The early return case does fewer things (or nothing) than the major case. Or the early return case is normal, but has a 'negative' sense (which again does less work) like not finding a key in a dictionary.
I think the statement (ish) “don’t use if/else, use return” deserves some qualification, and one natural place to use it is for errors.
When people start dropping returns all over massive multi-screen branching and looping structures, readability may suffer, and debugability quite often takes a big hit.
In general, I like to use guard clauses for any big at-entry branch asymmetries. If they’re farther down the logic tree and/or unwieldy, it may be time to call some more functions.