Dyego Maas - Blog

Generative AI Consultant and Software Architect

The best way to place local variables in a method

The best way to place local variables in a method

In this article, I present an intuitive way to decide where to declare each local variable in a method, one that's also easy to teach to people learning clean code.

4 min read

One of the simplest and most important topics in clean code is variable declaration. While variable names matter enormously in writing clean code, where they sit inside a method often gets little attention, and that can hurt how easy the code is to understand.

But there are a few super intuitive practices we can apply to make our code even easier to read, and they’re quite easy to teach to developers of any experience level.

Proximity

Unfortunately, a common programming habit is to declare variables near the top of a method and only use them further down:

The variables used throughout the method are declared at the top, so their purpose isn't obvious.
Variables declared at the top of the method

In the example above, this approach ends up creating unnecessary noise. The variables raioBusca (search radius) and utilizarBuscaEstendida (use extended search) are declared at the top and only used further down, after two other operations. Even though their names are quite expressive, they’re very far from where they’re used, which we can represent with a right triangle drawn over the code.

The same code as before, with a right triangle linking the variable declaration to its first use further down.
Distance between declaration and first use

Aviso

The longer the method, the more noise this approach creates.

As you can see in the example above, a good chunk of code falls inside the triangle. We could say this stretch stands in the way of more expressive code. The same problem affects the timestamp variable:

The same code as before, with a right triangle linking another variable declared at the top to a call even further down the method.
Distance between declaration and first use of the timestamp variable

We can fix this easily by moving the variables closer to where they’re used, as the next image shows:

The variables used throughout the method are declared close to where they're used.
Variables declared close to their use

This way, each group of operations is isolated from the others, and each processing step in the method stands out even more.

The hypotenuse now approaches a straight line.
Short distance between declaration and use

Order matters

Even in the second scenario, where the variables are declared close to their use, the order in which they’re declared matters when reading the code. In the example we’ve been working with, raioBusca is declared before utilizarBuscaEstendida, but used after it.

Even though they’re close to where they’re used, there’s a break in the sequence that kills some of the code’s flow. Once again, we can visualize this with triangles:

The first variable's triangle completely encloses the other one, exposing a problem.
The declaration order doesn’t match the order of use

By flipping the declaration order so it matches the order in which the variables are used, we can see that neither triangle fully encloses the other:

The first triangle's hypotenuse crosses the second one's.
The declaration order matches the order of use

Simply reordering the variable declarations makes the code a little easier to read, making it flow more naturally, with basically zero extra effort.

Distance as a placement indicator

Obviously, nobody needs to go around drawing triangles over their code while programming. I used these drawings to illustrate an intuitive way of deciding where to place each local variable inside a method.

It’s very easy to picture a straight line from a variable’s declaration to where it’s first used. We can then apply a simple heuristic: our goal should be to shrink that distance as much as possible for every variable in the method.

That imaginary straight line also coincides with the hypotenuse of a right triangle. Whenever it’s longer than it needs to be, the area of that triangle will end up enclosing code that isn’t directly related to the variables we’re declaring, and they need to be moved closer to where they’re used.

Moving the variables close to their use shortens the side leg.
The triangle’s area shrinks

Conclusions

There are some very subtle aspects that affect code readability, and their impact is far from obvious to many developers, especially those still early in their journey.

In a Zoom room it’s very easy to draw over the screen, and I often use the method described above to explain these concepts during pair-programming sessions. It usually works really well.