Minimize the Scope of Each Variable

Whenever you declare a variable, please make sure to make its scope as small as possible. Yesterday I was refactoring a pretty large function, with lots of loops in it. It looked something like this:

void f() {
  int index;
  double delta;
  //(...)tens of more variable definitions
  for (index = 0; index < _v1.size(); ++index) {
    delta = getDelta();
    //Computations, using delta and other variables
  for (index = 0; index < _v2.size(); ++ index) {
    delta = getDelta();
    //Computations, using delta and other variables
  //(...) hundreds of similar lines

This is of course a simplified example, the real function was four hundred lines long, and full of array-indexing and computations. (It had been ported more or less verbatim from Fortran, which explains the clustering of declarations at the top.)

The problem when making a change to such a function, is that the scope you need to understand is unnecessarily large. You never know if the value of index or delta is used in some clever way further down in the function, so if are changing it in one place, you basically have to grok the entire function.

On a side note, this is also a bad idea:

void f() {
  double delta;
  for (...) {
    delta = getVal();
    //Computations, using delta and other variables

Even though it looks like you have cleverly optimized the definition of delta outside of the for loop, the only thing you have done is increase its scope and decrease readability. The compiler is perfectly capable of doing such optimizations for you.

Posted in C++. Tags: . 2 Comments »

2 Responses to “Minimize the Scope of Each Variable”

  1. Asbjørn Ulsberg Says:

    Not only are you right, there are entire IDE’s and plugins devoted to solving problems like this. ReSharper helps a long way to fix this in C# code, where it recommends moving declarations closer to usage when possible. It also makes it a lot easier to refactor hideously large methods like the one you’re having pain with into smaller parts, only by marking the code you want to make into a method and pressing CLTR+ALT+M.

    Whenever the body of a method increases beyond the point of being able to see it all within one page on a reasonably large monitor, no compiler should allow you to compile the code. Infact, you shouldn’t be able to save it to disk, even. IMNSHO. ;)

  2. Anders Schau Knatten Says:

    Yeah, you C#/Java-guys are really spoilt! :)

    A nice little trick I sometimes use is to wrap pieces of large function in blocks, just to limit scope. It is then easier to reason about the code, and also to see what is needed to extract a method.

    Btw, if one is to follow your rule, no legacy C++ system in the entire world would compile until the next Mayan cycle…

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: