Multiple Classes with Single responsibility VS Single Class with Multiple Responsibilities

While taking a session with a set of mobile developers, one of them asked a question that made many of them curious.

Why multiple classes when single class can do?

  1. The main reasoning for single class was that one would be able to save some lines of code.
  2. Also, one would be required to create just one object for multiple functionalities.

Lets look at some of the key advantages of multiple classes, each one of them having single functionality.

  • High in cohesion, and hence reduced coupling. This tends to make the class reusable.
  • Lesser costly to change than single class. Following explains the cost structure related with a change:

Let’s say a class is performing multiple different functions. Mathematically, the class can be represented as following:

 Class A = f(x) + f(y) + f(z)

Let’s say, one needs to fix a bug related with f(x). Thus, one fixed the code in the class A that performed f(x).  The code was, then, shipped to QA engineers. QA engineers used to spend, let’s say, 1hour for each function. Thus, for a change in one function he spent 3 hours.

The total cost of change is 3 X (let’s say $20/hour as rate for QA engineer)  = $60.

  • On the other hand, if there were three different classes for three different functions, the cost of change would have been just $15. (Think How?)

Thus, it makes much more sense to split functionality into different classes as the change will be less costlier than following the approach of having multiple functionality in one class. The design leading to this approach (multiple class) could also be termed as “Cost-effective” design.

Latest posts by Ajitesh Kumar (see all)
Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.

Recent Posts

What are AI Agents? How do they work?

Artificial Intelligence (AI) agents have started becoming an integral part of our lives. Imagine asking…

2 weeks ago

Agentic AI Design Patterns Examples

In the ever-evolving landscape of agentic AI workflows and applications, understanding and leveraging design patterns…

2 weeks ago

List of Agentic AI Resources, Papers, Courses

In this blog, I aim to provide a comprehensive list of valuable resources for learning…

2 weeks ago

Understanding FAR, FRR, and EER in Auth Systems

Have you ever wondered how systems determine whether to grant or deny access, and how…

3 weeks ago

Top 10 Gartner Technology Trends for 2025

What revolutionary technologies and industries will define the future of business in 2025? As we…

3 weeks ago

OpenAI GPT Models in 2024: What’s in it for Data Scientists

For data scientists and machine learning researchers, 2024 has been a landmark year in AI…

3 weeks ago