Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refactoring. Your way to reduce code complexity of big class with big methods

Tags:

c#

refactoring

I have a legacy class that is rather complex to maintain:

class OldClass {
  method1(arg1, arg2) {
      ... 200 lines of code ... 
  }

  method2(arg1) {
      ... 200 lines of code ... 
  }

  ...

  method20(arg1, arg2, arg3) {
      ... 200 lines of code ... 
  }    
}

The methods are huge, unstructured, and repetitive (developer loved copy/paste aprroach). I want to split each method into 3-5 small functions, with one pulic method and several helpers.

What would you suggest? Several ideas come to my mind:

  • Add several private helper methods to each method and join them in #region (straight-forward refactoring)

  • Use Command pattern (one command class per OldClass method in a separate file).

  • Create helper static class per method with one public method & several private helper methods. OldClass methods delegate implementation to appropriate static class (very similiar to commands).

  • ?

Thank you in advance!

like image 969
Andrew Florko Avatar asked Jun 08 '10 04:06

Andrew Florko


People also ask

How do you refactor a big method in Java?

The step towards refactoring is primarily to separate the logic of PDF creation from the business logic. Once that design smell is addressed, we have few code smells to address, such as external iteration (the for loops) and code duplication (those repeated calls to table. addCell method).

What are the best practices for refactoring?

The best time for refactoring is before adding new features or updates to existing code. Doing so can help improve the product's quality. By cleaning the code base before adding new features or updates, it helps to make the product more robust and easier to use in the future.


1 Answers

SRP - Single Responsibilty principle and DRY - Don't Repeat yourself

like image 95
this. __curious_geek Avatar answered Oct 07 '22 12:10

this. __curious_geek