jasonbutz.info

Connascence - The Language You Have Been Needing in Code Review

Connascence, Code Review

Today I was attending Indy.Code() and I attended a single talk that I think will make the entire time at the conference worthwhile. The talk was A Physics of Software Design given by David Greg of the Kenzie Academy. The talk was about connascence, and I wish I had seen a talk like this years ago.

Wikipedia says:

Connascence is a software quality metric invented by Meilir Page-Jones to allow reasoning about the complexity caused by dependency relationships in object-oriented design much like coupling did for structured design.

Two pieces of code are connascent if a change in one would require changes in another for everything to remain correct. Connascence provides a language discussing coupling. Connascence is not necessarily a bad thing, without it your code wouldn’t make much sense and likely wouldn’t do much. Putting pieces together is a large part of building software.

Connascence is made up of three axes:

  • Strength
  • Degree
  • Locality

Stronger connascences are harder to find and refactor. An entity that is connascent with higher numbers of other entities has a higher degree of connascence. Entities that are closer together in the code base are able to withstand higher connascence than entities further apart, that is they have a higher locality.

There are currently nine agreed-upon types of connascence. The diagram below was created by Kevin Rutherford of XP Surgery. It shows the types and their generally accepted strength or “severity”.

Types of connascence sorted by strength

Connascence of Name is when multiple entities must agree upon the name of an entity. This type of connascence cannot be avoided, but it can be minimized. For example, If you have a Car class that has an engine property that is the type Engine which has a start method. The Person class wants to start the car. Instead of having the Person class know about the methods of the Engine class, let the Car class handle that and expose it’s own start method.

Connascence of Type is when multiple entities must agree upon the type of an entity. For example, think about a number in an accounting application. Is the number a Double or is it a Float? Many times more than two decimals are used with accounting, especially when currency conversion is involved.

Jumping around, Connascence of Execution is when the order of execution of multiple entities is important. Think about it as a state machine.

I’m not going to run through and summarize every type, connascence.io does a great job.

If you take a step back and think about the code smells that you identify during code review many of them will come back to connascence.

Meilir Page-Jones initially coined the term connascence in an ACM article and then later included a section on it in his book What every programmer should know about object-oriented design. Jim Weirich, a Hoosier and the man who created the Rake tool for Ruby, was a major proponent of teaching people about connascence. He frequently spoke at conferences about connascence. Unfortunately, he passed away in 2014, since then other speakers have taken up his mantle and are continuing to spread the word. Below are links to talks on YouTube focusing on connascence.

Jim Weirich, The Grand Unified Theory of Software Development

Jim Weirich, Connascence Examined

Thomi Richards, Connascence in Python

Josh Robb, Connascence & Coupling (Java)