DEV Community


Don't declare your Variables at the top

gsferreira profile image Guilherme Ferreira Originally published at on ・1 min read

When I start learning to code, I was taught to declare variables at the top of the code. I followed it for a long time before start questioning that absolute truth.

We are writing code for others to read. So, we shouldn't present our characters too early in the story. Our working memory handle a strict number of things at a given time.

Working memory overflow

Keeping related concepts closer, we are reducing the cognitive load. This will lead us to clean and simple code.

You can find evidence of that, in Design Principles. By using the Proximity principle, we form groups by keeping things closer.

This is one of the reasons why I run away from global variables.

Besides the cognitive load reduction, there's a positive side effect from following this. I use this as a warning sign to extract a function. Whenever you can't keep the variables close to where they are used and to the top, extract it.

So, instead of doing this:

Example 1

You can do this:

Example 2

And then refactor to this:

Example 3

Discussion (0)

Forem Open with the Forem app