C
C#

help

✅ Help me understand what a "God" class is and why/how should I avoid it.

Llukkasz3231/25/2023
I'm specifically referencing to this: https://en.wikipedia.org/wiki/God_object The first example is how my current game project is structured. The second is a different example that is my other theory how a "God" class might look like. Which of these are God classes? Both? Neither? Why is this even considered a problem? How an alternative could look like?
God object
In object-oriented programming, a god object (sometimes also called an omniscient or all-knowing object) is an object that references a large number of distinct types, has too many unrelated or uncategorized methods, or some combination of both. The god object is an example of an anti-pattern and a code smell. A common programming technique is t...
Kkhamas1/25/2023
from reading the description you basically need to put the code in the class it belongs not make a code noodle soup
Llukkasz3231/25/2023
What about the " all-knowing object" description? The "Game" class has access to every part of the program through it's members.
Kkhamas1/25/2023
well it's just an object that references others nothing special really if you need to access the objects you can ask the god class I guess
CChucklesTheBeard1/25/2023
1st, it's a very, very good idea to learn about this. 2nd: in game design, performance is god and many good enterprise design practices are sacrificed at the altar of performance, possibly including "never use god objects". A "god class" is a class that knows too much; it knows that Processors need to update Entities, but some Entities have to talk to a Processor and check for collisions with non-entity objects in this Scene but not that Scene, and that UpdateSystem needs to run part 1 before part 2 and EntityManager can only have 4 objects at a time because... WHOA WHOA WHOA, this is supposed to be a high-level class, we are way too deep into the weeds, there are too many levels of abstraction are mixed together... this class is trying to do everything. If you want to add a new Entity you may need to update 40 lines of code scattered throughout the god class to make it work. A SOLID class is only responsible for exactly one thing; if you want to add an Entity, you define the new behaviors in one 40 line chunk, then update maybe one or two lines in directly adjacent classes, to call the new behavior. Of the two example diagrams, the 2nd is much more likely to be a god class than the 1st
Llukkasz3231/25/2023
Now that I think about it, I made every member in my composition public, only now I see how much nested access it has.
Llukkasz3231/25/2023
I think only tried to limit child -> parent access, but not the other way around. and didn't even succeed at that lol
CChucklesTheBeard1/25/2023
child -> parent and parent -> child can be ok... the problem becomes more obvious when you have parent->child->grandchild->greatgrandchild->...
Llukkasz3231/25/2023
I see
CChucklesTheBeard1/25/2023
if greatgrandchild needs a change, you don't want to have to modify all 3 other classes
Llukkasz3231/25/2023
I feel like I get confused sometimes with names when talking about composition of objects. Are you refering to inheritance here?
CChucklesTheBeard1/25/2023
Layers of abstraction Program (yep, I'm running it on a computer) -> Game (needs to know how to be a game) -> Scene (particular collection of behaviors; here's how the update loop runs in the "pause menu" scene or whatever) -> EntityManager -> Entities (perhaps including the "save & quit" button)... Program should never ever need to know the damnedest thing about a save and quit button, frankly neither should Game or probably even Scene
Llukkasz3231/25/2023
I understand. What do you think about a situation when the "save & quit" button has access to other intermediate objects like for example Entity creator, or a list of sound files that can be played?
CChucklesTheBeard1/25/2023
Those requirements should be gathered elsewhere and handed to the button, the button itself shouldn't know the details of how they were assembled.
class SaveAndQuitButton : Entity
{
var _listOfSoundFiles;
public SaveAndQuitButton(List<SoundFiles> injected){
_listOfSoundFiles = injected;
}
}
class SaveAndQuitButton : Entity
{
var _listOfSoundFiles;
public SaveAndQuitButton(List<SoundFiles> injected){
_listOfSoundFiles = injected;
}
}
, not
class SaveAndQuitButton : Entity
{
var _listOfSoundFiles;
public SaveAndQuitButton(TightlyCoupledParent p){
_listOfSoundFiles = p.SoundFilesListCreator.Create();
}
}
class SaveAndQuitButton : Entity
{
var _listOfSoundFiles;
public SaveAndQuitButton(TightlyCoupledParent p){
_listOfSoundFiles = p.SoundFilesListCreator.Create();
}
}
Llukkasz3231/25/2023
I see, thanks, that's very helpful.
CChucklesTheBeard1/25/2023
This is called dependency injection, there are other approaches that operate on the same principle (like, passing in a kinda godlike "dependancy manager" object) but that's the simplest I think
SScratch1/25/2023
In terms of responsibility, each class should have a single responsibility. A god class has many. A common thing I see new game devs make is a "GameManager" class that just does everything
FFusedQyou1/25/2023
There's nothing wrong with a God class
Llukkasz3231/25/2023
I feel like "single responsibility" is very poorly defined. One could say that "Managing the game" is the responsibility of the "GameManager" class and there are no other things the class must do, so therefore it follows that principle.
FFusedQyou1/25/2023
People just decide to throw unnecessary shit in a God class It's perfectly reasonable to create a God class if it's an entrypoint to the main behaviour of something. Like with a game, being able to start/stop it or adjust certain rules without going into the dependencies that hold the main logic for them A God class is also a very good place to apply main communication between dependencies that rely on eachother, without making them communicate directly. This is a very good way to scale I made a console for Unity yesterday doing exactly this and it's very easy to use, adjust and read in general
Llukkasz3231/25/2023
And that person would be right from a logical standpoint. I'd be surprised If I was the only one that doesn't really understand the scope of this principle.
Ddont1/25/2023
if you have a class called SomethingManager that's quite a yellow flag to begin with
Llukkasz3231/25/2023
So you mean that there should be no classes that manage other things?
Ddont1/25/2023
exactly, it shouldn't "manage", it should do something specific if it's called Manager then you have no idea what it really should do it has... all the responsibilities? which goes against previous said principle
Llukkasz3231/25/2023
I see, this makes more sense
SScratch1/25/2023
I read Clean Code. Uncle Bob says that functions should be operating at a single "level of abstraction". A "manager" can be fine if the way it's interacting with other objects is at a very high level. But if it starts getting deep into messing with other objects it'll quickly become a mess and way too many responsilbilities.
AAccord1/26/2023
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
❔ Managing `global.json` with JebBrains Rider?I'm trying to get it to follow/manage a `global.json` file at the root of my solution but I can't geHttpListener listen on ipv6Hi, is it possible to specify ipv6 in HttpListener?❔ Return IEnumerable backwardsHey! I got a method which returns a specific collection depending of the parameter. Right now, I nee✅ Shorten inline switch with multiple passing valuesHi! Would there be a way to shorten this? In python, their pattern matching would have a simple `in ❔ Where to store hashed user passwordsI'm working on a really small project just to learn more about ASP.NET and DB stuff. I have a users ❔ Is there a way to remove brackets from empty methods?E.g. ```CS public MismatchedEnumLengthsException() : base("Length of values and enums are different.❔ compare two datessee below <:RoleAssociate:605486956700237856>hello can someone fix me please and also tell me why im dumbpublic class PlayerGUI : MonoBehaviour { public float playerMoveSpeed; public float pla❔ how to save text from TextBox to file?without save file window❔ I can't autocomplete on VS code :cI tried many ways to fix this but nothing works. Help!❔ Can I generalise this method?```CS public static IEnumerable<Enum> TransformIdToEnums(string value, params Type[] enums) { st❔ c# helphow can i make ipv6 altering application using c#❔ Cross-platform keyboard and mouse automation library wantedAnyone knows a .NET library that is on par with pyautogui? Specifically, waiting for a key to be preHow to change the icon for Android in a Maui/Blazor application?Net 7.0, latest visual studio preview. I have a simple (beginner) one page Maui/Blazor app that is ❔ EF Core Code-first relationshipsHi there, I am trying to create a coffee vending machine application. But, I can't quite work the re❔ Hello, postman tips on hitting API with external login page? I need to get the 'code' response❔ Hello, I would like to make a console with the command: Script (some script)if (TextBox.Text == "script" { run script }❔ help me plsmain. cs(7,0): error CS1525: Unexpected symbol 'if'❔ Reusing strings across classesThis is file 1 https://cdn.discordapp.com/attachments/987310138195054592/1067462232860004433/image0❔ UWP TreeView is not stretching to fill spaceTitle pretty much explains it all: I have a TreeView that's not stretching and stays 0px x 0px if t