Live data from Hacker News

Writing good code: how to reduce the cognitive load of your code

chrismm.com

21–30 of 187 posts

Re: Writing good code: how to reduce the cognitive load of your code

#21

I like code that reads like a Dick & Jane book ("See Dick. See Jane. See Dick run. See Jane run."). However, it appears to be trendy to write insanely difficult to read code. To use the analogy, Shakespearean code. Instead of one line of code doing one thing the developers will write a ton of functionality into one line of code by using fluent and method chaining. As someone reviewing the code I have to keep this men…

I can't up-vote this enough. As someone that started doing development in the late 80's, I find this style of coding to be infuriating. It completely destroys the ability to map lines of code directly to function calls without resorting to manual coding rules that require that each .function() be on a separate line, and makes debugging way harder than it needs to be. And, for what purpose ? To avoid a local, temporary variable ? Do these developers think that their code is faster this way, or...what ?

It's "write-only" code - good luck to the next guy that has to read it.

And, don't get me started on: if = ... :-)

Re: Writing good code: how to reduce the cognitive load of your code

#22
post #18

Sometimes I find myself writing in reviews for less experienced developers the comment: this is clever but not clear. I think as developers we get too enthralled in the problem solving and forget that in the long run we are more like journalists noting business rules at a snap-shot in time, which a future maintainer of our software must act as historian/archaeologist in order to understand. What's funny is that often…

Everything is unclear until you become used to it, though. I mean, used to it, as in, you can read it without stepping yourself through the steps manually. And to a novice programmer, pretty much everything they come up against represents this. Turning five lines into one line with a reduce function sounds like a normal thing to do for experienced programmers, but to a beginner, they'll think you're a genius for poin…

You'll also find that some experienced programmers specifically turn 1 line into 5. It's the beginner that tries to create the 1 liner because they think it's genius.

Re: Writing good code: how to reduce the cognitive load of your code

#23
post #18

Sometimes I find myself writing in reviews for less experienced developers the comment: this is clever but not clear. I think as developers we get too enthralled in the problem solving and forget that in the long run we are more like journalists noting business rules at a snap-shot in time, which a future maintainer of our software must act as historian/archaeologist in order to understand. What's funny is that often…

One of the most valuable things I got from university was my professor's saying, "When somebody tells you your code is clever or interesting, that's an insult."

Re: Writing good code: how to reduce the cognitive load of your code

#24
post #18

Sometimes I find myself writing in reviews for less experienced developers the comment: this is clever but not clear. I think as developers we get too enthralled in the problem solving and forget that in the long run we are more like journalists noting business rules at a snap-shot in time, which a future maintainer of our software must act as historian/archaeologist in order to understand. What's funny is that often…

Offtopic, but whenever I read someone referring to a programmer having to be an archeologist, I can't help but think about Vernor Vinge's _A Deepness In The Sky_[1], where there was so much legacy code that there was a need for "programmer archaeologists" to dig through it all looking for something suitable for whatever current problem needed to be solved.

1: https://en.wikipedia.org/wiki/A_Deepness_in_the_Sky

Re: Writing good code: how to reduce the cognitive load of your code

#25
post #18

Sometimes I find myself writing in reviews for less experienced developers the comment: this is clever but not clear. I think as developers we get too enthralled in the problem solving and forget that in the long run we are more like journalists noting business rules at a snap-shot in time, which a future maintainer of our software must act as historian/archaeologist in order to understand. What's funny is that often…

This, I have very rapidly found myself writing code which only very occasionally uses something fancy and otherwise generally opt for the most plain, boring and straightforward code. Internally, I describe the code as idiot proof.

Putting it more kindly, my litmus test for my own code is "will a 5 year old understand this?". In the many instances where I have since returned to my code to maintain it, I am often grateful for every less minute I spend reunderstanding all my own code.

To use your analogy of engineers being like authors, as a teenager, I would often find every excuse to use some exciting sentence structure or long word - doing so made me feel authoritative and clever. But once reading more, you find that some of the most powerful, and clever, writing is concise and plain.

Be Hemingway, not Nabokov.*

*not to say Nabokov wasn't clever.

Re: Writing good code: how to reduce the cognitive load of your code

#26
post #2

I don't understand the first example. if (null != variable) If this was C, it should be NULL, and it is almost always better to just write `if (variable)` to check for NULL pointers instead. If this was JavaScript, this check includes undefined too. Not sure why it didn't use triple equal. If this was just talking about placing a constant value to be compared before a more complicated expression, I really don't see a…

I also don't understand this, nothing wrong about Yoda conditions and I don't understand the 'cognitive load' argument since the condition is just as readable. And: Visual Studio 2015 does not warn in the case of "if (a = 5)", not even at the highest warning level, only the VS2015 static code analyser catches this.

It also probably shouldn't (warn)

    if(Foo* a = GetAFoo()) { /* Do something with a non-null foo */ }
is perfectly valid code.

Re: Writing good code: how to reduce the cognitive load of your code

#27
post #2

I don't understand the first example. if (null != variable) If this was C, it should be NULL, and it is almost always better to just write `if (variable)` to check for NULL pointers instead. If this was JavaScript, this check includes undefined too. Not sure why it didn't use triple equal. If this was just talking about placing a constant value to be compared before a more complicated expression, I really don't see a…

I'm the accidental maintainer/guardian/dungeon keeper of a bunch of scary code at work, which happily does:

    if (false != aBooleanVariable)
I still can't parse that without stopping and thinking (and sometimes cursing the original author). To me, with booleans, this can only sanely be written:

    if (aBooleanVariable)
Code with literals is often worse than functionally equivalent code without; literals are complexity. And I hate literal comparisons with true and false for booleans. Aargh.

Re: Writing good code: how to reduce the cognitive load of your code

#29

I like code that reads like a Dick & Jane book ("See Dick. See Jane. See Dick run. See Jane run."). However, it appears to be trendy to write insanely difficult to read code. To use the analogy, Shakespearean code. Instead of one line of code doing one thing the developers will write a ton of functionality into one line of code by using fluent and method chaining. As someone reviewing the code I have to keep this men…

I love chaining, but one verb per line. Instead of something like

    Me.getFish.skin.debone.flour.salt.fry.eat
It would be something like

    Me.getFish
      .skin
      .debone
      .flour
      .salt
      .fry
      .eat
I find this more readable than doing it without chaining like this:

    fish = Me.getFish
    fish.skin
    fish.debone
    fish.flour
    fish.salt
    fish.fry
    Me.eat(fish)

Re: Writing good code: how to reduce the cognitive load of your code

#30

I like code that reads like a Dick & Jane book ("See Dick. See Jane. See Dick run. See Jane run."). However, it appears to be trendy to write insanely difficult to read code. To use the analogy, Shakespearean code. Instead of one line of code doing one thing the developers will write a ton of functionality into one line of code by using fluent and method chaining. As someone reviewing the code I have to keep this men…

I can't up-vote this enough. As someone that started doing development in the late 80's, I find this style of coding to be infuriating. It completely destroys the ability to map lines of code directly to function calls without resorting to manual coding rules that require that each .function() be on a separate line, and makes debugging way harder than it needs to be. And, for what purpose ? To avoid a local, temporar…

To get you started, I'd like to compare my problems regarding `if = ` with yours. This assumes you aren't the kind of lunatic to actually assign a constant inside of an if's condition.

1. I can't articulate exactly why, but that should totally be flipped. The constant should be on the right side! `if(3.17 == possiblyPi)` would make me seriously question the author's motives.

2. It's personal preference, but in almost every situation I'd prefer to use a switch over an enum than lots of constants.

Post reply on HN