Being a Java developer in an agile development cycle, I have learnt that it is essential to make sure I design my classes in a manner that I can refactor them easily without much of a pain. I want to know, what are the best practices that you follow in your daily design/development cycle that helps you to perform refactoring easily.
For example, I know that I should hide the implementation details behind an interface. So that event if I am changing the implementation tomorrow, I am not disturbing the client code which consumes this APIs. Similarly I should use “factory design pattern” wherever possible so that the change of implementation classes can be controlled from one factory class rather than find out all the places and change them.
Similarly I would like to know what all best practices that you are following which will help me.
views:
276answers:
6Use TDD. Seriously. Writing tests while you write your classes forces you to think about how they will be used by others. You'll tend to write better abstractions when you do this.
Entire books have been written on this subject:
- Code Complete
- Working Effectively with Legacy Code
- Clean Code
- Refactoring
- Refactoring to Patterns
- Agile Principles, Patterns and Practices
Each one of these touches on this subject in its own unique way.
Don't copy and paste (also known as DRY: Don't Repeat Yourself). When any given functionality is implemented in no more than one place, then it's easier to reimplement that functionality.
My answer relates to what constitutes a good result after the refactoring.
Methods should be...
- short - they should never be more than a screen full of code.
- do at most one thing:
- calculate a value
- test and branch
- log
- guard and throw exception
- have a single exit point - my reasoning..
- If you later need to come back and do something to the return value, it all happens in one spot.
- Easy to set a breakpoint and inspect the returned value...
- Amounts to DRY.
I would recommend picking up two books if you're going to be doing a lot of java.
I'd recommend Effective Java and Design Patterns: Elements of Reusable Object-Oriented Software.
This may sound like a bit of circular reasoning but I find it to be true in my experience:
Refactor it a lot.
Many of the answers here (especially a strong test suite) are great advice and help a lot, so let me be clear in saying that I'm all for those pre-emptive measures as well.
At first, it's always easy to change (when it's small). Then, you generally have to go through a few bouts of tough refactoring before you have a breakthrough and get something that is really supple.