Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

splitting a class that is too large

Tags:

python

class

I have a class BigStructure that builds a complicated data structure from some input. It also includes methods that perform operations on that data structure.

The class grew too large, so I'm trying to split it in two to help maintainability. I was thinking that it would be natural to move the operations into a new class, say class OperationsOnBigStructure.

Unfortunately, since class BigStructure is quite unique, OperationsOnBigStructure cannot be reasonably reused with any other class. In a sense, it's forever tied to BigStructure. For example, a typical operation may consist of traversing a big structure instance in a way that is only meaningful for a BigStructure object.

Now, I have two classes, but it feels like I haven't improved anything. In fact, I made things slightly more complicated, since I now need to pass the BigStructure object to the methods in OperationsOnBigStructure, and they need to store that object internally.

Should I just live with one big class?

like image 679
max Avatar asked Feb 06 '12 04:02

max


2 Answers

The solution I came up with for this problem was to create a package to contain the class. Something along the lines of:

MyClass/
    __init__.py
    method_a.py
    method_b.py
    ...

in my case __init__.py contains the actual datastructure definition, but no methods. To 'attach' the methods to the class I just import them into the class' namespace.

Contents of method_a.py:

def method_a(self, msg):
    print 'a: %s' % str(msg)

Contents of __init__.py:

class MyClass():
    from method_a import method_a
    from method_b import method_b

    def method_c(self):
        print 'c'

In the python console:

>>> from MyClass import MyClass
>>> a = MyClass()
>>> dir(a)
['__doc__', '__module__', 'method_a', 'method_b', 'method_c']
>>> a.method_a('hello world')
a: hello world
>>> a.method_c()
c

This has worked for me.

like image 147
Emanuel Ey Avatar answered Nov 01 '22 04:11

Emanuel Ey


I was thinking that it would be natural to move the operations into a new class, say class OperationsOnBigStructure.

I would say, that's quite the opposite of what Object Oriented Design is all about. The idea behind OOD is to keep data and methods together.

Usually a (too) big class is a sign of too much responsibility: i.e. your class is simply doing too much. It seems that you first defined a data structure and then added functions to it. You could try to break the data structure into substructures and define independent classes for these (i.e. use aggregation). But without knowing more it's difficult to say...

Of course sometimes, a program just runs fine with one big class. But if you feel incomfortable with it yourself, that's a strong hint to start doing something against ...

like image 25
MartinStettner Avatar answered Nov 01 '22 05:11

MartinStettner