Why does code become hard to maintain? - Part 1
Code becomes hard to maintain when one change breaks unrelated things, so this article explains cohesion, coupling and the four pillars of object oriented design with a mutual fund example.
Why does code become hard to maintain? - Part 1
Almost all business software lives longer than we expect. A mutual fund platform which is built for one fund house today will be changed for new fund types, new fee rules and new regulations for many years, and most of the time the people who change it are not the people who wrote it. The first release is only the beginning of the cost, the real cost is paying for every change after that. When the code is hard to change, every small request becomes a risk, the team becomes afraid to touch it and the business pays for that fear. In this series, I share how to write maintainable code with the object oriented principles, and we will use one running example, a mutual fund system. In this article, we are going to see why code becomes hard to maintain, what is cohesion and coupling, and a quick recap of the four pillars of object oriented design which all other principles are built on.
Maintainable Code series:
- Why does code become hard to maintain? - Part 1 (this article)
- How to keep the code simple and the objects in charge? - Part 2
- What are the Single Responsibility and Open Closed principles? - Part 3
- What are the Liskov Substitution and Interface Segregation principles? - Part 4
- How to use Dependency Inversion to loosen the coupling? - Part 5
TL;DR Summary
- Maintainable code is code which is easy to understand, easy to change and easy to test.
- Code becomes hard to maintain when one change breaks unrelated things, and that happens because of low cohesion and high coupling.
- Cohesion - How strongly the members of one class are related to each other.
- Coupling - How much one class depends on the other classes.
- Aim for high cohesion and low coupling, every principle in this series is a way to get there.
- The four pillars, Abstraction, Polymorphism, Inheritance and Encapsulation (APIE), are the base. The design principles are built on top of them.
Why does code become hard to maintain?
Nobody writes hard to maintain code on purpose. It happens slowly, one quick fix at a time. Let us take a closer look at how it goes in a mutual fund system.
The first version of the system calculates the Net Asset Value (NAV) of a fund and prints a statement. After few months, the fund house asks for an email alert when the NAV is published, then a new expense ratio rule, then a new fund type. Each request is small, so we add the code to the same class where the related code is already available, because it is the fastest way.
After a year, a single class calculates NAV, applies fees, prints statements, sends emails and reads the configuration. Now a small change in the statement format breaks the NAV calculation, because both of them use the same private fields. The test of the NAV calculation needs a mail server, otherwise it can’t run. Nobody understands the whole class, so nobody wants to touch it.
Here is the consequence chain,
- The code has many responsibilities in one place, so a change for one reason breaks other things.
- Developers become afraid to change it, so they add more code around it instead of fixing it.
- Bugs go to production, the customer finds a wrong NAV before we do, and we are in reactive mode.
- Releases become slow, the cost of every change goes up, and the business loses its confidence in the system.
We need to be proactive here. Instead of waiting for the pain, we should design the code in such a way that change is cheap.
What is maintainable code?
Maintainable code is code which can be understood, changed and verified by a person other than the author, in a short time and with low risk. In simple terms, it is the code which welcomes the change.
It has three important qualities,
- Easy to understand - A new developer can read a class and say what it is for, without reading the whole system.
- Easy to change - A change is made in one place, and it doesn’t ripple to many other places.
- Easy to test - A class can be tested alone, without a database, mail server or web server.
All three qualities depend on two ideas, cohesion and coupling.
Cohesion and coupling
Cohesion tells us how strongly the members (methods and fields) of one class are related to each other. In a highly cohesive class all the members work together for one purpose. In a low cohesive class the members have nothing to do with each other, they just happen to live in the same place. If the members don’t use the same fields at all, the class has no cohesion.
Coupling is the other side of the coin, it tells us how much one class knows about and depends on the other classes. When the coupling is high, a change in one class forces changes in all the classes which depend on it.
The following diagram depicts the difference between low and high cohesion.

Find below the same idea in code,
// Low cohesion - the members have nothing in common
public class FundHelper
{
public decimal CalculateNav(Fund fund) { /* ... */ }
public void SendEmail(string to, string body) { /* ... */ }
public string PrintStatement(Folio folio) { /* ... */ }
public string ReadConfigFile(string key) { /* ... */ }
}
// High cohesion - every member works for the NAV
public class NavCalculator
{
public decimal SumMarketValue(IEnumerable<Holding> holdings) { /* ... */ }
public decimal SubtractLiabilities(decimal assets, decimal liabilities) { /* ... */ }
public decimal Calculate(decimal netAssets, decimal units) { /* ... */ }
}
Our target is always high cohesion and low coupling. A simple pulse check, if you can’t name a class without using the word “and” or generic words like Helper, Manager and Util, most probably the cohesion is low.
Recap of the four pillars (APIE)
Before we go to the design principles, let us recap the four pillars of object oriented design, because every principle in this series is built on them. I discussed these in detail in the Object-Oriented Thinking series, refer the links in each pillar to read more.

Abstraction
Abstraction defines the essential features of a class or an object. It uses two steps, reduction removes the unnecessary details, and generalization finds the common and important features which are exposed to the outside world. In simple terms, abstraction tells what it can do, the implementation tells how it is done. For a Fund, the investor needs to know the name, the NAV and the way to purchase, he doesn’t need to know which broker the fund manager used. Refer to this link for more information.
Encapsulation
Encapsulation binds the data and the functions together as a single unit, and hides the information which others don’t need. It complements the abstraction by hiding the unnecessary details, and it separates the concerns. The units of a Folio are changed only by the Folio itself, otherwise any code can put the folio into a wrong state. Refer to this link for more information.
Inheritance
Inheritance creates a new class on the top of an existing class, and it is used for the reuse of the implementation. It is an “is a” relationship, an OpenEndedFund is a Fund. Use it carefully, because it is the strongest coupling between two classes. Refer to this link for more information.
Polymorphism
Polymorphism means a context dependent behavior, the same call behaves differently based on the object which receives it. There are two kinds, the subtype polymorphism is about “can do” (implementing an interface), and the subclass polymorphism is about “is a” (reusing the implementation). Every subclass can be a subtype, but all subtypes can’t be a subclass. We will use this difference again in Part 4. Refer to this link for more information.
Note: Knowing the four pillars is not enough to write a good design. You can use all four and still end up with a tightly coupled system. The design principles in the next articles tell us how to use the pillars in the right way.
What is next?
Find below the roadmap of this series, with the problem each principle solves,
| Principle | The problem it solves | Article |
|---|---|---|
| KISS, DRY, YAGNI | Unnecessary complexity and duplication | Part 2 |
| Tell, Don’t Ask | Logic scattered away from the data | Part 2 |
| Law of Demeter | Code which knows the structure of other objects | Part 2 |
| Single Responsibility | A class with many reasons to change | Part 3 |
| Open Closed | Editing old code for every new requirement | Part 3 |
| Liskov Substitution | Subclasses which break the parent’s promise | Part 4 |
| Interface Segregation | Fat interfaces which force unused methods | Part 4 |
| Dependency Inversion | High level code which depends on low level details | Part 5 |
Wrap up
In this article, we have looked at why code becomes hard to maintain, what is maintainable code, the meaning of cohesion and coupling and a recap of the four pillars of object oriented design. I really appreciate that you took the time to read this article. In the next article, I will walk you through the basic guidelines, KISS, DRY and YAGNI, and the two principles which keep our objects in charge, Tell Don’t Ask and the Law of Demeter.
Related Posts
How to keep the code simple and the objects in charge? - Part 2
KISS, DRY and YAGNI keep the code simple, while Tell Don't Ask and the Law of Demeter keep the logic near the data, explained with mutual fund redemption and NAV examples.
How to use Dependency Inversion to loosen the coupling? - Part 5
Dependency Inversion is explained with layered and multi-implementation examples, dependency injection, IoC containers, testing benefits and a summary of all the principles.
What are the Liskov Substitution and Interface Segregation principles? - Part 4
Liskov Substitution and Interface Segregation are explained with open-ended funds and fixed maturity plans, including the warning signs of a broken hierarchy and how to fix it.