I have a class that I think is too long. I don’t really know what too long means but it’s about 2500 lines of code. However, all the methods use at least one or more of the variables so I think it’s pretty cohesive. I’m thinking of still breaking this class up into a few smaller classes that would use three of the same variables. Is this bad design or does this represent a pattern?
class MyClass
{
...
MyVar1 myVar1;
MyVar2 myVar2;
public void DoStuff()
{
...
MyPart1 myPart1 = new MyPart1(this,myVar1,myVar2);
myPart1.DoStuff();
MyPart2 myPart2 = new MyPart2(this,myVar1,myVar2);
myPart2.DoStuff();
}
}
You can’t generally say that 2500 lines of code is too much for one class.
You can however say that a class that gets used for 10 different actions is quite monolithic. Some people here say that each class should only have one functionality. These people would read the “10” as binary…
Now if you don’t see a chance to divide your class in half, maybe start by splitting small functional parts off instead. This way you might get a better view for what’s really essential to your classes’ functionality.
Start with looking at your methods: If your class has several methods that basically belong to the same scope (e.g. XML-I/O or something like a Play/Pause/Stop/Reset function set) you could create a subclass for these.
If all your methods are on a par with each other (i.e. the opposite of the above), I’d say your class is not too big.
The most important thing though is that you don’t lose orientation in your code. Try structuring your class and order your methods as seems best fit. And don’t forget to comment this order so you’ll get into it easily again…