Large Method Signatures (too many arguments)
The Problem
A recent project of mine had me looking at C# code which had been brought over from old C/C++ code. One of the first things that struck me was the sheer number of arguments in each method. It wasn’t unusual to find a method such as
CreateAccount(string arg1, bool arg2……….string arg11, someothertype arg12…)
Such methods are hard to maintain and hard to work with (Imagine being a consumer of the above method).
A good refactoring workaround
- Define a type (a class) for encapsulating as many of the above arguments as possible. If the arguments are related (as they often tend to be), that makes it that much more logical to have such a class defined.
- Example : Instead of (int accountNumber, string accountDescription….), simply define an Account type that encapsulates both accountNumber and accountDescription).
This is what our refactored method looks like:
CreateAccount (Account inAccount)
Summary
Granted, this is a trivial example – for in this case, chances are good that you already have an Account type to work with. However, in a lot of cases, the above encapsulation may not exist – and it is your job (being the expert OO programmer that you are), to go through the code and provide such encapsulations where ever possible. It will make the codebase a lot cleaner and a lot more maintainable.
Leave a Reply