Download Refactoring Patterns in Software Engineering: Lecture 10 by Raman Ramsin and more Slides Software Engineering in PDF only on Docsity! Patterns in Software Engineering Lecturer: Raman Ramsin Lecture 10 Refactoring Patterns Part 1 Department of Computer Engineering 1 Sharif University of Technology Patterns in Software Engineering – Lecture 10 Refactoring: Definition Refactoring: A change made to the internal structure of software to make it easier to understand, and cheaper to modify. The observable behavior of the software should not be changed. Department of Computer Engineering 2 Sharif University of Technology Patterns in Software Engineering – Lecture 10 Symptoms of Bad Code (1) 1. Duplicated Code 2. Long Method 3. Large Class 4. Long Parameter List Di t Ch Wh l i l h d i diff t5. vergen ange: en one c ass s common y c ange n eren ways for different reasons. 6 Shotgun Surgery: When every time you make a kind of change you. , have to make a lot of little changes to a lot of different classes. 7 Feature Envy: A method that seems more interested in a class other than. the one it actually is in. 8 Data Clumps: Bunches of data that regularly appear together Department of Computer Engineering 5 Sharif University of Technology . . Patterns in Software Engineering – Lecture 10 Symptoms of Bad Code (2) 9. Primitive Obsession: Excessive use of primitives, due to reluctance to use small objects for small tasks. 10. Switch Statements 11. Parallel Inheritance Hierarchies: Where every time you make a subclass of one class, you also have to make a subclass of another. 12. Lazy Class: A class that isn't doing enough to justify its maintenance. 13 Speculative Generality: Classes and features have been added just. because a need for them may arise someday. 14 Temporary Field: An instance variable that is set only in certain. circumstances. 15. Message Chains: Transitive visibility chains. Department of Computer Engineering 6 Sharif University of Technology Patterns in Software Engineering – Lecture 10 Symptoms of Bad Code (3) 16 Middle Man: Excessive delegation. . 17. Inappropriate Intimacy: Excessive interaction and coupling. 18. Alternative Classes with Different Interfaces: Classes that do the same thing but have different interfaces for what they do. 19. Incomplete Library Class 20. Data Class: Classes that have fields, getting and setting methods for the fields, and nothing else. 21. Refused Bequest: When subclasses do not fulfill the commitments of h lt eir superc asses. 22. Comments: When comments are used to compensate for bad code. Department of Computer Engineering 7 Sharif University of Technology Patterns in Software Engineering – Lecture 10 Composing Methods: Inline Method Inline Method A method's body is just as clear as its name . Put the method's body into the body of its callers and remove the method. Department of Computer Engineering 10 Sharif University of Technology Patterns in Software Engineering – Lecture 10 Composing Methods: Replace Method with Method Object Replace Method with Method Object You have a long method that uses local variables in such a way that you cannot apply Extract Method. Turn the method into an object so that all the local variables become fields on that object It can then be decomposed into other methods on the same object . . Department of Computer Engineering 11 Sharif University of Technology Patterns in Software Engineering – Lecture 10 Moving Features Between Objects: Move Method Mo e Methodv A method is, or will be, using or used by more features of another class than the class on which it is defined. Create a new method with a similar body in the class it uses most. Either turn the old method into a simple delegation, or remove it altogether. Department of Computer Engineering 12 Sharif University of Technology Patterns in Software Engineering – Lecture 10 Moving Features Between Objects: Inline Class Inline Class A class isn't doing very much. Move all its features into another class and delete it . Department of Computer Engineering 15 Sharif University of Technology Patterns in Software Engineering – Lecture 10 Moving Features Between Objects: Hide Delegate Hide Delegate A client is calling a delegate class of an object. Create methods on the server to hide the delegate. Department of Computer Engineering 16 Sharif University of Technology Patterns in Software Engineering – Lecture 10 Moving Features Between Objects: Remove Middle Man Remove Middle Man A class is doing too much simple delegation. Get the client to call the delegate directly. Department of Computer Engineering 17 Sharif University of Technology