It's about time I put my foot down. Programmers that say not to use While... Wend loops in VB6 are smoking the wacky weed. (here's one example, "The antiquated While..Wend loop shall not be used". Reminds me of when Joel Spolsky says, "As if source code rusted")

Look, the only advantage to using Do While... Loop is that you can break out of the loop prematurely. That's a bad coding practice, people. If I'm eying code at the bottom of the loop, debugging something sufficiently long, I might miss that you've Exit Do'd somewhere above. One might complain that loops shouldn't include something quite so big. Then just substitute a function call in the loop that I'm debugging and missing that exit in the loop instead.

The point is that exiting a loop in some random place is every bit as confusing as a GOTO; it breaks an easy, logical flow. I should be able to glance at a loop and reasonably assume that each operation on the same level of whitespace is executed. Logical flow is also a reason why you should never put Dim's (VB's method of declaring variables) mid-method. I shouldn't have to fish through your method's code to see what kind of object "tempX" is. Which is even more reason why you should use some sort of naming convention that explains what each variable is. Now I don't even have to glance at the declarations section to see what's going on in a line of code. And, of course, as implied above, whitespace should be used to show when branches can be taken and what level of code belongs where.

To sum:
1.) While... Wend encourages good programming practices. Use it. Have a really good reason when you don't.
2.) Don't use Goto. (For PolCorrect people: "Do use alternatives to Goto")
3.) Don't exit loops prematurely (there is an exception that proves the rule. I might talk about it later) (PC People: "Do complete each loop in its entirety, using If's to section logic within")
4.) Declare variables at the top of a method unless it's, for some screwed up reason, impossible.
5.) Use a naming convention that allows you to know what each variable's object type is at a glance.
6.) Use "hierarchical whitespace", daggummit.
7.) Getting me to talk about one "good" programming practice will, if I'm not careful, unleash hours of rules/Don't's.
8.) Summing is fun.

Labels: