A reusable variable: still good to define in the scope that uses it?

In summary, the main argument for the second way of writing the code is that it is equivalent and more efficient as the variable c does not need to be initialized during every iteration of the while loop. However, this is not a valid argument as it does not make a difference in terms of efficiency after compilation. The first approach is preferable as it follows the best practice of defining variables in the smallest scope possible. Additionally, it is recommended to initialize variables even if they are expected to run at least once, to prevent any potential issues.
  • #1
SlurrerOfSpeech
141
11
I'll give you an example.

Let's say I have a snippet of code like

Code:
   while(*s2)
   {
       char* c = s1;
       while(*c++==*s2++);
       if(!*c)
       {
          flag=true;
          break;
       }
       else
       {
          flag=false;
       }
   }

(which is a real snippet of code I have).

One might alternatively write it like

Code:
    char* c;
   while(*s2)
   {
       c = s1;
       while(*c++==*s2++);
       if(!*c)
       {
          flag=true;
          break;
       }
       else
       {
          flag=false;
       }
   }

The main argument I can see for the first way is

"The variable c is only used in the while loop and therefore should be defined there as best practice."

The main argument I can see for the second way is

"It's equiavlent and more efficient because you don't have to initialize c during every iteration of the while loop."

What are your thoughts?
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
SlurrerOfSpeech said:
The main argument I can see for the second way is

"It's equiavlent and more efficient because you don't have to initialize c during every iteration of the while loop."
This is not a correct statement. In both cases, what changes is where c is declared, not initialized. After compilation, there will be no difference between the two pieces of code. The first approach is preferable.
 
  • Like
Likes FactChecker
  • #3
You can verify DrClaude's claim easily at http://gcc.godbolt.org/. It is a very convenient tool for showing how your code maps to the machine.

(In many cases, it will also show just how much work your compiler does for you. This can give you confidence in writing the clearest code you can think of, and leaving micro-optimization to the compiler. It's usually much better at it than you. Modern optimizing compilers are remarkable.)
 
  • Like
Likes FactChecker and DrClaude
  • #4
The only advantage for c being defined outside of the loop is if you need to reference later on outside of the loop.

In either case, the code you've shown gives me a headache as you have to remember the operator precedence for the more arcane C operators and its easy to get it wrong. I especially hate the expression: *c++==*s2++
 
  • Like
Likes Samy_A
  • #5
jedishrfu said:
I especially hate the expression: *c++==*s2++
Good lord. Yeah, at least get some parenthesis in there.Uhh... your code is incorrect. That can also go pass the null terminator easily if the strings are the same length.
 
  • Like
Likes jedishrfu
  • #6
DrClaude said:
This is not a correct statement. In both cases, what changes is where c is declared, not initialized. After compilation, there will be no difference between the two pieces of code. The first approach is preferable.

Sorry, the mord I meant to use is instantiated.
 
  • #7
jedishrfu said:
The only advantage for c being defined outside of the loop is if you need to reference later on outside of the loop
Bad idea, I could cause that to give you undefined behavior by setting s2 = ""; If your planning on using something outside of a loop that may run 0-n times, you should always initialize it. I like to initialize my variables outside of loops even if I'm 100% sure it'll run at least once, it prevents me from messing things up if I refactor it later.
 
  • #8
Hint: smashing things together in one line of code is all of these: cool, hard to read, more prone to problems.
So, what do you gain: cool? Not cool actually.

Quite simply: Don't do that. The compiler will undo it anyway. Follow the advice @Integrand gave you.

Plus I would suggest very strongly that you learn the standard C library, one member of which does what it appears your code :nb) tries to do.
See if you can tell us which one that is. Try the string section, you know strcmp() and friends.
 
  • #9
It is a question of the well known software engineering "least privilege" principle: at every function or construct for procedural code and their counterparts in OOP, you must always give a variable the least chance to be modified by the rest of your code or so to speak, you always have to begin in local scope and expand to the "outside" only for very good reasons.
 
  • Like
Likes FactChecker
  • #10
newjerseyrunner said:
Bad idea, I could cause that to give you undefined behavior by setting s2 = ""; If your planning on using something outside of a loop that may run 0-n times, you should always initialize it. I like to initialize my variables outside of loops even if I'm 100% sure it'll run at least once, it prevents me from messing things up if I refactor it later.

I didn't mean not to initialize it only that if you need it later then you promote it to the next higher scope. I figured it wasn't apparent as often posters don't post all of their code.
 

What is a reusable variable?

A reusable variable is a named storage location in a computer program that holds a value that can be changed and reused multiple times within the same scope.

Why is it important to define a reusable variable in the scope that uses it?

Defining a reusable variable in the scope that uses it ensures that the variable is only accessible within that scope, making the code more organized and easier to read. It also prevents potential conflicts with other variables that may have the same name but are used in different scopes.

Can a reusable variable be defined in multiple scopes?

Yes, a reusable variable can be defined in multiple scopes, as long as it is not redefined or reassigned in a nested scope. This allows for more flexibility in the code and makes it easier to reuse the variable in different parts of the program.

Can the value of a reusable variable be changed?

Yes, the value of a reusable variable can be changed as many times as needed within the same scope. This is one of the main advantages of using reusable variables, as it allows for data to be updated and manipulated without having to create a new variable.

What are some best practices for using reusable variables?

Some best practices for using reusable variables include giving them meaningful and descriptive names, limiting their scope to where they are needed, and avoiding redefining or reassigning them in nested scopes. It is also recommended to initialize the variable with a default value to avoid errors.

Similar threads

  • Programming and Computer Science
Replies
4
Views
734
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
5
Views
883
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
11
Views
2K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
32
Views
2K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
18
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top