Wednesday, April 18, 2012

Handling large classes

I've recently begun coding in Java in the past few months. I have a Matrix class that's becoming much too bloated with a lot of methods. I also have a SquareMatrix class that extends Matrix, and reduces some of the bloat.



What I've found is that alot of the methods in the Matrix class are relevant to matrices in general. It has all the basics, like addMatrix(Matrix), multiplyMatrix(Matrix), multiplyMatrix(float), then more complex methods like getGaussian(Matrix) or getLUDecomposition(Matrix).



What are my options in reducing the number of lines in my class Matrix?
Is it normal for a class to become extremely large? I don't think so...
Is this something I should have considered early on and refactoring is difficult? Or are there simple solutions?



Thank you!






Edit: After reading a couple of responses, I'm considering doing the following:



Utility class Matrix contains all common/basic methods for a matrix



Delegation classes (Helper/sub):



*Helper Class*
getGaussian(..), getLUFactorization(..), ...



Subclasses
(extends Matrix)
SquareMatrix, LowerTriMatrix, UpperTriMatrix, ...



Delegation seems similar to defining multiple .cpp files with headers in C++.
More tips are still welcome.






Edit2: Also, I'd prefer to refactor by properly designing it, not quickfixes. I hope it helps down the road for future projects, not just this one.





No comments:

Post a Comment