Is it always a good idea to divide large classes into smaller ones? [duplicate]

I’ve heard time and time again that in object-oriented programming, you should try to split objects that ‘do too much’ into multiple classes, to avoid the “God Object” problem.

This seems like fine advice for a project that has plenty of room to expand, but in our project, our packages are already loaded down with too many objects – some that are very bare-bones – while we also have the problem of very large objects that do too much.

Is it a better idea, for code sanitation, to split our larger objects that do too much work into smaller objects? Or is there a limit to the amount of good it can do?

7

It seems to me that the fundamental principle to apply here would be the Single Responsibility Principle. Does each class have a single, clearly articulated, well-bounded responsibility?

Note that I don’t mean “does each class do one thing.” For example, a repository “Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects.” But it still might have several methods that accomplish parts of this overall responsibility.

If you find that a clearly-articulated, well-bounded responsibility that should be contained in a single class with multiple methods is instead being split over many smaller classes, then your classes are getting too small.

5

I assume you’re on a legacy project where you weren’t the one who developed the code. I am in a similar situation.

In my opinion, don’t touch the code unless:

  1. You have been tasked with the refactor by a superior.

  2. You are in fact a senior guy who knows the code inside and out.

Otherwise I would have to say I would apply the “if it isn’t broke, don’t fix it” law. While it is quite possible that you may achieve better modulation and encapsulation by breaking up huge classes, it could also introduce tons of problems that are not there currently.

4

Is it a better idea, for code sanitation, to split our larger objects that do too much work into smaller objects?

It is not about the length of the class but about the responsibility.

You could potentially have a class that is a bit too long but is taking care of only one responsibility. So, definitely don’t go by length.

Instead, try to understand the problem it is trying to solve.

If it is solving a single problem then it is following the single responsibility principle and you could either leave it the way it is or DI some of its private methods as helpers classes.

If you notice that it is taking care of more than one responsibility then no matter how long the class I would suggest to break it down into two or however many responsibilities it takes to have them all take care of singular concerns.

There are a couple of principles that apply here

  • SOLID design principles for OO
    The main one that applies to your description is the single responsibility principle.
    Although the Interface segregation principle by its nature tends to keep class sizes reasonable.

  • Refactoring Question
    So whilst it is better to keep classes smaller, the decision to refactor is a more cokplex one.

    Do you have good Test cases.
    With good test cases you can be more adventurous with refactoring . But as usual if the original developers were getting OO build done with a good set of test classes, then they would have already refactored.

    Did the large class need to be changed?
    If you have to change of fix something, that is often a point were a refactor can be worth the investment.

    Refactoring to make it more elegant
    As much as it is nice to have clean and more easily maintained code, without sufficient maintainability issues it is most likely hard to justify refactoring for elegance.

When to refactor Blog, worth a look

if you happen to have a pluralsight subscription, there is an excellent course on this topic refactoring on plural – subscription fee applies

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật