Refactoring methods in an existing code base with a huge number of parameters

I inherited an existing code base where the "functions" are:

  • huge monolithic classes with (literally) 100 member variables and methods that go one for pages (screens).
  • public and private methods with many arguments.

I am trying to clear and reorganize the code to leave it a little better than I found it. So my questions

  • are (or are you) refactoring methods with 10 or so arguments to make them more readable?
  • Are there any better methods regarding how long the methods should be? How long do you usually keep them?
  • are monolithic classes bad?
+6
methods arguments
source share
3 answers

is it worth (or are you) refactoring methods with 10 or so arguments to make them more readable?

Yes, it's worth it. As a rule, it is more important to use refactoring methods that are not "reasonable" than those that are already good, short and have a small list of arguments.

As a rule, if you have many arguments, this is because the method is too much - most likely, it should be a class, not a method.

Moreover, in cases where many parameters are required, it is best to encapsulate the parameters in one class (i.e., SpecificAlgorithmOptions) and pass one instance of this class. This way you can provide clean defaults, and it is very obvious which methods are necessary and optional (depending on what is required to create the parameter class).

Are there any better methods regarding how long the methods should be? How long do you usually keep them?

The method should be as short as possible. It should have one goal and be used for one task when possible. If you can break it into separate methods, where each, as a real, high-quality "task", then do it with refactoring.

are monolithic classes bad?

Yes.

+6
source share

if the code works, and there is no need to touch it, I would not refactor. I am only refactoring very problematic cases if I still have to touch them (either to expand them for functionality, or to fix errors). I approve of the pragmatic approach: only (in 95%) concern what you are changing.

Some first thoughts on your specific problem (although it’s difficult in detail, not knowing the code):

  • start grouping instance variables, then these groups will focus on executing the 'extract class'
  • by grouping these variables, we hope we can group some methods that also move when executing the 'extract class'
  • often there are many methods that do not use any fields. make them static (they are most likely helper methods that can be extracted into helper classes.
  • in case non-related field instances are mixed in many methods, load the "extract method"
  • make the most of the tools for automatic refactoring, because you most likely do not have tests, and automation is safer.

Regarding your other specific questions.

is it worth (or are you) refactoring methods with 10 or so arguments to make them more readable?

definitely. 10 parameters are too big for people to understand. most likely the method does too much.

Are there any better methods regarding how long the methods should be? How long do you usually keep them?

it depends ... on preferences. I said some things in this thread (although the question was PHP). still I would apply these numbers / metrics to any language.

are monolithic classes bad?

It depends on what you mean with the monolith. if you mean a lot of instance variables, endless methods, a lot of complexity if / else, yes.

also look at a real gem (for me it must be for every developer): it works effectively with outdated code

+1
source share

Assuming the code is functioning, I would suggest thinking about these issues first:

  • Is the code well documented?
  • Do you understand the code?
  • how often are new features added?
  • how often are errors recorded and corrected?
  • How hard is it to change and fix the code?
  • What is the expected duration of the code?
  • how many versions of the compiler are you (if at all)?
  • is the OS she runs on expected to change over the course of her life?

If the system is replaced in five years, it will be well documented, it will undergo small changes, and errors can be easily fixed - leave it alone, regardless of the size of the classes and the number of parameters. If you are looking to reorganize, make a list of your refactoring offers in order of maximum profit with minimal changes and attack it gradually.

0
source share

All Articles