Skip to main content

A Tale of Three Loops

This one has been cooking for a very long time. Like many professional programmers I have often wondered what is it about programming that is just hard. Too hard in fact.

My intuition has led me in the direction of turing completeness: as soon as a language becomes Turing complete it also gathers to itself a level of complexity and difficulty that results in crossed eyes. Still, it has been difficult to pin point exactly what is going on.

A Simple Loop

Imagine that your task is to add up a list of numbers. Simple enough.

If you are a hard boiled programmer, then you will write a loop that looks a bit like:

int total = 0;
for(Integer ix:table)
total += ix;

Simple, but full of pitfalls. For one thing we have a lot of extra detail in this code that represents additional commitment:

  • We have had to fix on the type of the number being totaled.

  • We have had to know about Java's boxed v.s. unboxed types.

  • We have had to sequentialize the process of adding up the numbers.

While one loop is not going to hurt anyone; real code is stuffed with them. There have been occasions (not many thankfully) where I have written loops nested to a depth of 7 or 8. Such code really does become impossible to follow; let alone to write.

A Functional Loop

In a functional programming language, there are two ways to accomplish the task. The apprentice's approach might be to write a recursion:

total(nil) is 0;
total(cons(E,L)) is total(L)+E;

While workman-like, for many instances a smarter way is to use a fold:


Apart from being more concise; the fold is higher-level: it abstracts away the machinery of the loop itself and it is also independent of the representation of the collection of numbers.

(That is assuming that you have a functional language with overloading).

What is really interesting in relation to my original thesis is that the fold expression is closer to a problem-solving representation of the task.

However, ask any functional programmer about their use of fold and you will likely encounter a fairly procedural interpretation of how it works and how it should be used. (Something about how it successively applies the + function to each element of the list accumulating the answer as it goes.)

I.e., fold is better than for; but is not good enough.

A Totalization Query

My third version of this would be familiar to any SQL programmer:

total X where X in L

I.e., if you want to add up the elements of the list, then say so!

This query — which is based on notation in the Star programming language — declaratively states what is required. Although it's form is a little too specific, a more realistic variant would be:

fold X with (+) where X in L

I argue that either of these queries is better than either of the previous solutions because it comes closest to the original description and makes the fewest assumptions about the nature of the collections or the arithmetic.

It is also much closer to a problem oriented way of thinking about the world. I would argue that more people — especially non-programmers — would be able to follow and even to write such a query than either of the earlier formulations.

Why is that?

The Homunculus

Traditional programming is often taught in terms of programs being sequences of steps that must be followed. What does that imply for the programmer? It means that the programmer has to be able to imagine what it is like to be a computer following instructions.

It is like imagining a little person — a homunculus — in the machine that is listing to your instructions and following them literally. You the programmer have to imagine yourself in the position of the homunculus if you want to write effective programs.

Not everyone finds such feats of imagination easy. It is certainly often tedious to do so.

Popular posts from this blog

Minimum Viable Product

When was the last time you complained about the food in a restaurant? I thought so. Most people will complain if they are offended by the quality or service; but if the food and/or service is just underwhelming then they won't complain, they will simply not return to the restaurant. The same applies to software products, or to products of any kind. You will only get negative feedback from customers if they care enough to make the effort. In the meantime you are both losing out on opportunities and failing your core professional obligation. Minimum Viable Product speaks to a desire to make your customers design your product for you. But, to me, it represents a combination of an implicit insult and negligence. The insult is implicit in the term minimum. The image is one of laziness and contempt: just throw some mud on the wall and see if it sticks. Who cares about whether it meets a real need, or whether the customer is actually served. The negligence is more subtle but, in the end,

Comments Should be Meaningless

This is something of a counterintuitive idea: Comments should be meaningless What, I hear you ask, are you talking about? Comments should communicate to the reader! At least that is the received conventional wisdom handed does over the last few centuries (decades at least). Well, certainly, if you are programming in Assembler, or C, then yes, comments should convey meaning because the programming language cannot So, conversely, as a comment on the programming language itself, anytime the programmer feels the imperative to write a meaningful comment it is because the language is not able to convey the intent of the programmer. I have already noticed that I write far fewer comments in my Java programs than in my C programs.  That is because Java is able to capture more of my meaning and comments would be superfluous. So, if a language were able to capture all of my intentions, I would never need to write a comment. Hence the title of this blog.

In Praise of Crappy Code

Not all code needs to be perfect! This is pretty heretical thinking for a software engineer. The issue is simple: how do you go about developing software for a small fixed budget. Imagine that you have $500 to implement a solution to a problem. If you spend more than that you will never recoup the extra that you spent. This comes up a lot in systems integration scenarios and also in customization efforts. Someone wants you to 'tweak' an application that they are using; you know that no-one else would want that feature and that if you spend more than what the customer will pay you will end up losing money. From the customer's perspective, the common 'time and materials' approach to quoting for software development is a nightmare. Being able to offer a fixed price contract for a task is a big benefit for the customer. But, how much do you quote for? Too much and you scare the customer away. Too little and you lose money. This is where 'crappy code' com