If you implement something you will write a lot of loops. Within the loops you will often use variables. This is a very common issue but it isn’t that easy like it looks in the first moment. Because you have to make one decision: whether you define the needed variable(s) inside or outside the loop.
// define variable outside of loop MyClass myInstance; for (int i = 0; i < n; i++) { myInstance = DoExecute(); } // define variable inside of loop for (int i = 0; i < n; i++) { MyClass myInstance(DoExecute()); }
I have heard many arguments, pros and cons about both approaches. Therefore I want to use this article to write about the most important aspects of this decision. I think the most important aspects are: readability, maintainability and costs (speed and memory). These are basic criteria which you can use for any kind of design decision.
Readability
In my opinion both implementations are coequal.
I already have heard the argument that the definition outside of the loop is less readable because if you read the assignment you sometimes have to have a look at the variable definition and therefore scroll up until you will find it. This may be necessary to understand what kind of variable we have. But there is something wrong with this argument. The root cause of the issue isn’t the question where we define the variable. There are two other design issues. At first your variable shall have a name which makes it readable without the need to know its type. And second you loop and your function shall not contain that much code that you have to scroll.
Maintainability
I think there is one difference between the both implementations which may have an influence to the code maintainability. The difference is the scope of the variables. In case you define the variable outside of the loop in can be accessed in a bigger scope. And therefore the variable which was created to use it within the loop only may be used in other parts of the function and therefore creates dependencies and reduces the maintainability of the source code.
But unfortunately this argument isn’t that solid too. If this issue occurs, your function will probably do more than one thing. If a function does one thing only the function wide scope of the variable should not be an issue.
Costs
As we have not seen any important differences so far we will now look at the costs of the different implementations. So we want to think about memory usage and execution speed.
One common guideline says: “Define a variable when it is needed”. The thought behind this guideline is that you don’t define a variable which will maybe never be used. A function may return early in case of an error or in case you have explicit return statements for example in parameter checks at the beginning of the function. If you define a variable prior to this possible function returns it may happen that it is never used. Therefore it’s a waste of time and memory to define the variable before it is needed. In the case of the loop the probability of this use case is small. Parameter checks which may lead to an early function return should already be done. But if you execute some functions within the loop which may throw an error, this argument will become relevant. Therefore with respect to this common development guideline we shall define the variable when it is needed and prefer the definition inside of the loop.
At next I want to think about the execution speed. In terms of operations the two approaches will create the following costs. (‘n’ is the number of loop iterations)
Variable definition outside of the loop | Variable definition outside of the loop |
1 constructor
n assignments 1 destructor |
n constructors
n destructors |
These costs of constructors, assignments and destructors depend on the programming language and on the variable type. Therefore it is not possible to say you shall always use the first or second approach. But if you implement the loop you may think about the used object. Is it a large object which needs to manage resources? In this case construction and destruction may be very expensive. Or is it a lightweight object with small construction cost? In this case construction and destruction may be very fast.
Summary
In terms of readability and maintainability you should prefer to define the variable inside the loop. The arguments for this decision are loose and therefore the main aspect is regarding execution costs.
If you don’t have any performance issues or if you don’t know the costs of the destructor, constructor and assignment of the object, you shall also prefer to define the variable inside the loop.
Only if you are dealing with a performance sensitive part of your application and you know that the constructor-destructor pair costs more than an assignment, you shall prefer to define the variable outside of the loop.