Connascence of Algorithm

Connascence of algorithm is the next level of connascence that we’re going to be looking at. We’re still in the static category of connascence, at level 4 (or 5, there seems to be some disagreement on the order of connascence of algorithm and connascence of position).

Connascence of algorithm is similar to connascence of meaning, though it tends to be more broad than agreeing on the meaning of one variable or value. Connascence of algorithm refers to when multiple components must agree on an algorithm used to perform a particular task- the obvious candidates for this are encoding, encrypting and decrypting, and hashing. There are also more subtle cases of connascence of algorithm, such as data validation.

Consider the following:

Wow. So much connascence of algorithm! Anyone would think it was an arbitrary example designed to prove a point…

Anyway, let’s take a closer look at the classes we have here. We have a Dinosaur that is created from DNA that has to be in a certain format. The dinosaur itself probably doesn’t care that much, but it makes it interesting for the other classes that rely on Dinosaur.

The DinosaurTransmitter and DinosaurReceiver are where the bulk of the connascence of algorithm lies. Our transmitter gets the plain DNA of the dinosaur and transforms the format into FASTQ for easier data transmission. We then encode the DNA into bytes with a Base64 encoder, using the bytes of the DNA in UTF-8 format. To ensure correct delivery of the entire payload, we’re also passing along an MD5 hash as a checksum.

And here lies our problem- The DinosaurReceiver is completely separate from the DinosaurTransmitter (Maybe even a different application completely), yet it has to know about each and every one of those algorithms- both individually, and the overall “transmission” algorithm, made up of all of those steps. We have to agree on using MD5 as the hashing algorithm for the checksum, we have to agree that the DNA is being sent Base64 encoded. We have to agree that the bytes being sent are UTF-8 encoded, and we have to agree that we’re using the FASTQ format to transmit the data, and the Dinosaur has to be created with plain format. If we change any of these encodings or hashing algorithms in one place, we have to change them in the other.

Another more subtle variation on connascence of algorithm is in data validation- where multiple software components must agree on the algorithm to use to ensure valid data. Let’s see what this might look like in action.

We’ve put safeguards in each of our application layers here, to make sure the name we’re assigning our dinosaur is valid. However, this has introduced connascence of algorithm- as if the validation algorithm ever changes in one place, it will have to change in all the others in order to remain correct and consistent across all layers.

Fortunately for us, connascence of algorithm can also be refactored into connascence of name in most cases. Let’s take our first example of transmitting a dinosaur. Consider the following changes:

We’ve extracted the majority of the connascence of algorithm into a DinosaurTransformer class, that the DinosaurTransmitter and DinosaurReceiver can refer to by name. The transformer still contains our connascence of algorithm in itself- the decode method must do the opposite of the encode method, but we’ve tried to extract as much of the algorithmic similarity into named concepts- both methods refer to exactly the same Charset, MessageDigest and DnaTransformer explicitly, reducing the connascence to connascence of name. The transmitter and receiver are now connascent with the DinosaurTransformer by name, but the connection they had with each other using the same algorithms in the same order is now broken. One must encode, the other must decode, and neither cares how it happens.

Our second example is more difficult to reduce the connascence- these are algorithms written in different languages for the same purpose, with no easy way of unification. And that’s probably ok- we eliminate as much connascence as we can given the constraints that we have, and remain conscious of the connascence that is left behind.

 

Author: Sean O'Toole

I like development and I like dinosaurs. Here we are.

Leave a Reply

Your email address will not be published. Required fields are marked *