Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Are there any best approaches for refactoring a programming project that has previously been written with the static reference anti-pattern (the majority of classes refer at some point to a static bean, where all of it's variables are public.

Half-way through a class, for instance you'll see something like:

EvilClass.Variable = returnsABoolean().

Then in a different class, you'll see something like:

if(EvilClass.Variable == foo){
    doSomething();
}else{
    doSomethingDifferent();
}

Up until recently, I've been working with this pattern as I don't want to mix the styles and get things confused between the styles. But I want to slowly but surely 'fix' this project.

I'm the only dev on this, and I don't have the time to just sit down and refactor it (otherwise, I'd push for a full rewrite!).

Are there any best practices to be aware of when working with/fixing this kind of project/problem?

share|improve this question
    
If you don't have the time to fix it, aren't sure how to approach fixing it, and it isn't broken and works, why do you need to 'fix' it? I mean is this something you want to do to teach yourself, or the project needs expanding or are you just fixing it for something to do? –  RhysW Apr 16 '13 at 9:52
add comment

1 Answer

I would probably approach this with the following multi-step approach:

Phase 1:

  1. If possible, isolate the static class.
    • In C# I would move the static class to its own assembly, or an isolated assembly, and make all its public members internal so that only a class in the same isolated assembly can use it.
  2. Then, create a non-static class that exposes the static class.
  3. Inject this non-static class into all other bits of code that required the static class.

Phase 2:

  1. Make the non-static class a singleton (only temporarily) so that it throws an exception if instantiated more than once
  2. Move all the static code into the non-static class.
  3. Delete the static class

Phase 3:

  1. Run your entire set of tests with this new setup (if you're lucky enough to have any)
    • Or, make a list of all code paths that use the new non-static class, and try to cover all of them with manual testing.
  2. If you encounter singleton exceptions, then you're accidentally instantiating your non-static class more than once. See why, and fix it
  3. If you don't encounter any exceptions, you're safe.

Phase 4:

  1. Refactor any code that uses your new non-static class so that it respects the Single Responsibility Principle. This will be a lot of work, but should help prevent accidentally using the wrong instance of the non-static class won't happen.
  2. Remove the Singleton pattern from the non-static class
  3. Keep refactoring until all dependencies are injected everywhere :) (easier said!)
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.