• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Programming practice: assignment statements inside loop conditions

In the data structures book I'm reading they mention that it's somewhat controversial to include assignment statements inside loop conditions in terms of good programming practice. How do you feel about it? Do you think it makes code too difficult to read? Is it worth it to make code more concise?

I personally think the pros outweigh the cons and I use the practice. I'm not sure which languages allow it and which don't but I'm currently learning Java as a first language. I'm confident in both methods so adapting to other languages shouldn't be a problem. Here is an example of what I mean in case you're confused.

BufferedReader in = new BufferedReader(new FileReader(file));

while ((name = in.readLine()) != null) {
....
}

Alternative:

name = in.readLine();
while(name != null) {
...
name = in.readLine();
}
 
I don't mind it for simple functions, and I think the order of evaluation is clear enough. Your example isn't hard to read at all.

Error handling can be an issue, naturally, but that's what exceptions are for.
 
Um, I don't have an answer as I'm a noob to computer science myself. But um...how did you get iamaelephant as your member status?
 
Sometimes clobbering variables inside conditionals is OK, e.g.:

while( fgets(buff,size,FILE*) ) { ... }

The above clobbers buff, and notice that we discarded the return value after we made our control decision. But, I belived explicit assignments in conditionals should be avoided for readability. If it was desirable to squirrel away the return value from the function, the function should be called in the loop body and the variable should be used in its place in the conditional, e.g.

int ret = fgets(buff,size,FILE*); // manual loop unrolling
while(ret) { ... ret = fgets(buff,size,FILE*); }

 
I like my code to be simple and readable... I never do things like that.
Of course I also never do the bracket on the end of the line either.
especially when {} get nested, it helps you see which part of the loop you're in.


bad code

int foo(double d) {
...
char nested_foo(bool b) {
...
}
...
}


readable code

int foo(double d, char c)
{
...
<indent> char nested_foo(bool b)
<indent> {
<indent> ...
<indent> }
...
}





WHY IS THE CODE EDITOR STILL BROKEN?
 
Why not just do this?

while(in.readLine() != null) {
name = in.readLine();
}

That's bad practice. It's not only harder to read it's non linear coding.
 
WHY IS THE CODE EDITOR STILL BROKEN?

It will never be unbroken, at least on this platform. On a different platform... in the future... many things may be possible.
 
Not recommended. Its against some industry standard rules e.g. Motor Industry Software Reliability Association (MISRA)
 
Originally posted by: Chosonman
Why not just do this?

while(in.readLine() != null) {
name = in.readLine();
}

That's bad practice. It's not only harder to read it's non linear coding.

It also wouldn't work, as the first line would be lost. ...

while(name = in.readLine()) {
...
}

That's perfectly acceptable as far as I'm concerned. Anyone who understands the concepts of a while loop and an assignment operator should have absolutely no trouble understanding what the flow of the code block will be.

This does, of course, assume that the return value of the assignment statement is equal to the right side of the assignment (which is the case in most modern languages, but I'm sure there are a couple in which that is no the case). If it's not the case, obviously this will not work.
 
Originally posted by: GodlessAstronomer
In the data structures book I'm reading they mention that it's somewhat controversial to include assignment statements inside loop conditions in terms of good programming practice. How do you feel about it? Do you think it makes code too difficult to read? Is it worth it to make code more concise?

I personally think the pros outweigh the cons and I use the practice. I'm not sure which languages allow it and which don't but I'm currently learning Java as a first language. I'm confident in both methods so adapting to other languages shouldn't be a problem. Here is an example of what I mean in case you're confused.

BufferedReader in = new BufferedReader(new FileReader(file));

while ((name = in.readLine()) != null) {
....
}

Alternative:

name = in.readLine();
while(name != null) {
...
name = in.readLine();
}

I'd definitely choose your first example in that case, by far. Well I'd do a minor change:

while (name = in.readLine()) {

}

But yeah, pretty much the same. To me it's very easy to read and I do stuff like that quite a bit, but definitely will break it down if the part in the while() or if() or foreach() line starts getting more complex.
 
Back
Top