# Word-A-Day Challenge 2017

1 Post authored by: Patrick Hubbard

# Day 11 - Loop

Posted by Patrick Hubbard Dec 11, 2017

Loops are among the most sublime constructs of programming, but as it turns out, often consume our personal lives as well. Whether baked into the silicon of routing ASICs or beautifully parallelized in Go, loops are the mechanism by which we say, “Yes machine you’re done, now do it again”. Loops tirelessly execute routine business logic, evaluate access control policies, they ensure our backups happen on schedule and they pull resources in and out of production to suit demand. But we forget that they are also a metaphor, endlessly iterating, endlessly reminding us as their keepers, not to fall into life-wasting loops of our own.

Consider three loop patterns, in BASIC, Java and Go:

BASICJavaGo
100 REM GUESSING GAME
110
120 PRINT "GUESS THE NUMBER 1-100"
130
140 LET X = INT(100*RND(0)+1)
150 LET N = 0
170 INPUT G
180 LET N = N+1
190 IF G = X THEN 300
200 IF G < X THEN 250
210 PRINT "TOO LARGE, GUESS AGAIN"
220 GOTO 160
```// print the integers 10-19
for(int x = 10; x < 20; x = x + 1) {
System.out.print(
"value of x : " + x );
System.out.print("\n");
}```
```func main() {
for {
test := statusNow()
if test == 10 {
break
}
if test == 5 {
continue
}

fmt.Println(test)
time.Sleep(300 * time.Millisecond)
}
}```

At first glance, they are the same: keep doing something until a condition is met. However these aren’t the same, not one bit, and they tell the human stories of their creators. Left-to-right, you might think of them as Get Out Fast, Deterministic, and Eventual Exit.

### GOTO: Chaos Classic

In the case of BASIC, this loop expects to get out or at least is in the habit of falling-through. BASIC is like most languages of yore, procedural. It has first and last line, a beginning and an end, and all loops must end or the process can’t exit. GOTO came after conditionals, a mechanism to redirect the falling momentum of top-down execution. But it’s branch exception based on one particular condition- anything else and the process falls through. It’s also not technically a loop in the modern sense, GOTOs can go anywhere and the loop is actually a pattern not an operator.

The human equivalent of the BASIC GOTO loop is an interrupt driven life. You’re heading with determination in a direction, but issues pop-up and you have to go back to fix them. It’s two steps forward, one step back. In many ways it’s actually the most hopeful because it rewards tenacity, eventually leading to progress or transition. It’s also the diametric opposite is the For-Next Loop.

### For-Next: Precision Planning

Say what you will about strongly typed languages, but the stalwart For-Next loop in Java, C# and others will save you debugging pain. While it’s possible to mess with the iterator or otherwise engage in continue/break shenanigans, for is used because it’s deterministic. The code will execute precisely a known number of times only. At it’s most extreme it’s the human analog of a prison sentence, or at least week, semester, year or similar conventions.

There’s a certainty about For-Next- awareness of initial commitment and start conditions and it’s iterators provide feedback along the way that guides us to conclusion. Why is Friday great? Because the next value in the day iterator is Saturday. We use these loops as technologists to accomplish life change, or at least to provide time scaffolding to hang other milestones of personal development. With For-Next we know what we’re getting into, even it’s a bit routine along the way.

### While: Seductive, Soporific, Insidious

while then must be the ideal use of a computer, the ultimate hands-off delegation to automation. It’s an expectation that a thing will continue open-ended. In the GoLang example above for { may look like For-Next, but it’s not. No condition is being evaluated each loop- there’s no iterator. It just spins, printing integers to the console with a 300ms nap each pass. It continues or breaks on unrelated if’s. In other languages while contains a condition for escape, but not here. This use is the inescapable, even dreadful, while(true).

OS message and event loops are canonical monster examples, expecting to run without interruption until shutdown. The while loop’s momentum is not to run to a terminus like BASIC, and it’s also not deterministic like For-Next. while expects not to progress unless something exceptional happens. Its human analog literally, is to “while (wile) away your time”.

And in this time of IT transformation, failing to recognize one is trapped in a while is increasingly perilous. When I hear Ops teams say things like, “I guess this job is OK”, “Meh, SDN won’t affect me”, or “The datacenter will never go away”, I ask them what their day-to-day is like. Almost all fall into a narrow pattern of help desk misery, systemic underinvestment, nervousness about the future and lack of time for personal skills development. For them while is innocent enough- it’s emotional compensation to get though each day hoping there’s something out on the horizon.

Some in IT even subconsciously wait for radically new hardware, a layoff, re-org, or family relocation- anything that will force a major job change because they’re stuck. Usually in the past it’s worked out, but fortune without development is less guaranteed each year. Cloud and DevOps are brining unique changes that require significant skills upgrades. Many IT Jobs- at least the ones that will be interesting and rewarding- won’t be network configuration or application debugging. They’ll be something more. Something architectural, something analytical, something less expert and more inventive.

### GOTO: Neurons

Perhaps there’s irony in the BASIC example above. It’s old, limited and even in its agnostic Integer BASIC form terrifically uncool. But just perhaps, it might be the most compatible with our human, wet-CPU processors. Perhaps living a life of interconnected pointers and evolving spaghetti code in the wonderful synaptic sense offers the most resiliency, captures the most opportunities, and breaks away in the most important top-right quadrant of human endeavor: fun.

Beware settling for while. It’s process interrupts are usually unexpected.

By date: By tag: