Connascence— Part III: Connascence applied
Tags: Code Quality, Connascence, Clean Code, Software Development, Software Engineering

Before we start, let’s see our table of the different connascence types:

The image above shows all the connascence orderd from top to bottom. With the worse one on top and the good one at the bottom. The arrow shows in which direction we should refactor. In the following I’ll present to you code snippets I discussed in part II already and first identify what kind of connascence it is. Then I’ll refactor to a lower connascence and explain why it is better. Let’s start.

This code snippet is of type connascence of meaning, because problematic part here is the meaning behind it: What does 123.45 stand for? We know it’s money. But money isn’t equal money. Is it USD? Is it EUR? Is it YUAN? We don’t know.

The solution to this is quite simple. By creating a type Money we pass in the value and the currency. We have thus turned a connascence of meaning into a connascence of type:

Now there shouldn’t be any issues with the meaning. Looking at this and compared to the code before, you’ll notice that it is more declarative. If we look at the our connascence type list, you see that we moved the code type from connascence of meaning to connascence of type — we lowered the connascence type and thus make it objectively better. This is just one simple example how connascence can guide us to write better code.

You probably remember the code above: A function that has multiple parameters. If being called, the arguments have to be passed in the same order as the parameter list. We identified it as connascence of position.

The issue is with connascence of position here is that the order of the arguments is important for the code to run correctly. Once you switch the passed in arguments, the function will return an incorrect result. These kind of coupling can be resolved by creating a new object type and pass that object type as argument into the function:

The benefit that we now have is that the order of the passed in arguments, i.e. firstName , lastName , address , is now unimportant. You can now change the order and the function would still return the correct result. By creating a new object type, we have decoupled our code and thus made it better.

This is another example of starting from a high connascence type, i.e. connascence of position, and moved to lower connascencce type, i.e. connascence of type. We lowered the connascence type and thus improved our code.

Let’s have a look at our HTML/CSS code from the last article. We identified this connascence type as connascence of position. Take a pause here and ask yourself how you could improve this code and what connascence type would you receive at the end?

The problem here is obviously that the text color of the first child in the section can change if you change the structure of the HTML code, by e.g. adding another element as first-child into the section .

There are different ways how to mitigate this. One is by using BEM. You basically apply a class to the element, here the header , you want to style, here applying the color red to the text:

Now the HTML structure is more robust, because changing the HTML structure would have no effect to the text-color style of the header . We changed the code of connascence of position to connescence of name

Let’s go to our next example. It’s a test and consists of two classes. Both classes are using the value 50 . The test depends on that the class Checkout , respectively it’s instance, returns the number 50 . As you might have guessed this is connascence of value.

The first step of how to improve this code is reducing the locality: We change function scan so that it accepts the value as a second parameter:

You will notice that we still have connascence of value. But the code is still better as before, because the locality has changed from locally far to locally close, i.e. the value is now not spread out to two different classes but is only located in one class.

But we still aren’t there, yet. We still have connascence of value and we can improve this creating a new variable priceOfA and assign a random number to it. Now it is independent of the value and we have thus change the code into connascence of name — the best of all connascence:

Conclusion

Ok, cool. Now we know what connascence is. What gives?

For me, connascence is a cool new tool: