Live data from Hacker News

Simple Ways of Reducing the Cognitive Load in Code

chrismm.com

21–30 of 203 posts

Re: Simple Ways of Reducing the Cognitive Load in Code

#21

...and another: Use of whitespace (vertical and horizontal) to group and associate code with related parts. Its a trick borrowed from graphic design, but negative-space works really nicely.

I've come to appreciate this as well and it's what has driven me to prefer spaces over tabs.

Re: Simple Ways of Reducing the Cognitive Load in Code

#22
post #4

I really like the advice from "Perl Best Practices" to code in paragraphs. Sometimes, a large function cannot be broken up usefully, because a lot of state needs to be shared between the different parts, or because the parts don't have a meaning outside of the very specific algorithm. In that case, code in paragraphs: Split the function body into multiple steps, put a blank line between these and, most importantly, a…

Shared state between different parts of the code sounds an awful lot like something that could be a class.

Yes, a Method Object pattern http://c2.com/cgi/wiki?MethodObject

Re: Simple Ways of Reducing the Cognitive Load in Code

#23
post #17

As a junior dev I can confirm the advice about junior devs is very accurate. An anecdote: I recently started working with a team on their half completed web app. They had so many dependencies, and tools for managing dependencies, it took me far longer than it should have to become productive. It's obviously not my place to question which technologies they use, but it can be frustrating.

It is your place to question everything. Your contribution to the team, even if it comes in the form of perspective alone, is valuable anyway.

Re: Simple Ways of Reducing the Cognitive Load in Code

#24
post #23
post #17

As a junior dev I can confirm the advice about junior devs is very accurate. An anecdote: I recently started working with a team on their half completed web app. They had so many dependencies, and tools for managing dependencies, it took me far longer than it should have to become productive. It's obviously not my place to question which technologies they use, but it can be frustrating.

It is your place to question everything. Your contribution to the team, even if it comes in the form of perspective alone, is valuable anyway.

I'd go even further and say that newcomers to teams are often the most able to pinpoint essential flaws in the development process.

Re: Simple Ways of Reducing the Cognitive Load in Code

#25
post #4

I really like the advice from "Perl Best Practices" to code in paragraphs. Sometimes, a large function cannot be broken up usefully, because a lot of state needs to be shared between the different parts, or because the parts don't have a meaning outside of the very specific algorithm. In that case, code in paragraphs: Split the function body into multiple steps, put a blank line between these and, most importantly, a…

I completely disagree, every method can be split in private methods. In that way you don't need awful and unhelpful comments in the middle because you can understand what it does simply from the method name.

Methods can be split into private methods. But what that sometimes means is, when you're trying to understand how a piece of information flows through a method to fix a bug, instead of it being all self-contained in the method, instead you have to jump around to 10 places in a file to see where the information goes. That severely increases the cognitive overhead.

Re: Simple Ways of Reducing the Cognitive Load in Code

#26
post #4

I really like the advice from "Perl Best Practices" to code in paragraphs. Sometimes, a large function cannot be broken up usefully, because a lot of state needs to be shared between the different parts, or because the parts don't have a meaning outside of the very specific algorithm. In that case, code in paragraphs: Split the function body into multiple steps, put a blank line between these and, most importantly, a…

I completely disagree, every method can be split in private methods. In that way you don't need awful and unhelpful comments in the middle because you can understand what it does simply from the method name.

sorry, but given this pre-condition (from gp):

> ... a large function cannot be broken up usefully, because a lot of state needs to be shared between the different parts ...

there is no way to break that up with multiple smaller functions without stowing away the state somewhere.

sometimes reading a large function is not half as bad as reading 10 different ones with each altering the shared state.

Re: Simple Ways of Reducing the Cognitive Load in Code

#27
post #24
post #23

Earlier quoted context omitted.

It is your place to question everything. Your contribution to the team, even if it comes in the form of perspective alone, is valuable anyway.

I'd go even further and say that newcomers to teams are often the most able to pinpoint essential flaws in the development process.

And that applies irrespective of a newcomer is junior or senior. There is hardly a substitute for a fresh pair of eyes t to reveal what one thinks is cool design, code et al is not so cool after all.

Re: Simple Ways of Reducing the Cognitive Load in Code

#28
post #13

I'd like to add one: let your tools do the work for you. It may seem like a pain to learn the tooling behind what you do, but once you internalize it, it becomes a superpower. An example is that I use Clojure Refactor Mode (with CIDER) for emacs. A trick (and treat) that a lot of Clojure code uses is the arrow macros: -> and ->>. Clojure Refactor Mode has thread-first, thread-first-all, thread-last, thread-last-all a…

I agree with the tooling part. I tend to prefer the functional version to threading because (1) honestly, like fluent interfaces, it seems overused (2) function application is damn easy to read and (3) as soon as you have as many nested operations, it can and should be refactored into meaningful auxiliary functions. The first line reads as: Multiply all ... 4 copies of ... the length of ... the string "200". So, basi…

One of the nice things about the Clojure standard library is that most everyday functions actually do fit the first/last category (by design). Functions operating on sequences (map, filter, reduce, etc) take the sequence as the last argument and are suited for use with the ->> macro, while functions operating on data structures in a non-sequence context typically take the data structure first (assoc, conj, update) and are good for ->. So you get either:

    (->> (range 10)
         (map inc)
         (filter even?)
         (take 2)) ;=> '(2 4)
or

    (-> {:body {:some {:json :data}}}
        (assoc-in [:body :some :more-json] :more-data)
        (assoc :status 200)
        (update :body json/generate-string))
    ;;=> {:body "{\"some\":{\"json\":\"data\",\"more-json\":\"more-data\"}}", :status 200}
        
It doesn't work all the time, obviously, and it can be easy to get carried away with 15 threaded map/filter/reduce calls that should be factored into separate functions, but most of the time I find it to be a nice idiom that substantially improves readability.

Re: Simple Ways of Reducing the Cognitive Load in Code

#29
post #24
post #23

Earlier quoted context omitted.

It is your place to question everything. Your contribution to the team, even if it comes in the form of perspective alone, is valuable anyway.

I'd go even further and say that newcomers to teams are often the most able to pinpoint essential flaws in the development process.

Except often the things pinpointed are not flaws just different from that they are used to.

Re: Simple Ways of Reducing the Cognitive Load in Code

#30
For the most part, I agree with this. The biggest problems I've had at work have been due to constructs that were a neat idea but just add to the complexity of figuring out the application. Throw in a bunch of business-specific engineering terminology that is not defined anywhere for the development team, and it becomes a wicked PITA to learn.

However, the one-liner example and the chained English-sounding methods, I think might be taken the wrong way. Both can be done well.

Post reply on HN