At this point I am doing testing and refactoring and have quite a lot of good results and fun as well (If everything runs OK and things get better quite easily it should be fun to continue). But now I come across a problem that should have required more thinking if done in a conventional way. Fortunately I don't do it the conventional way. To catch the thoughts that possess my mind at that moment I think its best to quote from my own log (see A.1, p.).
- I am even more surprised, when pondering about problems arising from the temporary local variable (boolean array) isPartOfQuery for the elements of the Kriterium object, I realized that applying the Replace Temp with Query refactoring could not only be helpful but also allows to restructure the createSelect() method as this is the method producing the boolean array.
- So I extract the code needed for isPartOfQuery() from createSelect(). Then I have a very strange moment when things begin to fall in place and the code really communicates to me that this is the right thing to do. Suddenly I am able to cut the createSelect() method to its basic task, creating a part of a SQL select expression for the fields of the Kriterium object that have to be retrieved from the database. The work of determining which are those fields, doesn't have to do anything with this task but is mingled before in createSelect().
- In the end I have two methods which are both well structured, small and which concentrate on their basic tasks.
- I think that is the real gain from refactoring. On the way to restructure your code you apply a refactoring and this starts a chain reaction which results in a - not previously anticipated but convincingly superb - code that does still all the things it should but is much more focused and clear. Not mentioning the removal of lots of duplicate code by working through it. I don't think one would come to this conclusion that easily when trying to imagine it with an upfront design.
- and all test still run fine ;)
- I must say when experiencing this moment of clarity, I have to agree with MARTIN FOWLER who states that refactoring is a lot of fun. In my humble opinion refactoring has even more gains than programming from scratch . You not only take a bad thing and turn it into a high quality piece of code without much thinking but you can also learn much more about the system in general and in detail, about programming style and design and about the inhoerent structure which is needed to solve a problem, but which is usually disguised by many lines of bad code.
The code mentioned above can also be found in the appendix (see A.2, p.).