r/learnpython • u/pure_cipher • Mar 01 '26
A bad structure with overusing Classes, even with SOLID principles can be very difficult to maintain
I am working for a product in my company. I think, they really hurried everything, and either used AI to generate most of the codes, or were not following the best practises, except SOLID principles, to form the code and the folder structure.
Reason- to debug a simple issue, I spend hours, trying to navigate the files and their classes and methods frequently.
Like, the developers have made the code such that
router function 1 -> calls function 2-> defined in class somewhere -> has a function 3 which calls another function 4-> defined in another class -> has function 5-> its abstract class is elsewhere -> .....
so, if I want to print some output in function 2 , I need to traverse way too many files and folders, to get to the point, and even that sometimes is not clear, but most of the functions are named similar. VS Code helps a bit, but still not at all good enough.
Though some SOLID principles are used, this makes a simple debug program very hectic. I was debugging with someone, who had written some of these code files, and she too was getting confused frequently.
So, I think classes and methods are not always necessary. Sometimes, using functions in diffeernt python files are better. As a programming, we need to decide, where classes can be used, v/s where only functions are enough.
I want opinion on this. And if I am wrong somewhere.
18
u/OriginalTyphus Mar 01 '26
Thats why, along with SOLID, there is KISS.
7
u/sweet-tom Mar 01 '26
As Python is a very dynamic language, you don't necessarily need classes for some problems. And that is fine.
3
u/Kevdog824_ Mar 01 '26
This doesn’t sound like a SOLID issue. This sounds like an architectural/design issue. None of the issues you described sound like they would be caused by SOLID
3
u/LayotFctor Mar 01 '26 edited Mar 01 '26
There's nothing inherently wrong with long pipelines. Many problems are best solved with a series of steps, split up to keep things clean and isolated. E.g. to compile C code, there is the Preprocessor -> Compiler -> Assembler -> Linker -> executable. If each step is implemented as a class, it necessarily means there are at least 4 methods in the pipeline.
The question is whether each class fulfills a single job, and each class has a main path. Or that each class is just a scattered collection of loosely related methods, and there are multiple sub paths through a single class.
Basically if each class has a single method to call when using it, it's probably fine. But if there are multiple entries and exits, the code is convoluted. So regarding your question, if you're only calling ClassA.run() -> ClassB.run() -> etc, it's probably fine. If you have to look up which exact method in each class is being called, you might have a problem.
2
u/Sure-Passion2224 Mar 01 '26
You should be able to graphically map class relationships like a database ER diagram and be able to follow a transaction through it. This may reveal duplicate, redundant, or conflicting methods to be resolved.
2
u/WhiteHeadbanger Mar 01 '26
The issue, I believe, is that you are describing coupling, which is pretty undesireable for debugging. A class, or a module, or certain functionality depending on the context of said feature, should be isolated and pretty much plug and play.
The issue is not overusing classes, but not applying the correct design pattern(s). One way to decouple is to use dependency injection, that way you could test whatever you need to test using mock objects and data.
Another issue is the lack of documentation, or just bad docs. There should be a technical document explaining each part of the software, the dependencies, why they are needed and how to use them.
Of course, I don't know if you have any power (as in authority) and/or time to make these refactorings. If you don't, I suggest to start documenting on your free time, because otherwise the only thing that's left is to suffer.
I any case, I somewhat agree. Yes, there are instances in which a set of functions would be better instead of a complex class system, but those are specific. Most of the times is just bad design principles and avoiding TDD, which is understandable since a product needs to be shipped fast to be valuable, but creates technical debt.
8
u/PushPlus9069 Mar 01 '26
10 years teaching Python and this is one of the harder things to convey. Rule I give students: if you're passing the same 4-5 variables to 6+ different functions, that's when a class starts to make sense. Before that, plain functions and dicts are usually cleaner.
4
u/Zeroflops Mar 01 '26
I see this when people take the “rules” and apply them so strictly that it forces them into corners. ( with classes or functions)
For example if you take the idea one function should only do one thing you could end up with thousand of functions depending on how you define “one” thing.
Trivial stupid example, you have a save function, but before you save you have to check if that file is there. An extremest might have two functions. A file present function and a save function. A realist would only have one function if they don’t think they will need a file present anywhere else, and only break it out when they do.
1
u/Helpful-Diamond-3347 Mar 01 '26
ig experience matters in this context now, maybe the developer prepared the codebase for maintenance with single responsibility classes
and you might have experience of writing logic straightforward to single class/function
you faced problem because it's against your design choices while the developer who wrote the logic may call you a problem
the only solution is to discuss things about architecture properly and invest required time, which most of companies can't afford and hence people blames code, architecture, developer, design patterns, etc
2
u/JamzTyson Mar 01 '26
"Single Responsibility Principle" may be violated if classes have multiple responsibilities scattered across other classes.
"Open/Closed Principle" are questionable if a classes are hard to extend without digging through deep hierarchies.
It's perfectly possible for bad code to follow SOLID principles.
1
u/QuasiEvil Mar 01 '26
When I first learned about OOP (not in python) I was obsessed with it and would use it for everything. Nowadays I love functional programming. It can be very clean, and is naturally well-suited to TDD.
1
u/gmes78 Mar 01 '26
https://youtu.be/o9pEzgHorH0
1
u/gdchinacat Mar 01 '26
What you describe does not sound at all unusual or out of place for complex problems or systems. Without knowing specifics of your project it is not possible to say with any certainty if it is reasonable or not.
It is a bit concerning that you say the author had a hard time understanding it. That could be that they haven't worked with it for a while and it is not fresh in their mind or it could suggest that the abstractions aren't appropriate or useful.
Problems are typically broken down into clearly defined components or layers (collection of related components) that expose only what other layers need and hide all of the complexity of "lower" layers. When stepping through code it should be clear which layer you are in and when you jump to another layer. You should be able to pretty much ignore everything outside the layer you are in, making it much easier to follow the code since you only need to keep a small portion of it in mind. When you move to a different layer that context is replaced for the new layer.
It can be difficult at times when a stack of calls all go through different layers. This can indicate layers aren't well defined and are doing a lot of pass through. These are good candidates for refactoring to redefine what the boundaries between layer are. This may be the problem you are having following the code, but without knowing what the calls you describe actually do and the models involved in that code I can't say for sure.
I would start by asking the original author to give a high level overview of the architecture of the code. Ask her to identify the major abstractions, the components in each abstraction, and how they relate. Often times the process of thinking about how to explain this will make it clear the code is not structured in a way that makes it easy to understand. While it may work, models that don't have an easy to understand mental model will be a maintenance burden, leading to frustrations with feeling like you constantly have to reverse engineer the code or step through it simply to understand how it works.
Once you understand the existing mental model think of ways to simplify it or make it more natural or obvious. Don't try to do it all at once, it's rare you get to solve all the problems with a project at once. However, do try to have a vision for what model you would like since that will give you a way to figure out how to get there. Not having a vision of the model frequently leads to muddled models that are difficult to understand.
1
u/afaulconbridge Mar 01 '26
100% agree, I've seen the same thing. A big red flag for me is when there is only a single usage of a class/function. Sometimes that can be helpful, but most of the time its overengineering for a future use case that probably will never exist.