Write Clean Code to Reduce Cognitive Load

Cognitive Load (via the Google blog)

Cognitive Load (via the Google blog)

Today I stumbled upon a Google blog on writing clean code. It talks about why and how we should write Clean Code

Cognitive Load

Cognitive load is the amount of mental effort that is required to complete a task. In this case, our cognitive load grows with the complexity of the codebase. It's usually harder for other people to understand your code, because everyone writes code a little differently.

Be kind to your co-workers: reduce their cognitive load by writing clean code

Mantras

Some rules to always follow while writing code are:

1. Limit amount of code in function & files:
  • single responsibility, keep functions small
  • Make code self documenting with small functions (with good names)
2. Create abstractions to hide implementation details
  • use abstractions, functions, interfaces hide complex details
  • but don't over-engineer
  • If you view a code, the intentions of it does not jump at you at first sight. Abstract
3. Simplify control flow:
4. Minimize mutable state:
  • make types immutable
  • avoid mutable class fields
5. Others:
4/3/2024