6

I was reading a book and here, a program is given like this -

#include<fstream>
#include<string>
#include<vector>

int main()
{
    string filename; // #1

    cout << "Please enter name of file to open : ";
    cin >> filename;

    if(filename.empty())
    {
        cerr << "Something...";
    }
    ifstream inFile(filename.c_str());  // #2
    if(!inFile)
    {
        cerr<< "Somthing...";
    }
    .
    .
    .
}

And the explanation paragraph says, that, the declaration statements exhibit locality of declaration, Which is explained like this

the declaration statements occur within the locality of the first use of defined objects.

I am very confused with that sentence and I am not able to understand what it actually means. I need an explanation with some example.

Adrian Mole
  • 30,672
  • 69
  • 32
  • 52
dukeforever
  • 174
  • 7

2 Answers2

9

the declaration statements occur within the locality of the first use of defined objects.

Is another way to say don't declare a thing until you need it. By doing so you bring the declaration to where the object is used and doing so makes it easier to know what that object is.

Imagine you have a function that is 1000 lines long. If you declare all the variables you use in the function at the start, but don't happen to use one of them until line 950, then you have to scroll back 950 lines to figure out what the type of that variable. If you instead declare it on line 949, and use it on line 950, then the information is right there and you don't need to hunt it down as much.

So in your example #2 is declared right before it's used, instead of at the top like #1 is.

NathanOliver
  • 150,499
  • 26
  • 240
  • 331
4

There are several different places in which you can declare variables in a C++ module. For example, one can declare all variables at the beginning of that module, like in the following example:

int MyFunc(int a, int b)
{
    int temp1, temp2, answer;
    temp1 = a + b * 3;
    temp2 = b + a * 3;
    answer = temp1 / temp2;
    return (answer % 2);
}

Alternatively, as in the code you have cited, one could declare each variable immediately before it is first used, as follows:

int MyFunc(int a, int b)
{
    int temp1 = a + b * 3;
    int temp2 = b + a * 3;
    int answer = temp1 / temp2;
    return (answer % 2);
}

Both are valid styles and each will have its supporters and opponents. The latter uses declarations that are in the locality of their first use.

In these simple examples, the difference in styles is really trivial; however, for functions that have (say) 100+ lines of code, using such 'local' declarations can enable a future reader of the code to appreciate the nature of a variable without having to 'scroll back up' to the the start of that function.

Adrian Mole
  • 30,672
  • 69
  • 32
  • 52
  • Another difference in your examples is that in the first example the variables are initially created and default initialized and then subsequently assigned to. Whereas in your second example they are only initialized (to their final value) and no subsequent assignment is taking place. That doesn't matter much for `int`s, but for some user defined types it can matter *a lot* (both performance wise, but also because they may not have assignment operators and thus wouldn't even *work* with example 1). Also; in example 2 all variables can be declared `const` which is not possible in example 1. – Jesper Juhl Aug 20 '20 at 16:31
  • @JesperJuhl Indeed! Although I didn't actually mention the issue of default initialization (or the lack thereof), I did *deliberately* use direct initialization in my second example to illustrate that it can be used *only* for such 'locally declared' variables. I could add that detail, or just leave these comments to cover the case (as it isn't *directly* connected to the OP's question). – Adrian Mole Aug 20 '20 at 16:35