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.
Interfaces should be both complete and minimal, meaning that a type interface should contain all methods necessary to achieve the needed and meaningful tasks on that type, and only these.
So analyse the API methods of Matrix and determine which of them belong to the core API (which must have access to the internals of the class to accomplish their task), and which are providing “extended” functionality. Then reduce the class API to the core functionality, migrating the rest of the methods to separate helper / utility / subclasses which can accomplish their goals using the public core API.
For refactoring / unit testing help, consider getting Working Effectively with Legacy Code by Michael Feathers.