Live data from Hacker News

Avoid Else, Return Early (2013)

blog.timoxley.com

401–410 of 601 posts

Re: Avoid Else, Return Early (2013)

#401

Earlier quoted context omitted.

Not being a pest here, but how often are you comparing more than 2 functions at a time? I can't say that I have ever dealt with code in over 20 years where I had to deal with more than 2 functions at once. Maybe I am mentally disabled, but I can only focus on one logical task at a time. Can other programmers multi-task their coding?

Refactoring might have you adding a parameter to a function, and then feeding that additional parameter to the function at it's call sites. Even when you only have one call site and thus are in the "2 functions" case - these two functions are often separated by multiple "irrelevant" functions. The less scrolling, tab management, and other context switching I have to do to look at those "2" functions, the easier it is…

>The less scrolling, tab management, and other context switching I have to do to look at those "2" functions, the easier it is on my memory.

I don't disagree, but the sentiment seems to be "fit everything on the screen at all times", which seems like one of those rules that may take more time to implement than it saves in actual time.

I work on projects with upwards of 100k lines of code consistently, the very idea that anything relevant will fit on one screen is absurd, so I am always curious what kinds of projects people work on where the relevant code is literally within a half screen of each other.

Re: Avoid Else, Return Early (2013)

#402

Earlier quoted context omitted.

It's not "religion" if there's a technical reason to choose one method over another. A later comment on the SO post described how K&R can have maintenance costs because when moving things around it's more difficult to tell where a statement ends, and can accidentally cause side effects.

Not reflected in the bug statistics, according to the post. So if the effects exists, its negligible.

>So if the effects exists, its negligible.

The only thing being considered was "bugs in the final code", not "time and effort in maintenance."

Re: Avoid Else, Return Early (2013)

#403

Earlier quoted context omitted.

Also functional languages with function guards e.g. while Erlang/Elixir does not have a return statement (and thus early return) you can write handle(Err, Results) when Err /= undefined -> handle_error(Err); handle(_, Results) -> % etc… . though errors would generally be reified as the ad-hoc union of two tuples and would look more like this: handle({error, Info}) -> handle_error(Info); handle({ok, Results}) -> % han…

The functional style that you show certainly has multiple points where a value is returned, so you could call it "early return"; even if as a syntactic shorthand the keyword "return" is omitted. This is common across a lot of functional languages.

> The functional style that you show certainly has multiple points where a value is returned, so you could call it "early return";

It's not "early return" any more than:

    function(err, results) {
      if (!err) {
         // handle results
      } else {
         return handleError(err)
      }
    }

Re: Avoid Else, Return Early (2013)

#404

Programmers with lots of hours of maintaining code eventually evolve to return early, sorting exit conditions at top and meat of the methods at the bottom. Same way you evolve out of one liners. Same way comments are extra weight that should only be in public or algorithm/need to know areas. Same way braces go on the end of the method/class name to reduce LOC. Same way you move on from heavy OO to dicts/lists. Same w…

> Same way comments are extra weight that should only be in public or algorithm/need to know areas. Unless you are writing completely brilliant code your future self will hate you if you skimp on comments. Also self-documenting code is great, but intentions are not always clear to another person trying to figure out your code.

My rule of thumb is, if I have to ask a question during a code review, the answer needs to be in a code comment.

Re: Avoid Else, Return Early (2013)

#405

Earlier quoted context omitted.

I am on board with returning at the beginning while checking arguments. But I hate it when code returns somewhere in the middle for some reason. I have spent countless hours debugging a problem with unexpected behavior only to find a return statement in the middle of a huge block to code.

That's because the return statement is a kind of goto statement. Goto-like statements reduce the value of structured programming. In a pure structured program, each statement sequence is either completely executed or not executed. There are also pure structured languages, like Oberon, which doesn't allow impurely structured programs.

When return is used in the middle of a complex code sequence I much prefer a goto so at least I know where all paths exit.

Re: Avoid Else, Return Early (2013)

#406
post #388

Earlier quoted context omitted.

Pragmatic programming has lost. If you aren't a zealot about being pedantic, just move into the old-folks home.

Not at all, I'd argue this whole Tabs/Spaces, bracket placement, single/double quote discussion should be getting killed by code formatters. 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…

I think the person you are responding to was being sarcastic...

Re: Avoid Else, Return Early (2013)

#407
post #6

A more formal name for this approach is / could be Guard Clauses: https://refactoring.com/catalog/replaceNestedConditionalWith... . This pattern has been elevated to a language construct in Swift: https://thatthinginswift.com/guard-statement-swift/

I was more or less required to use guard clauses by my manager at my previous job. I used to hate being forced to use them, just because as a recent computer science graduate I was happy with nested conditionals everywhere. But guard clauses make life so much easier, I eventually discovered. They make testing your code a lot easier, and also make reasoning about the code and reading it so much easier. Ruby makes writ…

It makes logging easier

Re: Avoid Else, Return Early (2013)

#408
post #156
post #95

Earlier quoted context omitted.

> Same way you get joy deleting large swaths of code. This is the true sign of a programmer's transcendence. Specifically the irrational joy of seeing net negative LOC diffs. It's not about how much you can add. It's about how much you can remove without sacrificing correctness, functionality, and readability.

"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away."

This is my favorite research quote from a Civilization game (Civ IV, narrated by Leonard Nimoy). I use this as my mantra for all design-oriented aspects of my life now. Or... I try to. Sometimes it's hard not to want to add more haha.

Re: Avoid Else, Return Early (2013)

#409

Earlier quoted context omitted.

JavaScript has the guard operator. It can really clean up your control flow when you know it's a guard operator and not just logical AND.

Logical AND in JavaScript can be used as a guard, by why in the world would you think of it as anything other than a logical AND? If you start thinking it is a "guard operator", I would think that going down the wrong path, which might promote misunderstanding of the behavior.

> which might promote misunderstanding of the behavior

This isn't a misunderstanding, binary logical operators in JS short-circuit like this by design. I believe && and || returned a boolean value in the past, but were explicitly changed to support this behaviour.

Re: Avoid Else, Return Early (2013)

#410
I'm down-voting all arguing about brace style. Whenever I see discussions about brace style, I just roll my eyes.

The sign of someone who doesn't value their time is someone who argues about brace style.

Post reply on HN