Showing posts with label simplicity. Show all posts
Showing posts with label simplicity. Show all posts

Sunday, May 9, 2010

The single return point hypothesis

For many people it seems natural that functions (or methods) should have a unique point of return. To me it seems like a constraint imposed on problem resolution implementation that has the danger of reducing the understanding of the code.


OTOH, I consider that avoiding multiple return points IS programming to please some notion of 'structured' or to simplify work for the machine. Code readability usually suffers with those constraints, and functional structuring "magic" does not always simplify it (I think). Maybe, that code is more 'unified' and easier to reason about by compilers, optimizers ... but not to be READ by (untrained) humans. There are such things as base / recursive cases, early or fast returns for some (human) reason ... Maybe you miss postconditions?

I conducted an informal survey of the "best" implementation of linear search over an array of ints in Java (a beginner example). As a measure of simplicity we took the number of JVM instructions needed to implement the algorithm.

Code samples: (May 2010)

A. University code (30 to 35 instructions)


public static boolean exists(int [] data, int value) {
boolean matched = false;
for (int i=0; !matched && i< data.length; i++)
matched = value == data[i];
return matched;
}


There are variations on this one: going up or down, putting an if to avoid updating matched when the value is still false. They all have in common the use of a local boolean and the mixing of array limits with the found logic.




B. Spartan code (24 instructions)


boolean matches(int[] array, int value, int limit) {
for (int i=0; i < data.length; i++) {
if (value==array[i])
return true;
return false;
}

boolean exists(int [] data, int value) {
int i=0;
while (i < data.length) {
if (value==data[i++])
return true;
}
return false;
}


The above code shows C mastery. Those that think Java can be learned without learning any C are deluding themselves.

C. Professional code (20 instructions)

public static boolean exists(int [] data, int value) {
return Arrays.asList(data).contains(value);
}


The above code shows the user knows Java but has not kept up-to-date (generics and arrays). You need to create your own List implementation backed by an int array ...


And where is the single return? It is the one the compiler uses (ireturn, to return a boolean, use javap).
Some people should go to the original articles to understand what structured programming was about.