Programming practice: assignment statements inside loop conditions

Oct 27, 2007
17,009
5
0
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();
}
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
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.
 

BigPoppa

Golden Member
Oct 9, 1999
1,930
0
0
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?
 

presidentender

Golden Member
Jan 23, 2008
1,166
0
76
I do things like that, so long as another programmer (chosen at random from the people I know) can read it.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
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*); }

 

sao123

Lifer
May 27, 2002
12,653
205
106
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?
 

Chosonman

Golden Member
Jan 24, 2005
1,136
0
0
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.
 

ckwhy

Member
May 25, 2003
36
0
0
Not recommended. Its against some industry standard rules e.g. Motor Industry Software Reliability Association (MISRA)
 

drebo

Diamond Member
Feb 24, 2006
7,034
1
81
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.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Cuteness may work but the cost of maintainence and clarity is not worth it.
 

clamum

Lifer
Feb 13, 2003
26,256
406
126
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.