Title roll

In a movie, names of the important actors are shown at the beginning because knowing important actors helps the audience to appreciate the movie. Minor actors are not listed there because they don’t matter. The same thing applies to the local variables: You should list up the variables of important role at the beginning of the function to help readers of the function to understand it easier. And minor variables should not be listed here. For example,

    void search(.....) {
        int first;
        int last;  
        int mid;    
    .....
    }

Having those variables in mind (with appropriate names, please) helps to read the function. Sometimes knowing those variables makes the rest of the function even obvious.

One exception: In C, casual iterators may have to be listed at the top. This is due to the flaw of this language. However, I sometimes do something like this to avoid listing ‘i’ in the local variable list.

    {
        int i;
        for (i = 0; i < .....) .....
    }

Or

    { int i; for (i = 0; i < .....) {
          .....
    }}

In this way, the part of the program becomes independent, allowing you to cut and paste anywhere.

Advertisements