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 codelen = string2.size()...//more codeversussize_t str1len, str2len;str1len = string1.size()....//some codestr2len = string2.size()...//more codeAll are local variables within a function BTW.
Is it OK to use same variable to store similar stuff sequentially?
c++;c;coding standards
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 codedon'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.