4

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.

Akilan
  • 151
  • 4
  • 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 04 '13 at 09: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 04 '13 at 09: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. –  Oct 04 '13 at 11:15

3 Answers3

6

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
}
MrSmith42
  • 1,041
  • 7
  • 12
  • 1
    Wow that looks like an overkill :) – Kemoda Oct 04 '13 at 09: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 04 '13 at 09: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 04 '13 at 12:28
  • @Doc Brown: Sometimes I do. If the block is very small, I use it as a step before deciding if the block is worth to be extracted as a method. – MrSmith42 Oct 04 '13 at 12:50
  • 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 04 '13 at 13:05
  • @MrSmith42: so you use this as an intermediate step before eliminating the block again - then you may add that for clarification. – Doc Brown Oct 04 '13 at 13:14
  • @DocBrown: It's not always an 'intermediate step'. If the block is not worth to be extracted as a method, it keeps staying in place e.g. to show that the scope of a variable is limited to the block or to show that this is a candidate for extract method in the future, if the block increases size while implementing new features / bugfixes. – MrSmith42 Oct 04 '13 at 13:22
  • 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 04 '13 at 13:33
4

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.
Doc Brown
  • 199,015
  • 33
  • 367
  • 565
3

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)
Jan Hudec
  • 18,250
  • 1
  • 39
  • 62