News for hackers. Stuff that matters.
Re:What Makes Code Hard to Read: Visual Patterns of Complexi... by stared on Tuesday March 11, 2025 @10:36AM (#43332864)
My pet peeve:

    function getOddness4(n: number):
      if (n % 2 === 0):
        return "Even";
      return "Odd";
While it is shorter, I prefer vastly prefer this one:

    function getOddness2(n: number):
      if (n % 2 === 0):
        return "Even";
      else:
        return "Odd";
Reason: getOddness4 gives some sense of asymmetry, whereas "Even" and "Odd" are symmetric choices. getOddness2 is in that respect straightforward.
Re:What Makes Code Hard to Read: Visual Patterns of Complexi... by CatAtHeart on Tuesday March 11, 2025 @11:33AM (#43333493)
I personally prefer the former as you can visually see the return one level of indentation below function name. It shows a guaranteed result barring no early-exits. Something about having the return embedded lower just seems off to me.
Re:What Makes Code Hard to Read: Visual Patterns of Complexi... by cess11 on Tuesday March 11, 2025 @12:06PM (#43333868)
I would add a blank line to push 'return "Odd";' from the if, and also add brackets around the if-body if the language allows.

There are situations where I allow else, they tend to have side effects, but usually I refactor until I get rid of it because it'll come out clearer than it was. Commonly something rather convoluted turns into a sequence of guards where execution can bail ordered based on importance or execution cost. It isolates the actual function/method logic from the exit conditions.

Re:What Makes Code Hard to Read: Visual Patterns of Complexi... by __oh_es on Tuesday March 11, 2025 @12:36PM (#43334230)
I feel guard clauses/early returns end up shifting developer focus on narrowing the function operation, and not an originally happy path with some after thought about other conditions it could handle.

IME else’s also end up leading to further nesting and evaluating edge cases or variables beyond the immediate scope of happy path (carrying all that context!).

Re:What Makes Code Hard to Read: Visual Patterns of Complexi... by brulard on Tuesday March 11, 2025 @12:56PM (#43334528)
Nice example of how subjective this is. I immediately thought the first one without "else" is clearly the winner.

This is the problem with formatting rules. A codebase needs to have consistent style, even though that might mean nobody is fully happy with it.

I for example can not stand semicolons in JavaScript. It is just a visual clutter that is completely redundant, and yet some people really want it there.

Re:What Makes Code Hard to Read: Visual Patterns of Complexi... by bogomog on Tuesday March 11, 2025 @01:55PM (#43335218)

  function getOddness(n: number):
    return (n % 2 === 0)
      ? "Even"
      : "Odd";
Lowest boilerplate makes it the most readable. If working in a language with the ternary operator it ought to be easily recognized!
Re:What Makes Code Hard to Read: Visual Patterns of Complexi... by culopatin on Tuesday March 11, 2025 @02:18PM (#43335454)
While this is simple and all, the English words if/else don’t require the reader to know the ?: convention. Depending on what background the reader may have, they could think of the set notation where it could mean “all the evens such that odd is true” which makes no sense. Its also very close to a key:value set notation. If/else leave no doubts for the majority of readers. It’s more inclusive if you will.
Re:What Makes Code Hard to Read: Visual Patterns of Complexi... by bogomog on Tuesday March 11, 2025 @02:28PM (#43335554)
That's why I gave the caveat that if using a language with the ternary operator, one should know that operator. Python tried using English words for a ternary, but I think that's awkward from a readability perspective. A limited set of symbolic syntax improves readability over using words in my opinion, there's less text to scan.
Re:What Makes Code Hard to Read: Visual Patterns of Complexi... by erikerikson on Wednesday March 12, 2025 @12:41AM (#43340013)
Learning the basic operators of the language seems like table stakes.

Choosing one of world's spoken languages over others seems to be the opposite of inclusive.

Re:What Makes Code Hard to Read: Visual Patterns of Complexi... by stared on Tuesday March 11, 2025 @03:37PM (#43336222)
This is, IMHO, the idiomatic way to do so.
Re:What Makes Code Hard to Read: Visual Patterns of Complexi... by ajuc on Tuesday March 11, 2025 @02:52PM (#43335810)
I love code golf as much as anyone, not sure it's worth it on such small methods tho. Any of the propositions would be fine. Anyway:

    def oddness(n):
      return ["Even", "Odd"][n % 2]
BTW this trick with replacing if-then-else with a lookup is sometimes very useful. Especially if there's many ifs.
Re:What Makes Code Hard to Read: Visual Patterns of Complexi... by pasc1878 on Wednesday March 12, 2025 @05:54AM (#43341436)
Or your write in APL
Re:What Makes Code Hard to Read: Visual Patterns of Complexi... by elliottkember on Tuesday March 11, 2025 @07:42PM (#43338356)
Putting “return” on a different line from the actual value you’re returning?
Re:What Makes Code Hard to Read: Visual Patterns of Complexi... by Twisol on Tuesday March 11, 2025 @10:43PM (#43339435)
If there are two ways to say something, then people will find ways to make their choice of method into speech as well.

To me and my style of coding, there's a difference of intent between the two. A ternary connotes a mere computation, something that should have no side-effects. A conditional connotes a procedure; the arms of the conditional might be expected to have side-effects. (And the case of `if (_) return` or similar are pure control flow guards; they neither compute a value nor perform a procedure as such.)

It's not just about where the symbols go on the screen.

Re:What Makes Code Hard to Read: Visual Patterns of Complexi... by erikerikson on Wednesday March 12, 2025 @12:37AM (#43339997)
Since the article was in JavaScript:

  const getOddness = (n) => (
    n % 2
      ? 'Odd'
      : 'Even'
  )
Even less visual noise
Re:What Makes Code Hard to Read: Visual Patterns of Complexi... by lioeters on Wednesday March 12, 2025 @07:32AM (#43342117)

    const getOddness = n => n % 2 ? 'Odd' : 'Even'
Re:What Makes Code Hard to Read: Visual Patterns of Complexi... by zoogeny on Tuesday March 11, 2025 @05:21PM (#43337122)
90% of the time I prefer the first. I am allergic to indentation and I hate anything remotely like:

    function foo(a) {
        if (a) {
            return doThing()
        } else {
            return Error();
        }
    }
I like all of my assertion and predicate guards nicely at the top of a function:

    function foo(a) {
        if (!a) {
            return Error()
        } 

        return doThing()
    }
And for that reason, I would probably go for getOddness4 even though I see your point.
Re:What Makes Code Hard to Read: Visual Patterns of Complexi... by stared on Tuesday March 11, 2025 @08:07PM (#43338541)
In case of handling exceptions (and similar stuff), I also prefer avoiding unnecessary nesting.

For two (or more) equally valid, I prefer keeping same nesting.

Re:What Makes Code Hard to Read: Visual Patterns of Complexi... by makeitdouble on Tuesday March 11, 2025 @07:03PM (#43338061)
If it's this short, the ternary operator would be the absolute best option IMHO.

If any of the clauses are much longer, the first option reads a lot better if it can be a guard cause that returns very quick.

If neither options are short I'd argue they should be pushed away into scoped and named blocks (e.g. a function) and we're back to either a ternary operation or a guard like clause.

Re:What Makes Code Hard to Read: Visual Patterns of Complexi... by BlackFly on Wednesday March 12, 2025 @09:10AM (#43342848)
The asymmetry is apparent if the code gets refactored to continuation/callback style or mutation of a more complex data structure, the first method will fall through and execute the second instruction set. Return is a special operator in this sense in that it breaks control flow and the ordinary control flow of the first method is not capturing the exhaustiveness of the two cases.

In idiomatic rust, return isn't used except for exceptional cases that break the control flow of the method and the second example is more commonly seen without return statements at all. Idiomatic python also typically early exits in the beginning with a return on invalid parameters or state with a tail position return being the usual actual return value. Because of these conventional practices, breaking the exhaustive if-else control structure makes the indented return appear exceptional (like an invalidity). If you follow these conventions than naturally the return statement begins to appear redundant except in the break-control-flow cases and the choice of the rust convention begins to make sense: in all languages return is a statement equivalent to break.