# Algor Morphics

Algormorphics is the study of the effect of change on algorithms. This can be a change in the specs, environment or use and applies to classes and other software elements in addition to algorithms. Refactoring tends to make algorithms more robust in the face of change. As a simple example consider a max function over floats.

```  float max(float a, float b) {
return a<b ? a, b;
}

```
If the above function is called with say shorts

```  short a = 1;
short b = 2;
short c = 0;

c = max(a,b);

```
a lot of hidden activity occurs. The shorts are converted to floats, compared, and then reconverted back. In C this would be handled by a macro, but in C++ the better implementation is to use templates.

```  template <typename T>
T max(T a, T b) {
return a<b ? a, b;
}

```
While this avoids the hazards of macros, it is not without its own problems. This implementation is actually quite bad. To see why it helps to convert it to English, "Compare a copy of A to a copy of B, and return a copy of the larger." When the type is a class with a big ctor this is much more processing than intended, expected, or necessary. The correct solution is to use references

```  template <typename T>
T& max(T& a, T& b) {
return a<b ? a, b;
}

```
This code is now efficient and works for all types that define the '<' operator, but it still has problems. Imagine it being used in a sort routine on types where the less than operator is not defined so that total ordering is not possible. In other words, the type has subfields and only some of those are utilized in an ordering comparison. A typical example would be a record in a database. It is now possible for two objects to compare such that neither is 'less than' the other, yet neither are they equal. The way this max function is written however, says that if the first argument is not less than the second argument, return the second argument. When this implementation of max is used in a sort algorithm the sort is no longer stable. A stronger implementation is

```  template <typename T>
T max(T& a, T& b) {
return b<a ? b, a;
}

```
My intent in inventing this term is to contrast the study of algorithms, classical programming, with the challenge of writing code that is malleable in the face of change. The whole Agile, TDD, refactoring effort seems to be our latest attempts to deal with the effect of change on how to write clean clear code that works without constant surprises. I'd be interested in others opinions on the possibility of putting some meat on this term. As with design patterns part of the intent is to invent jargon that helps us talk about our field and the best practices we seek.

Just a thought in the middle of the night.

The above has nothing to do with the impact of change on algorithms per se. It has everything to do with sound or unsound implementation in a given language.

The term "algormorphics" appears to be an invention of the author. It does not appear in the literature, and the notion of a "study of the effect of change on algorithms" is questionable at best. A study of the effect of change on implementations of algorithms might have some validity (but probably doesn't warrant its own term), but the "effect of change on algorithms" is nonsense. Delete?

Yes it is an invented term. My intent is to explain it's usefulness. A little more time please? -- AllanGoff

Sure.

View edit of August 24, 2011 or FindPage with title or text search