in reply to Re4: Efficient Looping Constructs
in thread Efficient Looping Constructs
Isn't *looking* cleaner what we strive for when we attempt to write "clean code"?
At this point I think we've spent more brain cycles debating the readability difference of the two forms than may have ever been spent by those reading both forms of loop. But I would like to point out that I made a distinction between *looking* cleaner, and being easier to *read*.
Consider the case of indentation. Steve McConnel (in 'Code Complete', pp 409-410) refers to a study published in 1983 [1]. In this study, subjects scored 20-30% better on tests of comprehensibility when programs had 2 to 4 spaces of indentation than on programs with no indentation. No big surprise here right? However, it was interesting that while the lowest scores happened on programs with no indentation, the second lowest scores happened on programs with 6-space indentation. The interesting bit is that many subjects reported the 6-space indentation as being easier to use than the smaller indentations, but at the same time had lower comprehensibility scores with 6-space indentation. Thus, for many, the 6-space indentation *looked* better, but in fact it made the code less *readable* or comprehensible.
I am not aware of any studies comparing readability of the for-loop styles, and I am not suggesting what the results might be one way or the other. I am suggesting that a) what we *think* is more readable may not be the case, and b) I truly suspect that the readability difference (if there is one) is probably very low and would probably fall into the same category as the performace differences found above in this thread (ie: trivial in real life).
Without a real study, we are left with own viewpoints, and those of other experts who seem to pay attention to these very issues. In my view, the C-style for loop is not harder to read and understand, and I stated it was better than an equivalent while-loop because 'the key information is all together in one place'. You seem to have a very stong dislike for C's for-loop (even in C) to the point of recommending a while-loop instead. Your top three reasons against it come off as fairly subjective (cluttered, forced, eyesore). Your single non-subjective reason is that it breaks the semi-colon terminates a statement rule. I'm just guessing, but the tone of your responses suggest you wouldn't really like the for-loop much better if it used a different separator character.
The opinion of other experts seems to be against you in this respect. Steve McConnel (same book, p331) says that because the for-loop packages loop control code all in one place it makes for more *readable* loops, and makes *correct modification* easier [2]. Granted, he is contrasting it to other looping mechanisms and not to Perl's foreach form, but then your dislike of C's for-loop appears to go well beyond mere uses of such a loop in Perl, and thus stongly colors any argument you make about its readability in Perl.
[1] Miara, Richard J., et al. 1983. "Program Indentation and Comprehensibility." Communications of the ACM 26, no. 11 (November): 861-67 [2] He is talking about simple for-loops, not for-loops with extra non-loop-control-code packed into the header of the for-loop. But we aren't talking about those kinds of for-loop abuses here either.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re6: Efficient Looping Constructs
by dragonchild (Archbishop) on Oct 03, 2001 at 00:57 UTC |