←back to thread

1534 points nromiun | 1 comments | | HN request time: 0s | source
Show context
defanor ◴[] No.45081057[source]
I think most programmers agree that simpler solutions (generally matching "lower cognitive load") are preferred, but the disagreements start about which ones are simpler: often a lower cognitive load comes with approaches one is more used to, or familiar with; when the mental models one has match those in the code.

For instance, the article itself suggests to use early/premature returns, while they are sometimes compared to "goto", making the control flow less obvious/predictable (as paxcoder mentioned here). Intermediate variables, just as small functions, can easily complicate reading of the code (in the example from the article, one would have to look up what "isSecure" means, while "(condition4 && !condition5)" would have shown it at once, and an "is secure" comment could be used to assist skimming). As for HTTP codes, those are standardized and not dependent on the content, unlike custom JSON codes: most developers working with HTTP would recognize those without additional documentation. And it goes on and on: people view different things as good practices and being simpler, depending (at least in part) on their backgrounds. If one considers simplicity, perhaps it is best to also consider it as subjective, taking into account to whom it is supposed to look simple. I think sometimes we try to view "simple" as something more objective than "easy", but unless it is actually measured with something like Kolmogorov complexity, the objectivity does not seem to be there.

replies(9): >>45081450 #>>45081668 #>>45082133 #>>45082314 #>>45082455 #>>45082707 #>>45082777 #>>45082899 #>>45083671 #
whilenot-dev ◴[] No.45081668[source]
> one would have to look up what "isSecure" means, while "(condition4 && !condition5)" would have shown it at once

You would feel the need to look up a variable called isSecure, but would not need to look up condition4 or condition5? I think the point TFA was making is that one could read isSecure and assume what kind of implementation to expect, whereas with condition4 I wouldn't even know what to look for, or I'd even struggle to hold any assumption.

  /* this one needs to make sense in the end */
  isSecure = user.role == 'admin'
  
  /* these two do not */
  condition4 = user.id <= 4
  condition5 = session.licenseId == 5
> and an "is secure" comment could be used to assist skimming

Those are exactly the kind of comments I'd rather see written out as intermediate variables. Such comments are not explaining to you any of the Why?s anyway, and I also tend to trust the executing code much more than any non-type-related annotating code, as comments are rarely helpful and sometimes even describe wishful thinking by straight-up lying.

Intermediate variables assist in skimming too.

replies(4): >>45081878 #>>45082716 #>>45089490 #>>45093925 #
1. wraptile ◴[] No.45089490[source]
This is a very popular pattern in python where you'd introduce _variable before the evalution so:

if (user.id < 4 and user.session): ...

# you'd do

_user_has_access = (user.id < 4 and user.session) if _user_has_access: ...

# or even walrus

if _user_has_access := (user.id < 4 and user.session): ...

One of my coworkers really liked this pattern and his code was always so easy to read and to this day I'm carrying this torch!