Live data from Hacker News

How to reduce the cognitive load of your code

chrismm.com

11–20 of 239 posts

Re: How to reduce the cognitive load of your code

#11
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.

Re: How to reduce the cognitive load of your code

#13
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 applicable in all situations.

Re: How to reduce the cognitive load of your code

#14
post #5

If I was to describe the essence of clean code in one word, I would say "balance". Yes, yes, readability and problem separation and stuff is important, but so is efficiency and usability and security and deadline and everything. Bottom line: balance. And it's the hardest thing to achieve.

I've always worked through the idea that "Extremism is harmful".

Literally anything taken too far is a bad thing. And just about everything in life is a matter of finding the right balance.

Re: How to reduce the cognitive load of your code

#15

He advocates prefixes on variable names. I would like to see good examples of this as I have never seen them as helpful (especially the tblUsers and intUserId type that can be common) .

This old chestnut from Joel should be helpful (or at least entertaining): http://www.joelonsoftware.com/articles/Wrong.html

He eventually gets to "hungarian notation" (those redundant prefixes you don't like) towards the end of the piece. tl;dr: misinterpretation of a good idea, cargo-culted down through the ages.

Re: How to reduce the cognitive load of your code

#16
post #6

Isn't this just a fancy phrasing for "make your code easy to read"?

+1 This whole blog post reeks of elitism and over confidence.

The blog post over states its own importance as after learning these clean code would be easy. Like fade diets, magic paradigms etc... another example of someone believing or trying to convince others that there is a "magic path". "Just remember these four concepts", "get a six pack with just 20 minutes a day"...

There are a few good tips in here but really nothing new. A nice reminder that there are some simple steps to help improve your own code.

A large devil of clean code is not just in nitty gritty "have one line per logical action" but rather the deconstruction of a problem into easily followable steps.

In that way this is definitely not an exhaustive description.

Re: How to reduce the cognitive load of your code

#17

He advocates prefixes on variable names. I would like to see good examples of this as I have never seen them as helpful (especially the tblUsers and intUserId type that can be common) .

Christian points to Joel Spolsky's example of using prefixes to add meaning, not type information like your examples. From Joel's essay:

All strings that come from [user input] must be stored in variables (or database columns) with a name starting with the prefix "us" (for Unsafe String). All strings that have been HTML encoded or which came from a known-safe location must be stored in variables with a name starting with the prefix "s" (for Safe string).

    us = Request("name")

    ...pages later...
    usName = us

    ...pages later...
    recordset("usName") = usName

    ...days later...
    sName = Encode(recordset("usName"))

    ...pages or even months later...
    Write sName
The thing I want you to notice about the new convention is that now, if you make a mistake with an unsafe string, you can always see it on some single line of code, as long as the coding convention is adhered to:

    s = Request("name")
is a priori wrong, because you see the result of Request being assigned to a variable whose name begins with s, which is against the rules.

[1] http://www.joelonsoftware.com/articles/Wrong.html

(edit: formatting, citation)

Re: How to reduce the cognitive load of your code

#18

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.

I think the reality is a mixture of both, the lazy, incompetent programmers are under pressure to ship too!

Re: How to reduce the cognitive load of your code

#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.

Re: How to reduce the cognitive load of your code

#20
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

Post reply on HN