Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

This question already has an answer here:

Say I have a variable name len in a function and several strings. Can I use this to store length of those strings one after the other or should I create separate variables? Basically this:

size_t len;
len = string1.size()
....//some code
len = string2.size()
...//more code

versus

size_t str1len, str2len;
str1len = string1.size()
....//some code
str2len = string2.size()
...//more code

All are local variables within a function BTW.

share|improve this question

marked as duplicate by gnat, Doc Brown, GlenH7, Karl Bielefeldt, sleske Oct 4 '13 at 13:24

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Based purely on experience: Use clear naming conventions and separate variables. It makes debugging much easier. It's a question quite the same as $i in loops. If you get a notice: "Undefined variable len" or "Undefined variable str2len" it makes it much easier to find it. –  Luc Franken Oct 4 '13 at 9:19
    
Interesting, i usually declare a new variable (mostly because its name will be more meaningfull increasing readability) in most of the cases, but i want a feedback from the community –  Kemoda Oct 4 '13 at 9:20
    
A use case or some context would go a long way to making your question more constructive. In some cases, what you suggest could be okay. In other cases, absolutely not. We don't have enough information about your intended use of this method to answer wether or not it is OK. –  GlenH7 Oct 4 '13 at 11:15

3 Answers 3

up vote 4 down vote accepted

If you are really going to introduce those variables as a shortcut for the expressions, and you cannot easily split up that code into two functions, I would definitely go for the second alternative. And I would write it this way:

 size_t str1size = string1.size()
 ....//some code
 size_t str2size = string2.size()
 ...//more code
  • don't use the name "len" when the original name is "size" (will be more consistent in naming)
  • declare and assign in one statement. That will help you to prevent a copy/paste error where you copy the first line down below and forget to change the second variable name.
share|improve this answer

I would use the same variable name but separate scopes to ensure that I do never accidentally use a length value of the wrong String

{
  size_t len;
  len = string1.size()
  ....//some code
}
{    
  size_t len;
  len = string2.size()
  ...//more code
}
share|improve this answer
1  
Wow that looks like an overkill :) –  Kemoda Oct 4 '13 at 9:21
2  
@Kemoda: If you can't either separate the code in separate functions or eliminate the variable by inlining the expression (fully appropriate for most getters), I would definitely agree with this approach. –  Jan Hudec Oct 4 '13 at 9:22
1  
Guys, do you really create local scopes in a function on a regular basis? I have come across such code in the past, and first thing I did was making the function smaller, so eliminating the extra parentheses was safe and easy. –  Doc Brown Oct 4 '13 at 12:28
3  
Yes, @DocBrown, some of us really do. It helps us remember that those variable are SHORT-TERM, and it helps the compiler do live-variable analysis for register allocation. –  John R. Strohm Oct 4 '13 at 13:05
1  
@MrSmith42: well, I guess it may be a matter of taste, but I don't like such local blocks very much (at least, not as the final construct) - too much noise. But opinions may differ, and I was wondering why you got so many upvotes for this suggestion. –  Doc Brown Oct 4 '13 at 13:33

Usually it's rather confusing. However the second options you list does not look right either. Instead you should:

  1. For simple things like str.size(), just use that expression where you need it and don't store it in variable at all.
  2. Split the code out to helper functions, so you only process one string in one helper.
  3. At least limit the scope of the variables with a block (see MrSmith42's answer)
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.