Lecture "Refactoring" in Object Oriented Software Engineering course at Beaconhouse National University Lahore for Spring 2017 Semester by Hafiz Ammar Siddiqui
Role Of Transgenic Animal In Target Validation-1.pptx
7-Refactoring (Object Oriented Software Engineering - BNU Spring 2017)
1. Refactoring
FROM: HAFIZ AMMAR SIDDIQUI – COURSE: OBJECT ORIENTED SOFTWARE ENGINEERING – INSTITUTE: BEACONHOUSE NATIONAL UNIVERSITY
2. Refactoring
■ Process of changing a software system in such a way that it does not change its
external behavior but improves its internal structure
■ Restructure a software without changing its observable behavior
■ Improving the code of a software without adding new functionality or changing any
existing functionality
■ Process of cleaning up the code of a software without changing its output
– Enhances the readability of code
– Reduces the complexity in code
– Improves the maintainabilityof code
– Provides the extensibility for code
FROM: HAFIZ AMMAR SIDDIQUI – COURSE: OBJECT ORIENTED SOFTWARE ENGINEERING – INSTITUTE: BEACONHOUSE NATIONAL UNIVERSITY
3. Principles in Refactoring
FROM: HAFIZ AMMAR SIDDIQUI – COURSE: OBJECT ORIENTED SOFTWARE ENGINEERING – INSTITUTE: BEACONHOUSE NATIONAL UNIVERSITY
4. Principles in Refactoring
■ Why refactor?
– Improves the design of software
– Makes software easier to understand
– Helps in finding bugs
– Helps in programming faster
■ When to refactor?
– On adding a functionality
– On fixing an error
– On reviewing code
■ When not to refactor?
– If rewriting code from scratch
– If too much time is required
FROM: HAFIZ AMMAR SIDDIQUI – COURSE: OBJECT ORIENTED SOFTWARE ENGINEERING – INSTITUTE: BEACONHOUSE NATIONAL UNIVERSITY
5. Identifying Bad Smells
FROM: HAFIZ AMMAR SIDDIQUI – COURSE: OBJECT ORIENTED SOFTWARE ENGINEERING – INSTITUTE: BEACONHOUSE NATIONAL UNIVERSITY
6. Identifying Bad Smells
■ Duplicated code
– Extract method: If same code in multiple methods of same class
– Extract class: If same code in multiple classes
– Substitute algorithm: If same functionality at multiple places with different algorithm
■ Long method
– Extract method: If a part of a method can be separated
■ Large class
– Extract class: If a part of a class can be separated
– Extract subclass: If a part of a class can be extended into another class
■ Long parameter list
– Replace Parameter with Method: If parameters value can be obtained by a method
– Extract Parameter Object: If parameters can be grouped into an object
FROM: HAFIZ AMMAR SIDDIQUI – COURSE: OBJECT ORIENTED SOFTWARE ENGINEERING – INSTITUTE: BEACONHOUSE NATIONAL UNIVERSITY
7. Identifying Bad Smells
■ Temporary field
– Extract class: If all temporary fields can be put together separately
■ Data class
– Encapsulate Field: If class have public fields
– Remove Setting Method: If a field is read-only
■ Refused Bequest
– Push down method, Push down field: If a sibling class can be created to hold unused
■ Comments
– Extract method: If a block of code need a comment to explain what it does
– Rename method: If a method need a comment to explain what it does
FROM: HAFIZ AMMAR SIDDIQUI – COURSE: OBJECT ORIENTED SOFTWARE ENGINEERING – INSTITUTE: BEACONHOUSE NATIONAL UNIVERSITY
8. Reference
■ Refactoring: Improving the Design of Existing Code by Martin Fowler, Addison-
Wesley, 1999
FROM: HAFIZ AMMAR SIDDIQUI – COURSE: OBJECT ORIENTED SOFTWARE ENGINEERING – INSTITUTE: BEACONHOUSE NATIONAL UNIVERSITY