Here’s all one should know about God Class in Java

Most of the java developers seek it but fail to attend

Image for post
Image for post
Photo by Michiel Leunens on Unsplash

ILLUSTRATION OF GOD CLASS

Let’s say you are building a customer management application. Here’s the following snippet depicting the same.

Image for post
Image for post
God Class example screenshot by the author

Some more problems of having a “God Class”

  • All unwanted members of such class will be inherited to other classes during inheritance, violating the principle of code reusability.
  • The object will occupy more space in heap memory, so it will be an expensive operation in terms of memory management and garbage collection.
  • The phenomenon of tight coupling is noticed among such object class making the code difficult to maintain.
  • Unwanted threads will be running in the background thus at the time of parallel processing low priority threads might block high priority tasks to run.

Why and When does this happen?

Programmers face a conundrum of basic values. Every programmer has his own favorite formatting rules, but if he works in a team, then the team rules. A team of developers should agree upon a single formatting style, and then every member of that team should use that style. We want the software to have a consistent style. We don’t want it to appear to have been written by a bunch of disagreeing individuals.

Strategic measure to prevent or resolve the God class or object:

Firstly, let’s focus on avoiding god Objects or class while developing.

Leave the campground cleaner than you found it.

If we all checked-in our code a little cleaner than when we checked it out, the code simply could not rot. The cleanup doesn’t have to be something big. Change one variable name for the better, break up one function that’s a little too large, eliminate one small bit of duplication, clean up one composite if statement.

Image for post
Image for post
Sources: - Clean Code By Robert C. Martin

FUNCTIONS SHOULD DO ONE THING. THEY SHOULD DO IT WELL. THEY SHOULD DO IT ONLY.

Now, let’s focus on fixing God objects that already exist. The only solution for this is to refactor the object so as to split related functionality into smaller, manageable pieces. For example, let’s refactor the Customer.java from earlier:

Image for post
Image for post
Custome.java screenshot by the author
Image for post
Image for post
Address.java screenshot by the author

Final Thoughts

God Objects happen in poorly-designed systems, where an assumption happens that all necessary data needs to be kept tightly coupled.

Written by

@Oracle Software Developer Dev + Data + Design ➡ Programmer An IT engineer by choice, works at IT by choice ✅ Sharing my journey as a women in tech.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store