News for hackers. Stuff that matters.
Re:Early exit is a tail call optimization of procedural lang... by SideburnsOfDoom on Monday December 14, 2015 @11:26AM (#10731927)
> A sufficiently complex method with 5 return statements ... is certainly less readable than the ... single return statement at the end version

In all cases? How can you even measure that? How can you do it in a way that's free of bias?

If you've never used a style, of course you will find it harder. Dogmatic assertions that boil down to "what I'm used to is objectively better because of how it feels to me" are IMHO harmful.

Re:Early exit is a tail call optimization of procedural lang... by glastra on Monday December 14, 2015 @11:37AM (#10732016)
I cannot measure that on anyone else other than myself. This is a place to express opinion.

I've read (and sadly written) enough code in both styles to know what _I_ (and all the peers I've bothered to ask with concrete examples, for that matter) find easier to understand, but there's of course all kinds of bias in there as well.

Taking comments in HN as fact or truth is harmful, and (replying to your other comment) waving acronyms around is equally harmful, if not more. You can't possibly assert that writing code in one or another way is easier or harder, or that the code is "more complicated" (precisely what we don't seem to agree on). To be honest, I find it easier to write methods with single return statements (and exceptions to handle... well, exceptions[^1]). It all seems clearer in a "stream of thought" kind of way. Engrish is hard, sorry.

[^1]: Early returns are exceptions for lazy coders.

Re:Early exit is a tail call optimization of procedural lang... by SideburnsOfDoom on Monday December 14, 2015 @11:45AM (#10732080)
> You can't possibly assert that writing code in one or another way is easier or harder

In general, of course not. I think that both single-return and multiple-return styles can be valid and readable. But of course both can be obfuscated.

But give me a particular piece of logic and I might give an opinion on which style I would use for it.

& if you think YAGNI is harmful, you should see the pointless code from people who have never thought that way!

Re:Early exit is a tail call optimization of procedural lang... by conceit on Monday December 14, 2015 @05:02PM (#10734280)
the term callback hell comes to mind, although I'm not sure that's the entire problem you quoted
Re:Early exit is a tail call optimization of procedural lang... by SideburnsOfDoom on Tuesday December 15, 2015 @04:13AM (#10736708)
I wasn't talking about callback hell (1) but if you want to do so - the point is that if the "nested callbacks" style is objectively worse (and this seems likely) then this can actually be measured.

Lines of code and cyclomatic complexity (2) is just the start.

You would have to do something like ... e.g. take a group people skilled in some other programming language, give them a basic grounding in Javascript, and have them work with some JS code samples with and without nested callbacks.

If there is clear difference in code comprehension, time to modify the code, lines of new code to accomplish a goal, and resulting bug count then you can have an informed opinion on if this code style is really objectively so bad.

That seems like a costly and time-consuming procedure. But if some academic institution did this I'd be pleased - some scarce data and some actual science is IMHO is worth a thousand opinions in this kind of recurring stylistic debate.

1) http://callbackhell.com/

2) https://en.wikipedia.org/wiki/Cyclomatic_complexity