Refactoring
Get Agile
Agility means being able to quickly change direction

Agile Engineering is a methodology that promotes iterations. open collaboration and adaptability throughout the lifecycle of the project


Keeping a house tidy involves having a place for everything. If everybody knows the DVDs are kept in the drawer underneath the TV and everybody takes responsibility for putting their movie back then Indiana Jones won’t disappear underneath the sofa and tears will be avoided. A house that is tidy and well organised can easily cope with an influx of rampaging kids or a visit from a frail elderly relative, its adaptable and welcoming.

In application development every line of code has its right place, and unfortunately loads of wrong places. If I don’t put the code away as soon as I spot it out of place I end up with a little mess, which easily attracts a bigger mess. Messy code like a messy house does not cope well with new challenges.

Refactoring, is the act of improving the internal design of an application without changing its functionality from the end user’s perspective. When we adopt a refactoring based approach to development our code base actually improves and gets stronger over time which is the opposite of the traditional life cycle of a software system were the integrity of the design gradually crumbles under the pressure of the demands of change, see Big Ball of Mud for more on this.

Code Smells


Agile Developers call refactoring motivations - Bad Code Smells. The code in the monthlyPayment method below looks like it really belongs in the Loan class as it uses two features from Loan and only one feature from its parent class Car. This code smell is known as feature envy, and in this case can be easily cured by applying the Move Method refactoring.

class Car { private int price; int monthlyPayment(Loan l) { int totalDue = price; for (int i=0;i

Agile Developers call refactoring motivations - Bad Code Smells.   The code in the monthlyPayment method below looks like it really belongs in the Loan class as it uses two features from Loan and only one feature from its parent class Car.  This code smell is known as feature envy, and in this case can be easily cured by applying the Move Method refactoring.   

class Car {

private int price;

            int monthlyPayment(Loan l) {  

                        int totalDue = price;

                        for (int i=0;i<l.months();i++) {

                                    totalDue=totalDue+((totalDue*l.interest())/100);

                        }

                        return totalDue/l.months();      

            }          

}

Patterns


A misapplied pattern adds complexity, which do you reckon is the simplest solution, below, the “if else” clause or the three classes?

The extra baggage that always comes with a design pattern needs to be justified otherwise the pattern is just adding clutter. Agile developers tend to rely on bad code smells to push them towards or away from patterns, rather than putting effort into producing pattern rich designs up-front. I need to be able to spot when a pattern is needed and adroitly refactor towards that pattern.

Automated Tests


Most of the simpler refactorings are now automated by all of the leading IDEs, however the more complex refactorings still need to be done manually. Without a suite of comprehensive automated tests in place a refactoring based approach is not sustainable, we are flying the high wire trapeze without a safety net.

Agile tests are also stories that enrich the domain language, they are the best kind of documentation, i.e. they communicate intent, are up to date, and contain no ambiguity. The following tests communicate the difference between a set and a list.

Zero Tolerance


Nothing attracts bad code like bad code. The more skilled you get at recognising bad code smells and refactoring the more intolerant you are likely to be of the presence of bad code. Many agile developers find themselves adopting a zero tolerance approach to poor design.

Test First Programming (TFP) is a useful micro process that allows you to effectively implement a zero tolerance approach.

AGILE SPACE Agile Home

MIND MAP

Agile Mind Map
Enlarge Mind Map

Contact us to discuss how CSE can help
emailE: cse@dcu.ie
phoneT: +3531 700 5427
Copyright © Centre for Software Engineering
Website: Red on Green Design