HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

How Godly does an object need to be before it becomes unholy?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
becomesneedunholydoeshowbeforeobjectgodly

Problem

I've been told that using God objects at all is a Bad Thing™

In object oriented languages, God objects know all, they control too much. I'm trying to build a game (or for the scope of this question a generic app with a GUI) and I'm using a Main object that holds all the other objects needed to make it all work.

At the moment (and probably forevermore), my main.py module contains only functions that initialise the other modules in order to setup my app.

The other modules setup are managers of different things and most inherit from a class ManagerBase which among other things can retrieve other managers and from that, the contents of said managers.

```
#Import modules
import pygame
import sys, os

import assets.config.config_manager
import assets.events.event_manager
import assets.events.ai_event_manager
import assets.font.font_manager
import assets.ui.subscription_manager
import assets.ui.screen
import assets.ui.keyboard_injector
import assets.entity.entity_manager
import assets.databin

CAPTION = "Generic Application"

class Main(object):
def __init__(self):
self.args = sys.argv
self.debug = "debug" in self.args

def init_databin(self):
self.databin = assets.databin.Databin()

def init_config_manager(self):
self.config_manager = assets.config.config_manager.ConfigManager()
self.fps_limit = self.config_manager["video_config","fps_limit"]
self.show_fps = self.config_manager["video_config", "show_fps"]
if not self.show_fps:
self.blit_fps = lambda: None

def init_screen(self):
pygame.init()
self.screen = assets.ui.screen.Screen(pygame.display.set_mode(*self.config_manager.get_screen_properties()), pygame)
pygame.display.set_caption(CAPTION)
if self.config_manager["video_config"]["screen_properties"]["fullscreen"]:
pygame.mouse.set_visible(False)
self.screen.blit(self.screen.old_im_load(os.path.join("assets", "loading.png")), (0,0))
self.update_screen()
self.clock = pygame.time.Clock()

Solution

According to Wiki "a god object is an object that knows too much or does too much". The general problem I have with this is: What means "too much"? Your question is about searching an absolute statement as we already know we can only find absolute statements within very restricted areas (and that's what they all have in common) that are not related to reality. As soon as we deal with real world applications we have to deal with uncertainty. That is because we derive OO models from reality as we perceive it. And this can be error prone.

To escape this dilemma in computer science some principles are discovered that lead to a step by step improvement of source code. They are called S.O.L.I.D. principles. If you violate a principle your source code becomes worse. So the target is to violate the principles as less as possible. So easy as I say it: The violation of these principles is an identification problem that sometimes becomes very very difficult.

In the case of the so called "God class" the "S" of these principles is addressed: The single responsibility principle (SRP). It says that one code fragment (module, class, method) should only have one responsibility. BTW this is applicable to other programming paradigms as well. A "God class" seems to have at least more than one responsibility. That can be said for sure. Anything else is popular speech if someone says "God class".

So working with SRP your code will improved step by step by identifying violations of this principle and eliminate them. That is by consolidating redundant responsibility and vice versa by separating different responsibilities.

But the whole thing only works if you identify the violation. And that's the core. To identify a violation you look for indicators:

  • real redundant code fragments



  • a bug that was not fixed everywhere because of code redundancy



  • a bug that was fixed but broke the application at another place



  • long classes, long methods



  • a lot of object local variables



  • deep nesting



  • objects that do a lot of different things



  • ...



I want to underline that these are only indicators. A long method method for example can do only one thing: initialize a hashmap with key value pairs. Although you would have thought about solving this another way it's not violoating SRP.

So if you have an indicator you can make a thought experiment if there is a violation. If you think you have redundant responsibilities then you should think about a new business requirement that changes one code fragment and ask yourself should the other change as well. This you should discuss with the business people. BTW consolidating responsibilities is much harder that separating them because this may break the application as one redundant code fragment will be omitted.

So back to your question: A class does not become holy or a god. It will become less godly by eliminating violations of SRP. Theoretically the class becomes godless when an 1:1-relationship of responsibility and code fragment is reached. But to ask when there are too much responsibilities does not make sense in the context of code quality. This will only be a matter of costs to maintain the code. If it costs too much (for the business men) and the costs can be assigned to the SRP violation then certainly there are too much responsibilities.

Context

StackExchange Code Review Q#115591, answer score: 5

Revisions (0)

No revisions yet.