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

  1. 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. 
  2. 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.

Anuj holds professional certifications in Google Cloud, AWS as well as certifications in Docker and App Performance Tools such as New Relic. He specializes in Cloud Security, Data Encryption and Container Technologies.

Initial Consultation

Anuj Varma – who has written posts on Anuj Varma, Hands-On Technology Architect, Clean Air Activist.