Live data from Hacker News

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

chrismm.com

111–120 of 187 posts

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

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

Unless it is an interesting algorithm or contains well documented optimizations. (with reasons for them, preferably grounded in measurements)

Rules of thumb only go as far as your thumb. Sometimes a clever abstraction makes everything clean and obvious, as opposed to a dumb abstraction.

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

#112
post #60

Code Complete is an old book now. Is it still good advice? In my new project I see a lot of code like below. I hate it but I'm not sure if I'm old fashioned or correct in thinking it should be 4 or five lines. Should I reject a code review for stuff like this? return (HadoopSummary)ScopeCoordinator.getInstance().findObject(Scope.getFirst(), new Path (SCOPE_PATH.split("\\.")));

> Code Complete is an old book now. Is it still good advice?

Software isn't a mature, rigorous field relentlessly marching forward into the future. Silicon engineering is, sure. But our field is constantly constantly rediscovering stuff from the 60's and 70's, and going through fashions and fads.

A software book being old really doesn't matter to me IMO. We still don't really know what we're doing yet.

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

#113
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…

> These days I'm pretty pleased when I can say a piece of code utilises only syntax and statements taught in an introductory programming course.

Why?

Shouldn't you be building up from that towards the problem you are actually trying to solve? Is it reasonable to expect someone to understand every single bit of code in a large project without reading documentation of the components below it?

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

#114

Earlier quoted context omitted.

One-liners are bad if they are obscure and experienced programmers know that. However I wouldn't go as far as say that experienced programmers don't use one-liners at all. As usual in programming, it's all about balancing clarity/conciseness. For example, I find this much clearer as a one-liner (Python): validated_items = filter(is_validated, items) rather than validated_items = [] for item in items: if is_validated(…

Ruby version is a pretty clear one-liner: validated_items = items.select { |item| is_validated?(item) } Though I'd expect a check for validation to be an instance method, so it'd probably look like: validated_items = items.select(&:validated?)

I don't find either of the Ruby versions clear at all. Both contain unnecessary syntax.

Ruby seems to be going down the same path as Perl in trying to make all possible combinations of characters valid programs.

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

#115

Earlier quoted context omitted.

But on what object? The fact that calling .MoveTo(10, 30) would return the same object it has been called on, and not, for example, a handle to created movement animation, is not exactly obvious. Modern pattern of methods which just return the objects that they've been called on, by default seems to be very trendy, but I fail to see how is it helpful.

Er... on the window, as the indentation and API tells you. It may be "not exactly obvious" to you, but having been doing GUI programming for a long time, this makes complete sense to me. It is also, of course, a matter of personal style and preference. (Aside: some friends tell me, a vi user, that the way Emacs works is obvious. I disagree, but then, I don't use it). And as for "modern"... I've been using this since…

Are you sure? Because to me it looks that first call should return a canvas, and all consecutive calls operate on it.

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

#116
post #99
post #69

Earlier quoted context omitted.

Or: validated_items = [x for x in items if is_validated(x)]

I prefer the other one, because then I don't have to consider whether there is an outer variable 'x' that is being clobbered.

Most linters would have suggested correcting it to:

validated_items = [_ for _ in items if is_validated(x)]

where _ is used as a safe variable that can always be clobbered. (Though this does also clash with its use as a gettext function for strings).

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

#117

Earlier quoted context omitted.

Ruby version is a pretty clear one-liner: validated_items = items.select { |item| is_validated?(item) } Though I'd expect a check for validation to be an instance method, so it'd probably look like: validated_items = items.select(&:validated?)

I don't find either of the Ruby versions clear at all. Both contain unnecessary syntax. Ruby seems to be going down the same path as Perl in trying to make all possible combinations of characters valid programs.

    my @validated = grep { is_validated($_) } @items;
I'd argue the Perl version is clearer... assuming you know its syntax, that is.

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

#118
post #73

Earlier quoted context omitted.

When learning C in college, I was specifically taught to use CONTSTANT == variable. Accidentally omitting a '=' is common and putting the constant on the left side ensured the compiler would catch the mistake.

I would argue that by using CONSTANT == variable, you are reducing the cognitive load of the compiler, but increasing the cognitive load of the human reading the code (who has to mentally flip the expression around). I think the original article intended to say the same thing.

I don't get it. A test for (in)equality is commutative unless you're inducing side effects (i++) - and then side effects are going to be the big "cognitive load", regardless of which side of the comparison they occur. Am I dyslexic because (null != foo) is exactly as easy for me to read as (foo != null)?

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

#119
post #89
post #39

Earlier quoted context omitted.

I can't articulate exactly why, but that should totally be flipped. Should it? If you accidentally use an assignment operator (eg = ) instead of a comparison (eg === )[1] with the constant on the left your code will throw an exception, which is what you want. If you put the variable on the left and accidentally use an assignment then you'll overwrite the variable with the constant value and return true, consequently…

This 'reason' is as old as C, and it's always seemed to me like tying bells to your shoelaces because it would be bad if you forgot to tie them and tripped. I.e. the 'solution' is orthogonal to the problem. If you can train yourself to put the constant first, you can train yourself to use the right operator. And you can test your code.

> If you can train yourself to put the constant first, you can train yourself to use the right operator.

Right... that's assuming you work solo. In that case, you might as well be using some higher level language and sidestep the whole issue.

Most C programmers out there work in teams, in long lived projects... that means teams with rotation of personnel. This is a fuckup waiting to happen.

I get you guys do not like axes, and it is really Ok. But forgive me if I am skeptical of your axe redesigns until the day you actually go out and do a full hour of wood chopping with that fancy bastard sword of yours.

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

#120

Whenever I read articles like this and the ensuing discussion that follows, I'm reminded of this quote from Dijkstra: Don't blame me for the fact that competent programming, as I view it as an intellectual possibility, will be too difficult for "the average programmer" — you must not fall into the trap of rejecting a surgical technique because it is beyond the capabilities of the barber in his shop around the corner.…

We've been refactoring and decoupling code for decades in imperative coding styles. I think one of the problems we have as a community of programmers is the concept that it must end up in a ball of mud. Almost all of our extant systems are developed in imperative style. The system I work on today is many orders of magnitude larger than the systems I first learned on and yet it is dramatically easier to write useful c…

   "If you don't have time to get it right the first time, how on earth will you have time to do it again?"
I like that quote, but to be honeste. Some times you have to get it out of the door, and time is of the essence. And you can go back and fix it. That is okay, as long as you understand that shortcutting now, will cost time tomorrow (i.e. technical debt).
Post reply on HN