A conversation with a colleague, which was originally sparked off by â€œC# BAD PRACTICES: Learn how to make a good code by bad exampleâ€ (an article about writing clear code), reminded me about a recent change I had just made. When I reflected back I began to consider whether I had just â€œdumbed downâ€ the code instead of keeping my original possibly simpler version. I also couldnâ€™t decide whether the reason I had changed it was because I was using an old idiom which might now be unfamiliar or because I just didnâ€™t think it was worth putting the burden on the reader.
The change I made was to add a simple retry loop to some integration test clean-up code that would occasionally fail on a laggy VM. The loop just needed to retry a small piece of code a few times with a brief pause in between each iteration.
My gut instinct was to write the loop like this:
int attempt = 5;
while (attempt-- > 0)
. . .
Usually when I need to write a â€œforâ€ loop these days, I donâ€™t. By far the most common use case is to iterate over a sequence and so Iâ€™d use LINQ in C# or, say, a pipeline (or foreach) in PowerShell. If I did need to manually index an array (which is rare) Iâ€™d probably go straight for a classic for loop.
After I wrote and tested the code I took a step back just to review what Iâ€™d written and realised I felt uncomfortable with the while loop. Yes this was only test code but I donâ€™t treat it as a second class citizen, it still gets the same TLC as production code. So what did I not like?
- While loops are much rarer than for loops these days.
- The use of the pre and post-decrement operators in a conditional expression is also uncommon.
- The loop counts down instead of up.
Of these the middle one â€“ the use of the post-decrement operator in a conditional expression â€“ was the most concerning. Used by themselves (in pre or post form) to just mutate a variable, such as in the final clause of a for loop , seems trivial to comprehend, but once you include it in a conditional statement the complexity goes up.
Hence there is no difference between --attempt and attempt-- when used in a simple arithmetic adjustment, but when used in a conditional expression it matters whether the value is decremented before or after the comparison is made. If you get it wrong the loop executes one less time than you expect (or vice-versa) .
Which leads me to my real concern â€“ how easy is it to reason about how many times the loop executes? Depending on the placement of the decrement operator it might be one less than the number â€œattemptâ€ is initialised with at the beginning.
Of course you can also place the â€œwhileâ€ comparison at the end of the block which means it would execute at least once, so subconsciously this might also cause you to question the number of iterations, at least momentarily.
Ultimately I know Iâ€™ve written the loop so that the magic number â€œ5â€ which is used to initialise the attempt counter represents the maximum number of iterations, but will a reader trust me to have done the same? I think theyâ€™ll err on the side of caution and work it out for themselves.
The solution I went with in the end was this:
const int maxAttempts = 5;
for (int attempt = 0; attempt != maxAttempts;
. . .
Now clearly this is more verbose, but not massively more verbose than my original â€œwhileâ€ loop. However the question is whether (to quote Sir Tony Hoare ) it obviously contains less bugs that the original. Being au fait with both forms itâ€™s hard for me to decide so Iâ€™m trying to guess that the reader would prefer the latter.
Given that we donâ€™t care what the absolute value of â€œattemptâ€ is, only that we execute the loop N times, I did consider some other approaches, at least momentarily. Both of these examples use methods from the Enumerable class.
The first generates a sequence of 5 numbers starting from the â€œarbitraryâ€ number 1:
const int maxAttempts = 5;
foreach (var _ in Enumerable.Range(1, maxAttempts))
. . .
The second also generates a sequence of 5 numbers but this time by repeating the â€œarbitraryâ€ number 0:const int maxAttempts = 5;
foreach (var _ in Enumerable.Repeat(0, maxAttempts))
. . .
In both cases I have dealt with the superfluous naming of the loop variable by simply calling it â€œ_â€.
I discounted both these ideas purely on the grounds that theyâ€™re overkill. It just feels wrong to be bringing so much machinery into play just to execute a loop a fixed number of times. Maybe my brain is addled from too much assembly language programming in my early years but seemingly unnecessary waste is still a hard habit to shake.
As an aside there are plenty of C# extension methods out there which people have written to try and reduce this further so you only need write, say, â€œ5.Times()â€ or â€œ0.To(5)â€ but they still feel like syntactic sugar just for the sake of it.
This is not the first time Iâ€™ve questioned whether itâ€™s possible to write code thatâ€™s perhaps considered too clever. Way back in 2012 I wrote â€œCan Code Be Too Simple?â€ which looked at some C++ code I had encountered 10 years ago and which first got me thinking seriously about the subject.
What separates the audience back then and the one now is the experience level of the programmers who will likely tackle this codebase. A couple of years later in â€œWill Your Successor Be a Superstar Programmer?â€ I questioned whether you write code for people of your own calibre or the (inevitable) application support team who have the unenviable task of having to be experts at two disciplines â€“ support and software development. As organisations move towards development teams owning their services this issue is diminishing.
My previous musings were also driven by my perception of the code other people wrote, whereas this time Iâ€™m reflecting solely on my own actions. In particular Iâ€™m now beginning to wonder if my approach is in fact patronising rather than aiding? Have I gone too far this time and should I give my successors far more credit? Or doesnâ€™t it matter as long as we just donâ€™t write â€œreally weirdâ€ code?
 In C++ iterators can be implemented as simple pointers or complex objects (e.g. STL container iterators in debug builds) and so you tend to be aware of the difference because of the performance impacts it can have.
 I was originally going to write while (attempt-- != 0), again because in C++ you normally iterate from â€œbeginâ€ to â€œ!= endâ€, but many devs seem to be overly defensive and favour using the > and < comparison operators instead.
 â€œThere are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.â€ â€“ Sir Tony Hoare.