2

I know that you can reuse variable names in C++, but I'm wondering whether I should. I'm not talking about reusing the variable itself, like with global variables. I'm talking about declaring and using a variable with a certain name in one scope, and then declaring and using another variable with the same name in a different scope. For example:

void Func1(int b) { /* Code here. */ };
void Func2(int b) { /* Different code here. */ };

void main()
{
    {
        int a=1;
        // Do stuff with this variable.
    }
    {
        int a=5;
        // Do different stuff with this variable.
    }
    int b=10:
    Func1(b);
    Func2(b);
}

It seems like doing this would allow for shorter variable names and allow copy and pasting of code without having to change the variable names, but it might cause confusion. Is there a style guide or generally accepted practice about when to do this?

I searched for a similar question to this and I couldn't find anything. I found some stuff about global variables, but that's not what I'm looking for.

CharType
  • 390
  • 1
  • 4
  • 16
  • 3
    This is probably better suited for [Software Engineering](http://softwareengineering.stackexchange.com/)? – UnholySheep Mar 17 '17 at 10:17
  • 2
    Generally accepted practice is to name your variables with meaningful names, so that whoever has to read your code doesn't have to do it 10 times to figure out what happens with certain variable, but has pretty good idea what is it for just by looking at name. – Tomasz Plaskota Mar 17 '17 at 10:20
  • @UnholySheep "name this $thing" is off topic on SE.SE – Caleth Mar 17 '17 at 10:21
  • If you can get from the context what the variable is, it's fine. For example `i` as an increment in a tight loop is ok. If the loop if over 10-20 lines though it's better to give it a more meaningful name. Basically it's not just variable name that matters, it's the context. – laurent Mar 17 '17 at 10:21
  • @Caleth the way I understand this question, OP is not asking for others to name his variables, but about general software development methodology regarding naming (using identical variable names in different scopes) - I am not familiar enough with SE though to understand if this is off-topic (opinion-based) or not – UnholySheep Mar 17 '17 at 10:24
  • 1
    @UnholySheep when referring other sites, it is often helpful to point that [cross-posting is frowned upon](https://meta.stackexchange.com/tags/cross-posting/info) – gnat Mar 17 '17 at 12:30

3 Answers3

1

You certainly can do that, as they are in different scopes.

If you want your variables to help explaining the semantics of the surrounding code, you should use appropriately named variables.

If the given variable is just used for counting or similar, then using a general name like 'i' or 'j' in all cases it's an accepted practice.

Edd
  • 1,350
  • 11
  • 14
1

The alleged benefit of shorter variable names may be of interest, but what is of greater interest is shorter variable scopes. Generally, the smaller the scope of the variable is, the better. Therefore, declaring variables within small scopes is a good practice, and introducing naked scopes purely for that purpose is good practice too.

Once we have gotten that out of the way, then we go to the question of whether it should be okay to reuse a variable name in different scopes.

I would say that the most important thing about choosing a variable name is:

The name of a variable should be chosen in such a way as to best describe what the value of the variable stands for.

Any other concern is secondary. This means that the best name for a variable within a scope cannot in any way depend on what variables exist in other scopes.

This in turn means: reuse variable names across scopes, it is perfectly fine.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
0

You should read more about Variable Scope in C++.

You can reuse the variables out of the scope.

There are some naming conventions in programming, google yourself. This is a hit I got from it. Naming conventions are different from language to language, company to company, etc. But most of them have common features such as naming variables to have a meaning.

Remember these naming conventions are there to make our life easier. Using the naming conventions will help to understand your code for another person (or your futureself, someday).

It's always the option you choose, easy way or the (relatively) correct way .

Community
  • 1
  • 1
ThisaruG
  • 3,222
  • 7
  • 38
  • 60