Code Readability — Is it really matters?

Ramanathan Chockalingam
3 min readMay 21, 2021

--

A Big Yes! It matters a lot because if the code is readable, It will be very easy for the developers to understand the functional flows and the code itself acts as a document, and even it shortens the learning curve and debugging.

Any fool can write code that a computer can understand. Good programmers write code that humans can understand. — Martin Fowler

It is not that much easy to bring readability everywhere in the codebase but still, it’s a necessary thing to consider.

Improving code readability is a key thing that helps to maintain a healthy codebase. We can improve the readability by using meaningful names for methods and variables, naming conventions, design patterns, and etc.

But, When code loses its readability is really matters. On initial development or from scratch everything looks good and pretty clear but when the requirements started growing over on time and continuously updating the codebase without a thought on refactoring would eventually lead to disaster.

For the new developers who are going to do an enhancement or a bug fix on the codebase will be a nightmare and the same applies to the one who worked on the codebase earlier.

Unit testing, continuous refactoring, and code review are the key things will rescue from these kinds of disaster and it should be taken care as part of development itself instead of carrying it to later point in time.

Consider the below requirements given some point in the past for getting eligible promotion based on the customer attributes and see how the code evolves accordingly,

Requirement #1

a) Is the customer's birthday falls in the current month, not today and is he an existing customer?

Applicable Promotion: Birthday

b) Is today his birthday and is this his first visit to the store?

Applicable Promotion: Birthday and FirstVisit

Requirement #2

a) Is the Customer age greater than 55 and visited the store within 30 days?

Applicable Promotion: ElderPeople

Requirement #3

a) Is the customer revisiting the store after a year from the last visit at store?

Applicable Promotion: RevisitAfter365Days

b) Is the customer visiting the store on store authorized special occasions or any existing customer visiting on their wedding anniversary?

Applicable Promotion: SpecialOccasion

After completing three different requirements at different points in time, the promotion eligibility code is now a mess, without refactoring and arises a lot of conflicts for a new developer when the earlier requirements or documents are not available.

As a new developer to the code base, He would have the following questions when seeing the code

we are giving a birthday promotion for a customer when his birthday falls on current month but why we need to check his registration date for this promotion?

We are giving a birthday promotion on customer’s birthday but why the store’s last visited date should be null?

We are giving an Elder People promotion when the age is greater than 55 but what is the requirement behind checking the store’s last visited date with certain date in past?

Since Promotion and its criteria cannot be equated together to get back the initial requirements. The above questions arise due to missing requirements or poor documentation. These are the areas we need to focus on and we can resolve them by using extension methods.

Note: We can have promotion criteria methods in the customer class itself instead of having extension methods because we are validating the criteria with customer class attributes only, but having promotion criteria methods inside the customer class doesn’t relevant anymore since it belongs to promotion context.

We can refactor the promotion criteria to more meaningful methods by using extension methods and the final code would look like below,

Note: IsNotNull is a util method which checks the object existence to evaluate success or failure outcomes.

There are a lot of improvements done on the existing PromotionService code with the .NET extension methods and now it is looking way better than the earlier code.

Github Link: CodeReadabilityDemo Repository

Note: The main theme of the article is to explain the uses of extension methods in terms of code readability and still there is room to improve the PromotionService code but which is beyond the scope of this article.

Caution: Refactoring a codebase without unit tests would lead to breakages. Need to have a thorough plan, before refactoring legacy codebases.

Happy Readable Coding 😉😉

Thanks for Reading!!!

--

--

Ramanathan Chockalingam
Ramanathan Chockalingam

No responses yet