Should I use variables explicitly?

It is rather a matter of style, because I know that in practice most compilers are probably optimized to give the same effect, but I continue to read that in general you should always declare / define variables in the scope that they are used. Therefore, in situations where I cannot embed an ad, for example, the following fragment, I thought about including index variables in parentheses for brackets (curly braces, I'm not sure what you name them in this case) in order to explicitly limit the scope of these variables. Is this a good practice? If so, could you explain why?

{ size_t i = 0; // this variable has no use outside of the range-based for loop for (auto const input : input_vector) { neuron_sequence[i].ForceSignal(input); ++i; } } 
+6
source share
4 answers

Of course, this is a good practice. It explicitly limits the use of this variable. I do this quite often. This definition is also used to force the start of some object destructors.

For instance:

 std::vector<int> v; v.resize( 10 ); // now holds memory for 10 ints 

How do you clear this memory? There is no function to call or any way to manually specify the vector v to clear its memory. The solution is to force it to go out of scope (assuming I used swap correctly):

 std::vector<int> v; v.resize( 10 ); // now holds memory for 10 ints { std::vector<int> temp; temp.swap( v ); } // temp goes out of scope and clears the memory that v used to hold 

Another common use is in switching cases. Often I need to create a temporary variable in the switch:

 switch( val ) { case constant: { int x = 10; // ... do stuff } } 

The last place I can recall from my head is writing scripts for some kind of code. When unit testing often happens, I just want to write my test code as quickly as possible without spending a lot of time on dev-time. So, I put a bunch of related tests in one function, but complete local variables in separate areas to make sure that I don't hit any weird errors (possibly exchanging iterators through tests).

+5
source

Yes, you must have variables explicitly:

  • Scope determines the lifetime of local variables. Thus, the definition of variable areas means that the variables are only active until they serve their intended use and are not just live and busy.
  • In the case of the same name variables, local vairables hide or shadow names called global. Thus, explicit disclosure of the area improves readability for the reader. (At least I feel it)
+3
source

For small data types, such as integers, you really don't need to worry about this because, as you said, compilers optimize the code based on the liveness variable and reach a specific location. In this case, it is rather a style problem. And I would recommend not to do this often, because code readability and ease of maintenance are just as important as performance.

However, for complex types, it may be useful to limit the lifetime in this way. For example, for a vector that internally allocates large amounts of memory, this can save some space if its size is limited in this way.

+2
source
  • Dense areas are good if you are concerned about the lifetime of the object - usually because of the used memory / resources or the desire to prevent accidental or facilitate the deliberate reuse of the same identifier
    • eg. performing some repetitive operations β€” perhaps even using macro substitutions β€” where temporary, but changing the name is not meant to be specific and tedious
  • You cannot always do this: you will encounter many situations when the maximum possible scope corresponding to one variable overlaps with another variable: for example, an attempt to "scope" a in { X a = 1; X b(a, 2); ++a; } ++b; { X a = 1; X b(a, 2); ++a; } ++b; destroys b too early
  • In some cases, creating arrays of small areas can greatly inflate the source code, making visual perception and support more difficult. There is some mental effort involved in verifying that the area you entered does not have control over the / for / while expression, and you can hardly see the general flow of functions. Of course, reducing the number of variables that remain in the scope further down can also reduce mental effort - this is a balancing act.

In general, it’s good to be selective, but you will develop a feeling when it is justified. If you're not sure, that probably doesn't matter.

0
source

All Articles