Psychology of code readability

https://medium.com/@egonelbre/psychology-of-code-readability-d23b1ff1258a
– we remember 4+/-1 item in our short memory, then they fade away.
– in our brain facts/knowledge pieces are grouped, chunked and CHUNKS are linked.
– we process input with our focus/locus of attention (one thing at the same time in the same space)
– focus/locus is called CONTEXT
– MENTAL MODEL is what our brain builds from CHUNKS in CONTEXT
– Problems with me our mental models are many. Brain needs an extra work if mental models are:
– rebuilt by switching contexts (which is expensive)
– dealing with abstractions
– are similar or ambiguous
– there is mental noise (extra unrelated input)
– we process negation
– All these obstacles increase COGNITIVE LOAD – amount of mental effort we invest in processing.
– More cognitive load incrementally decrease our mental capacity or efficiency, if you will.

Name scopes

Length is not a virtue in a name; clarity of expression IS.
Rob Pike

The longer name also doesn’t help creating a better context, effectively it is just noise.

Variable state

The more complex single variable states are during method execution,
the harder it gets to track all the changes at the same time.

Idioms/Phrases

Idioms are groups of words that took different meaning |
that is not deductible from each word separately.
Programming language expressions can turn into idioms,
at least in hour own minds and in small teams.
Conclusion: same expressions written with same names/words
create implicit idioms in our mind and are easier to read over time.

Consistency

The more consistent the code base is in naming, formatting, structure, interaction etc.
the easier it is to jump into arbitrary code and understand it.

No matter how good/bad is your architectural or coding decision is
(about code organization, naming convention etc.)
the best way to proceed with it is to be consistent about it.
John Papa

Uncertainty

Ambiguity & Similarity (DDD: Ubiquitous language)
[1, 2, 3].filter(i, i >= 2)
Client may be a different thing in shipping and billing modules

Comments

Comments should provide CONTEXT by answering WHY question.
Answering on WHY something was written in a particular way or WHY it does something it does
most certain must provide some kind of detailed context around the code.

Leave a Reply

Your email address will not be published. Required fields are marked *