Refactoring
Table of Contents
Refactoring is the modification of software source code to improve software qualities. … while preserving functionality. Enhancement is not refactoring. Refactoring is part of maintenance
1 Code Refactoring
- Software Quality
- Remove "bad smells"
- Functional equivalence: before v after
- Performance?
- Other quality issues
- Refactoring is exclusive of bug fixes.
- http://tedfelix.com/software/refactoring.html is a list extracted from Martin Fowlers writings.
- http://autorefactor.org/ Site looks old-fashined, but content is good. Collection of Examples auto- refactored.
2 Bad Code Smells
- Falling short of some goals.
- Comments poorly done
- Nesting depth too deep
- Duplicated code
- Long method
- Large class. "God object"?
2.1 Bad Code Smells #2
- Refused Bequest, contract of the base class
- Feature Envy: A class that uses methods of another class excessively.
- Inappropriate Intimacy: dependencies on implementation details of another.
- Data Clumps: "… consider deleting one of the data values: if you did this, the others do not make sense, …"
- Too many parameters, Lazy class / Freeloader, Excessively long identifiers, Excessively short identifiers, Excessive use of literals
2.2 Bad Code Smells #3
- Divergent Change "occurs when one class is commonly changed in different ways for different reasons."
- Primitive Obsession: "People new to objects usually are reluctant to use small objects for small tasks, …"
- Shotgun Surgery: "You whiff this when every time you make a kind of change, you have to make a lot of little changes to a lot of different classes."
2.3 Holzmann's "Ten Rules" to Have Good Smells
- Restrict to simple control flow constructs.
- Give all loops a fixed upper-bound.
- Do not use dynamic memory allocation after initialization.
- Limit functions to no more than 60 lines of text.
- Use minimally two assertions per function on average.
- Declare data objects at the smallest possible level of scope.
- Check the return value of non-void functions, and check the validity of function parameters.
- Limit the use of the preprocessor to file inclusion and simple macros.
- Limit the use of pointers. Use no more than two levels of dereferencing per expression.
- Compile with all warnings enabled, and use one or more source code analyzers.
3 OO Design Patterns
4 Big Refactorings
- Tease Apart Inheritance tries to tame a messy inheritance hierarchy.
- Convert Procedural Design to Objects
- Separate Domain from Presentation moves domain logic out of the UI classes.
- Extract Hierarchy introduces polymorphism to replace complex conditional code.
- Rearchitect: Client/ Server
- Rearchitect: Engine + GUI
5 Anti-Patterns
6 References
- Sir Charles Anthony Richard Hoare on Software Quality, http://onlinelibrary.wiley.com/doi/10.1002/spe.4380020202/epdf, Software – Practice and Experience, 103-105, 1972. Turing Award Winner. Required Reading.
- http://en.wikipedia.org/wiki/Refactoring Required Visit/Reading the individual refactoring techniques also.
- Kent Beck and Martin Fowler, http://martinfowler.com/ and http://refactoring.com/catalog/ A catalog of several patterns. Describes many of the smells discussed. https://sourcemaking.com/ also has most of the materials presented well. [Martin Fowler claims that much of their content is his.]; Martin Fowler, Refactoring: Improving the Design of Existing Code, book, Addison-Wesley, 2000. [Well .., not design improvement, but code improvement.] http://tedfelix.com/software/refactoring.html is a list extracted from Martin Fowlers writings. All sites: Required Visits.
- Gerard J. Holzmann, The Power of Ten – Rules for Developing Safety Critical Code,'' IEEE Computer, June 2006, pp. 93-95. http://spinroot.com/gerard/pdf/P10.pdf Required Reading.
- Mantyla, M. V. and Lassenius, C, "Subjective Evaluation of Software Evolvability Using Code Smells: An Empirical Study", Journal of Empirical Software Engineering, vol. 11, no. 3, 2006, pp. 395-431. http://www.soberit.hut.fi/~mmantyla/ESE_2006.pdf Skim Reading.
- Refactoring PDF Slides by Prof Kim ./refactoring-ut-austin-2013.pdf Required Reading.