Live data from Hacker News

Simple Ways of Reducing the Cognitive Load in Code

chrismm.com

151–160 of 203 posts

Re: Simple Ways of Reducing the Cognitive Load in Code

#151

These seem like good rules to follow, but there's nothing to suggest that they reduce cognitive load. To make that claim, you need experiments testing brain function or at least people's behavior...

To follow up with a point of comparison, this is the kind of work that can make a claim about how cognition and coding are related:

http://www.cs.cmu.edu/%7Eckaestne/pdf/icse14_fmri.pdf

Re: Simple Ways of Reducing the Cognitive Load in Code

#152
post #73

Earlier quoted context omitted.

state can be passed through as arguments...

When coding in C, I often package the state that's shared between a high-level function and its local subroutines in a local "struct context" that's instantiated in the high-level function and then passed by address as the first argument to the subroutines. Makes it easy to see what the shared state is, and adding/changing the shared state doesn't require changing all the formal and actual argument lists.

Me too. OOP is actually having several such states. My go to pattern is to root states in an application tree.

Re: Simple Ways of Reducing the Cognitive Load in Code

#153

Stopped reading at "Place models, views and controllers in their own folders". No worse way to organize your code than classify by behavior type. "Here are all the daos", "here is all business logic", "here are all the controllers". You add a feature as small as resource CRUD and scatter it's pieces across the whole code base. No.

Honest question from someone with little real world experience outside .Net: This is MVC's convention, to "Place models, views and controllers in their own folders", and it's what I'm used to working with. Can you point me to resources outlining other methods (responding with google "XYZ" would be fine too).

I believe it's called vertical packaging.

Package "employee" contains: EmployeeDto, EmployeeService, EmployeeController Package "order" contains: OrderDto, OrderService, OrderController

And so on. This way logic that is closely related is kept closeby.

Re: Simple Ways of Reducing the Cognitive Load in Code

#154

Stopped reading at "Place models, views and controllers in their own folders". No worse way to organize your code than classify by behavior type. "Here are all the daos", "here is all business logic", "here are all the controllers". You add a feature as small as resource CRUD and scatter it's pieces across the whole code base. No.

Honest question from someone with little real world experience outside .Net: This is MVC's convention, to "Place models, views and controllers in their own folders", and it's what I'm used to working with. Can you point me to resources outlining other methods (responding with google "XYZ" would be fine too).

A .NET take on it https://lostechies.com/jimmybogard/2015/07/02/ndc-talk-on-so...

Re: Simple Ways of Reducing the Cognitive Load in Code

#155
post #38

Good advice and worth reading especially for younger devs. With respect to... >> Prefix systems like hungarian notation were initially meant to add meaning, but with time they ended up being used in less contextual ways, such as just to add type information. Hungarian notation was pretty cumbersome to read, actually, and I think the main reason it fell out of use is that editors and IDEs began to make type and declar…

One of the major functions of Hungarian notion was to communicate information which was not contained in the types of the actual variables, for example an int could be a count of bytes 'cb', or perhaps a handle 'h', etc. But it ended up being mostly misused to communicate redundant type information, such as a char* being 'sz' (zero-terminated string), which tells us nothing we didn't already know. As you say, better…

'sz' is pretty useful if you have pascal strings floating around.

Re: Simple Ways of Reducing the Cognitive Load in Code

#156
Maximize order. Order is the lubricant for information. And if you back your reasons with guiding principles (aka philosophy) the specifics will remain obvious as well as sort themselves.

These are the only ways to reduce cognitive load and they apply to any situation where one needs to understand something. After all, code is about understanding.

Anecdotally, the specific methods mentioned in the article that seem most valid stem from the guiding principle of maximizing order, which drastically reduces the cognitive load of the contents of the article.

Re: Simple Ways of Reducing the Cognitive Load in Code

#157
post #65
post #47

Earlier quoted context omitted.

That's like the function composition operator [1] in Haskell, right? Very neat :D I wonder if there's an equivalent macro in Scala ... [1]: http://lambda.jstolarek.com/2012/03/function-composition-and...

It's more like the pipe operator in ocaml ( http://blog.shaynefletcher.org/2013/12/pipelining-with-opera... ). The lisp version has the extra advantage that you don't have to repeat it between all the intermediate functions. ((->> 2 (* 100) str count) vs 2 |> (* 100) |> str |> count).

I understand how a lisp implementation would work here to require only the single operator (I'm assuming a fairly simple macro).

Would it not be possible to do something similar in another functional language to take a and apply it sequentially to a list of function calls?

Re: Simple Ways of Reducing the Cognitive Load in Code

#158
Along the same lines, use the right language for the abstraction you are dealing with. In a server environment, I prefer to have modular services linked together with some message queue.

In web projects, this is what has kept me coming back to CoffeeScript: we found it less distracting visually, given the kind of code we were writing (heavy call-back oriented, lots of chained methods).

Re: Simple Ways of Reducing the Cognitive Load in Code

#159
post #31

Earlier quoted context omitted.

I recently got a code review that in several places suggested I switch to the new Java 8 stream API [1]. I just flatly responded that it was far less readable, even if I could condense a half-dozen lines of code down to one. Where I can quickly scan over a foreach loop to get the jist of what it's doing, I have to closely examine each call in the new approach to have any idea what it's doing. [1] http://www.oracle.co…

Look, you're basically just saying you don't want to learn anything new. "I just flatly responded that it was far less readable, even if I could condense a half-dozen lines of code down to one." Express the exact same thing, in 1/6 the code, and somehow the shorter code takes longer to read and understand? You don't even attempt to express why you find the shorter code harder to understand. Or show code written both…

> Express the exact same thing, in 1/6 the code, and somehow the shorter code takes longer to read and understand?

I think people need to differentiate between code review and code scanning. I look at code completely different when someone is pointing to it than when I'm scanning through a file. A ternary operator isn't tough to read when you're already looking at it. However, scanning through a file I'm going to have a lot more difficulty spotting a `?` than I am the indentation of an if/else block. I'm more than content to take 5 lines instead of 1 to spot it 2 months later when I don't know what I'm looking at.

Re: Simple Ways of Reducing the Cognitive Load in Code

#160
post #125

Earlier quoted context omitted.

Perhaps an alternative would be valid_user = loggedIn() && hasRole(ROLE_ADMIN) if (valid_user) { valid_data = data != null && validate(data) if (valid_data) { ... } }

For whatever reason, I'd prefer comments to this version. Note: I actually agree with the OP about pulling the logic into named conditionals whenever possible, but in the case you do want the short-circuiting behavior I would not bother with the variables at that point. if (loggedIn() && hasRole(ROLE_ADMIN)) { // User has permission to do this if (data != null && validate(data)) { // Submitted data is valid ... } }

I think part of the reason for doing it the way they do in the article is to reduce nesting. I saw the problem with missing the short circuit as well though which is why I like the suggestion of using

  if (isValidUser() && isValidData(data))

And checking for data being null in the isValidData method. There is a little overhead in the call and return if data is null, but the clarity provided seems like a win in these kinds of cases.
Post reply on HN