BrainBrain Duplication in a simple sum function
Something I said

Duplication in a simple sum function

by edA-qa mort-ora-y on Monday July 07 2008 @ 20:28:15 (25/-7920 Points)

Computing ↪Technology ✑ Reply ✓ Stick It ✗ Ditch It ⚐ Tag It

Recently, while proponing my zero redundancy method, a friend observed that to a functional programmer even the below piece of code contains duplication.

List::sum() {
  int total = 0;
  foreach( value in values )
    total += value;
  return total;
}

Now in general I don't like dealing with trivial examples. It's like attempting to describe the theory of relatively using an atom as an example: it just doesn't work. But let's humour this for a second and see what we can find out.

We will assume the language being used doesn't have a sum function on this data type. Otherwise this is a very simple duplication requiring a very simple adjustment, and is not really of any interest. We will also assume there is no other sum logic in the overall program; that is, we assume the programmer has already replaced all such duplication with a call to this function. Thus we can focus strictly on this function as presented.

Our first question of redundancy is whether the language exposes a fold operator or function. If yes, then one might argue there is some wasted coding, for the same function could be expressed as below.

List::sum() {
  return fold( +, values );
}

This looks better, and for the sum function it likely is. But you have to be quite careful here: this is not the exact same as the function we've replaced. The initial function gave an explicit ordering to the individual additions, the new function does not. Perhaps that order was there for a reason. To make this clearer, suppose we had a division sequence instead.

List::quotient() {
  int q = values[0];
  foreach( value in values[1..*] )
    q /= value;
  return q;
}

Division is not associative, therefore the precise ordering of the calculations is important. Simply replacing this with a fold operation is wrong, as it gives no indication of the ordering. If you are lucky then perhaps your language specifies a left fold, which is the type you need here. If not, then there is little you can do to reduce this code.

In general though, doing such small reductions is not really worth the effort. Considering the overall scope of a normal program this would be like picking pebbles out of a landslide. Furthermore, the above code is a leaf-node: it has no further internal dependencies (it doesn't call any of your functions). Such leaves always get special status with regards to reduction, as they enter the realm of cost benefit analysis, but that, is truly a different topic.

Nice summary. I completely agree that it is a very minor issue. Yet in software shops, people get into serious arguments over things like this. If you don't have a language with built-in fold/fold_left functions, should you bother to create your own? I personally consider that to be overkill. I just don't care about the minor problems that may arise from someone forgetting to return a value. Besides, I'd rather see people writing unit tests for their functions than for them to rely on language features. After all, you can always forget to call return in front of the fold function invocation itself, too, and if your languages behaviour is undefined in that case, you have exactly the same problem as if you forget to return at the end of a loop.

by Vlad on Tuesday July 08 2008 @ 01:03:24

That gives me an idea for another blog entry: why do people assume that because something is part of the language, or a 3rd party module, that it works correctly? I've seen enough bugs in compilers and libraries to know that certainly isn't the case.

by edA-qa mort-ora-y on Tuesday July 08 2008 @ 18:28:20

It's occured to me that one way of ultimately removing duplication is to program in a purely functional style. In this style there would be no loops at all. An application would start with one function call and spread from here into a chaining of more function calls. On one hand, using this approach one can make one's programs perfectly "orthogonal." However, I don't think I would enjoy actually working with code like this. My question is: Are there other ways to achieve this orthogonality nirvana? Are they equally unappealing to me as an everyday programmer? Perhaps leaving a certain amount of duplication in code is what makes it sane to the vast majority of regular people.

by Vlad on Tuesday July 08 2008 @ 20:55:15

Simply using a functional language will not remove redundancy, and it will add a different set of redundancy.

The only way to completely remove redundancy is through using multiple languages for a project. Each part of the project needs to be written in a language best suited for that domain.

I've seen many people also try to make the claim that somehow functional programming fixes everything. But that is just a load of crap and those people are just deluding themselves. I'll write a blog about it sometime...

by edA-qa mort-ora-y on Wednesday July 09 2008 @ 20:52:48

© 2008-2010 edA-qa mort-ora-y
Using Persephone and TestPlan
Tag: