1

Every description I have read about god classes considers them to be an anti-pattern and a bad practice. Most descriptions I have read about mixins considers them to be acceptable in some cases. If the functionality of a god class is broken up into several mixins, each mixin can be separately unit tested and separation of concerns is still maintained, at least to some degree. When it comes down to it, I'd still like to encapsulate all of the functionality into one object. I'd like to get opinions on this design. Is this design a bad practice?

  • 5
    Why do you want one object? – JimmyJames Nov 20 '17 at 17:17
  • It is if you are an atheist.... (OK, bad jokes done) – Berin Loritsch Nov 20 '17 at 17:22
  • 6
    @J.Darnell: If they are logically very different why do you want them on the same object? What are you trying to achieve by this? – JacquesB Nov 20 '17 at 17:54
  • 2
    Or to pose @JacquesB 's question from a slightly different angle, why would you not want different functionality split out into different classes? What issues are you concerned about with regards to splitting your application into logically separate components? – Ben Cottrell Nov 20 '17 at 18:03
  • 3
    @J.Darnell: You don't give any reason why you can't just use composition, so I will go ahead and say your use of a God class is bad practice. Let the logically separate components be properties of the project object. – JacquesB Nov 20 '17 at 18:35
8

Sorry to be the bearer of bad news:

Yes.

The larger your application gets, the more trouble this will cause. What happens when you try to name a method do_thing, import it into your god class, and find that it already has a method named do_thing? Now you have to worry about namespace collisions in your god class: what a pain!

Another (small) one: if everything goes here, and multiple people are working on different modules, but everyone has to "register" things in your god class, then you will be dealing with lots of unnecessary merge conflicts for that class in your code repository. If separate things were always separate, then you have less to worry about from people stepping on eachother's toes.

These are just a few things that come to mind. Really though, you are approaching it from the wrong perspective: why would you want to do this? If you have everything nicely separated and organized, why would you want to undo all of that and throw it all into a single class? Doing this might save you a few import statements at the top of your code, or a few less dependencies that need to be injected, but none of that is substantially improving your code base, and it comes at a real cost. It's a simple formula: cost + no benefit = bad idea. You're already doing the "hard" part of dividing your application nicely. Don't mess that up on the home stretch.

  • As a result, instead of using god_object.some_func(), it might look like obj = god_object.get_some_object() then obj.some_func(). This is less preferable to me but would fix the namespace conflict issue. I still call it god_object because I want to encapsulate all of the functionality into one object. There are a number of things you can do with a project and I would like them all to be available given a project object. – J. Darnell Nov 20 '17 at 18:19
  • If the project class did not exist, I believe the existing mixins would just become normal classes on their own. I would then have to come up with a way to identify what project each mixin class is supposed to be associated with. I think this complicates things. – J. Darnell Nov 20 '17 at 18:21
  • 2
    You are describing the process of design. After design, things will be much less complicated. Trust me. – BobDalgleish Nov 20 '17 at 18:23
  • @J.Darnell I think the issue is that you have some higher-level organization concerns to work through, which probably won't be possible in this one question. Having managed large applications (hundreds of thousands of lines of code), I've never had reason to fall back on God classes of any sort. Once things get that large, you just can't: you either figure out how to organize your application or it becomes very painful. – Conor Mancone Nov 20 '17 at 18:24
  • @BobDalgleish Okay, there is a consensus that my design is not great. What is the right design then? I understand that is probably not answerable with the limited details I have given here. I have explained some of the behavior that I would like though. – J. Darnell Nov 20 '17 at 18:25
1

From your comments, it sounds like you might want to approach this from the perspective of an IoC Container. If your application has a set of functionality available within it, and you need to have pieces, determined at run time, use that functionality, the best approach is likely a an inversion-of-control container with dependency injection.

  1. Your application consists of a set of libraries, likely individual objects that are tied together into larger functionality.
  2. The functional groupings conform to the mixins that your described earlier.
  3. Individual "project" classes will be added to the application for specific command-control-reporting functions, and as those projects are added in, their dependencies on those interfaces will be bound, using dependency injection.
  4. Each project class will run in the environment as if it has access to the various different interfaces. Control will pass back and forth between the main application and project class.

This is sometimes called an application framework.

  • I still make project classes then? I imagine the methods of that class would do nothing but wrap the functionality of the libraries underneath. From my brief study of IoC, it appears to be more abstraction than necessary for my application but I may be wrong. – J. Darnell Nov 20 '17 at 18:54
  • Since I don't know what your project classes do, I cannot comment further. However, the IoC container approach avoids the god-class abomination. – BobDalgleish Nov 20 '17 at 19:01
  • The point isn't simply to wrap a libraries functionality. It's to clearly express that all you need from this messy library are these 2 or 3 little things and you don't really care which library does that for you. That way when the boss says we can't get licenses for this anymore you don't have to reinvent the whole library. Look up the interface segregation principle. As for mixins: if you've mastered a hammer beware of screws. – candied_orange Nov 20 '17 at 21:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.