-1

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?

  • 4
    (1) The loop counter i is a float in your 2nd variant. That's annoying. That also requires casting float to int when you index into the array, and when you compare the counter to length. (2) I also legally obligated to reference this code quality metric. – Nick Alexeev Mar 8 '19 at 1:56
  • I have a friend who used to write FORTRAN as his job. He once commented to me about how there are many ways of doing anything, because they kept adding new ways, but never removed the old ones. but there is one, and only one, thing they did remove - support for floating point test expressions in loops. There is a very good reason for this. – Baldrickk Mar 11 '19 at 9:31
3

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.

|improve this answer|||||
3

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.

|improve this answer|||||
  • One can have a block without introducing an extra-function, though this is the preferred solution when it is actually a useful abstraction, like here. – Deduplicator Mar 11 '19 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.