Writing Modular Code is an Art of Programming

PySphere R&D Lab
3 min readMay 17, 2023

--

In Enterprise products, writing reusable code is a key to make your product reliable, testable and maintainable for long run. Modular and reusable code sits at the very heart of any standard product.

Let’s take an example to understand how we can convert and existing code into more modular and reusable code.

Let say there exist a method/logic in the product which take an 1-D array of positive integers as input and evaluate the sum of all the elements.

// Method signature
// Business: Each element of array holds the amount of total transaction done by the user on each day for a year. Hence the array length is 365.(ignore leap year)
int sumYear (int [] A) {
// logic
sum of elements from index 0 to last index (364) of array A
}

This method actually evaluates the total transaction amount of a user in a given year. Consider that this was the initial business requirement and hence code has been written in such a fashion.

Now, let’s say a new business requirement has come, which wants to evaluate the user transactions of a the first half of the year. How you are going to achieve it? Maybe writing one more method which can evaluate for first half of the year. Correct. Let see how that method looks like.

// Method signature
// Business: Each element of array holds the amount of total transaction the user has done on each day for a year. Hence the array length is 365.
int sumHalfYear (int [] A) {
// logic
sum of elements from index 0 to last index (182) of array A (ignore leap year)
}

Cool we have done it. :)

But can it be done in any other way, or may be a better way. Let see.

We can actually write a more generic/reusable method which can give the sum of transactions for a given period. Something like this

// Method signature
// Business: Each element of array holds the amount of transaction the user has done on each day for a year. Hence the array length is 365.
// startIndex: Starting index to consider for evaluation
// endIndex: Ending index to consider for evaluation
int sum (int [] A, int startIndex, int endIndex) {
// logic
sum of elements from index "startIndex" to "endIndex" of array A (ignore leap year)
}
Now we can call this reusable method for both of the existing requirement.
// Method signature
// Business: Each element of array holds the amount of transaction the user has done on each day for a year. Hence the array length is 365.
int sumYear (int [] A) {
//sum of elements from index 0 to last index (364) of array A
return sum(A, 0, 364);
}
// Method signature
// Business: Each element of array holds the amount of transaction the user has done on each day for a year. Hence the array length is 365.
int sumHalfYear(int[] A) {
//sum of elements from index 0 to last index (182) of array A (ignore leap year)
return sum(A, 0, 182);
}

And even any future requirements, lets say for the month of January, we need to simply call same method like this

// Method signature
// Business: Each element of array holds the amount of transaction an user has done on each day for a year. Hence the array length is 365.
int sumJanuray (int [] A) {
//sum of elements from index 0 to last index (30) of array A
return sum(A, 0, 30);
}

Why we are looking for better way? may be to accommodate future requirements and reducing line of code.

What we achieved:

1. Higher Usability

2. Higher Maintainability

3. Lesser lines of code

4. Less prone to future regression due to business logic modification

Vikram Singh Chouhan

--

--

PySphere R&D Lab
PySphere R&D Lab

Written by PySphere R&D Lab

We are passionate about transforming businesses through the power of technology. Trusted partner in the journey towards automation and digital transformation

No responses yet