Question

For example, sometimes I need a float variable to be used inside a for loop, e.g.:

float sum=0;
for(int i = 0; i < students.length; i++) {
    sum += Math.random();
    students[i].result = sum;
}

I feel uncomfortable that sum is accessible outside the for loop even it is used inside the for loop only, and my using language doesn't allow the for loop condition to have variables of different types, so I'm trying to put the sum into the start of for loop:

for(float i = 0, sum = 0; i < students.length; i++) {
    sum += Math.random();
    students[i].result = sum;
}

in order to limit the scope of sum, which also converts sum to float.

Is there any consequence to do so?

Was it helpful?

Solution

My opinion is that the first example you gave is the "right" way to do it. Why do I feel this way? Let's go over a few reasons.

Variable Scope

In this snippet that you have provided, it looks like it is all contained within a single function. As a result, none of the variables declared within the function are accessible outside of the function. Now, if sum were declared as a member variable (or field, in Java) to a containing class, your concern about accessibility may be more justified. But who else are you worried about having this value? No other object can touch it. It's even more private than a private member variable. Once the function returns, it dies...

Compilation

Eventually, the code written above gets compiled into machine code that the processor can run. You may be surprised to know that when this happens, both variables i and sum will live side by side in memory. Now scope-wise, the compiler will treat i and sum differently, but when they are finally compiled and running on the processor, i and sum will be neighbors.

Efficiency

In the second example, your iterator i is a float instead of an int. This means, your comparison, i<students.length and your increment, i++ are going to be less efficient for floats than they are for ints, because floating point arithmetic takes more processor cycles than integer arithmetic.

Maintainability

Software engineering ends up being an interesting problem to solve, because it all depends on what you are trying to optimize for. The code I work with often requires being optimized for readability so that when another software developer comes down a year from now, he can read what my code is doing with minimal confusion. The second example is much more confusing than the first – perhaps, if for no other reason, than it is significantly out of the norm, for very little benefit.

OTHER TIPS

Rather than trying to stick the variable sum in the loop, I recommend just packaging the entire loop in it's own function:

private static void assignStudentResults(Student[] students) {
    float sum = 0;
    for(int i = 0; i < students.length; i++) {
       sum += Math.random(); 
       students[i].result = sum;
    }
}

This prevents sum from being accessible anywhere else; it avoids the surprise (and inefficiency) of having an index variable that's a float; and it results in cleaner code.

Licensed under: CC-BY-SA with attribution
scroll top