Live data from Hacker News

How to reduce the cognitive load of your code

chrismm.com

21–30 of 239 posts

Re: How to reduce the cognitive load of your code

#21
No one ever mentions formatting.

I really like aligning multiline blocks, adding whitespace and useless braces here an there. e.g: Having just a single space between function name and arguments makes it look less like a call. Yet almost all lint presets/defaults forbid this. Typography is all about the whitespace between letters forming easily recognizable shapes.

Re: How to reduce the cognitive load of your code

#22
post #19

I used to think that a lot of bad code out there was made by lazy, incompetent programmers... But then, after a certain job, I realized that this is probably not the case. Now I belive that most bad code out thare is made by overworked and tired programmers in a rush to deliver something that works.

Most of the bad code was made by very productive developers.

Because productive developers write more code or because productive developers write worse code?

Re: How to reduce the cognitive load of your code

#23

I used to think that a lot of bad code out there was made by lazy, incompetent programmers... But then, after a certain job, I realized that this is probably not the case. Now I belive that most bad code out thare is made by overworked and tired programmers in a rush to deliver something that works.

Where I work, all the devs are lazy and incompetent... So I have to rush and overwork to get something that works.

Re: How to reduce the cognitive load of your code

#24
post #21

No one ever mentions formatting. I really like aligning multiline blocks, adding whitespace and useless braces here an there. e.g: Having just a single space between function name and arguments makes it look less like a call. Yet almost all lint presets/defaults forbid this. Typography is all about the whitespace between letters forming easily recognizable shapes.

Yes, very important. I love editors that does most of this from a button press. Makes others code easier to understand. Netbeans have lots of settings so you get the braces and spaces where you want them.

Re: How to reduce the cognitive load of your code

#25

I used to think that a lot of bad code out there was made by lazy, incompetent programmers... But then, after a certain job, I realized that this is probably not the case. Now I belive that most bad code out thare is made by overworked and tired programmers in a rush to deliver something that works.

It's both. Some of my worst code was to look at someone else's code after a manager shouted "FIX THIS NOW!!!" with some kind of quick and dirty hack, and then never going back to get it done right. I'm pretty sure this happens independent of whether or not I am any good. :-)

Re: How to reduce the cognitive load of your code

#26
post #21

No one ever mentions formatting. I really like aligning multiline blocks, adding whitespace and useless braces here an there. e.g: Having just a single space between function name and arguments makes it look less like a call. Yet almost all lint presets/defaults forbid this. Typography is all about the whitespace between letters forming easily recognizable shapes.

Typography is much more than that, of course, but with fixed width plain text, you don't have many options...so ascii art it is. I'm pondering a language that includes formatting abstractions so that you can prepare code for reading along with its functionality (sort of like literate programming, but still starting from code). I would love to see comments in a side bar, long monotonous calls organized into tables, proper spacing and even lines to delimit sections, and so on...

But our current programming systems, we are still very much in the dark ages of code typography.

Re: How to reduce the cognitive load of your code

#27
There are so many similarities between writing code and writing English.

- Thinking of paragraphs as functions with one purpose

- keeping sentences short to reduce load on working memory and increase comprehension

- create visual breaks to help the reader by grouping common stuff together as mini-functions

- reduce intimidation factor of reading by removing convoluted stuff

- remove cognitive noise (dead code, unnecessary comments, variables declared out of context or too soon etc.)

- keeping terminology consistent across the code (domain language)

- not using double negatives e.g a = !notLocked

- using automated systems to simplify expressions (e.g weird boolean conditions http://www.wolframalpha.com/input/?i=a+%26%26+b+||+c )

etc.

Unlike English, it is less difficult to have a program reorganize your code to make it more readable based on a set of cognitive principles.

But, beyond the readability and understanding of a function, we should also learn from other engineering fields. For example, system thinking helps tremendously in organizing code if the cost of refactoring code wasn't so risky in dynamic languages.

Re: How to reduce the cognitive load of your code

#28

The article is well-intentioned but misses the point in a few places. For example, the suggestion to have, in an MVC project, three top-level directories: one each for models, views, and controllers. This works fine for small projects, but larger projects can see significant benefit by keeping related code together. As with everything, it's a judgment call. The simple "one folder per type of thing" rule may not be ap…

Yeah, where do you add queues, events, observers, helpers, traits, interfaces, etc.?

Re: How to reduce the cognitive load of your code

#29

Very good Until you have some code reviewer that thinks otherwise because of some "stupid reason" and you can't get around their hard heads. One example, breaking a 81 char line because it goes over the limit and getting two shorter lines that are awful to read So yeah I'll go for this when I'm working with reasonable people

So, what do you do if you get a job with Java or iOS and 30++ char method names?

(Not being snarky, I would like to do some iOS development for fun but want lines under 78 chars, so it don't go over 80 with diff.)

Re: How to reduce the cognitive load of your code

#30
The variable name you choose when writing a function may not make any sense when reading it a month later, or to a third party. These rules are as useful as feng shui -- if you have a good design sense, you can produce good results with FS.

The readability wins I've seen come from using common tools; I'm going to be much more productive on a codebase written in a library ecosystem I understand. More generally, a powerful standard library with consistent calling conventions can help a language be useful. It's not productive for me to spend 10 minutes reading a function only to realize 'oh, this human wrote their own string split'.

Tests help too because they make clearer the dependent typing of a function's call signature; stuff like 'don't pass both of these variables' or 'a must always be > b' are never clear (and I've never used a design by contract langauge). Tests are sometimes better than documentation because you can sometimes get alerted if they're wrong.

Most important rule for readable code: hire programmers who know how to read. Reading a large project is a skill and lses people can read than write code. Every project is going to have a quirk of its evolution that's hard to understand without a close reading. (Every large C project contains a buggy partial implementation of LISP). Hire programmers who can survive that.

Post reply on HN