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

Posted by Andrew Florko on Stack Overflow See other posts from Stack Overflow or by Andrew Florko
Published on 2010-06-08T04:41:30Z Indexed on 2010/06/08 4:42 UTC
Read the original article Hit count: 188

Filed under:
|

I have a legacy class that is rahter 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 ... 
  }    
}

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

What will 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!

© Stack Overflow or respective owner

Related posts about c#

Related posts about refactoring