- 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();
}
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();
}
