Module 3. Smells Between Classes Course: Refactoring.

Slides:



Advertisements
Similar presentations
12-Dec-14 Refactoring IV. Previously discussed bad smells Duplicated code — and other forms of redundancy Long method — use short methods that delegate.
Advertisements

Test-Driven Development and Refactoring CPSC 315 – Programming Studio.
About Me – Frank Xu Education ▫ North Dakota State University  Ph.D. in Software Engineering ▫ Towson University  MS in Computer Science ▫ Southeast.
© 2010 Shawn A. Bohner Software Construction and Evolution - CSSE 375 Even more Bad Smells in Code Shawn & Steve Q1 Shawn & Steve Hint 
You want me to do what??? Refactoring legacy applications aka : fixing someone else’s “bad” code Niel Zeeman Team Foundation Consulting
Gregor Gisler-Merz BrownBag Session Refactoring.
Refactoring Overview  What is refactoring?  What are four good reasons to refactor?  When should you refactor?  What is a bad smell (relative to refactoring.
Lectures 17 and 18 A Refactoring Micro-Example FOR0383 Software Quality Assurance 5/16/20151Dr Andy Brooks Refactoring is really easy using this tool.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Software Construction and Evolution - CSSE 375 Bad Smells in Code Shawn Bohner & Steve Chenoweth.
1 Software Maintenance and Evolution CSSE 575: Session 1, Part 4 Even more Bad Smells in Code Steve Chenoweth Office Phone: (812) Cell: (937)
Introduction to Refactoring Excerpted from ‘What is Refactoring?’ by William C. Wake and Refactoring: Improving the Design of Existing Code by Martin Fowler.
George Blank University Lecturer. REFACTORING Improving the Design of Existing Code Supplement to Ian Sommerville, Software Engineering, Chapter 20 Prepared.
XP and Refactoring David Talby. Development Methodologies The Software Crisis – 84% of software projects are not on time – 31% of software projects never.
REFACTORING Improving the Design of Existing Code Atakan Şimşek e
“Blink-factoring”: Software engineering meets pop psychology Presented by Jamie Hohman EECS 816: Object-Oriented Software Development April 22, 2008.
Leiden University. The university to discover. Leiden University. The university to discover. Leiden University. The university to discover. Software Engineering.
Maintenance Refactoring and Code Smells. Where are we? Over the semester we have talked about Software Engineering. The overall goal of software engineering.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Refactoring. Refactoring is a step in the software design process.
1 Software Maintenance and Evolution CSSE 575: Session 1, Part 3 Bad Smells in Code - 1 Steve Chenoweth Office Phone: (812) Cell: (937)
LECTURE 38: REFACTORING CSC 395 – Software Engineering.
Software Engineering Modern Approaches Eric Braude and Michael Bernstein 1.
Refactoring - A disciplined approach to rework for better design.
Refactoring Improving the structure of existing code Refactoring1.
Refactoring (continued) Source: "Refactoring: Improving the Design of Existing Code", Martin Fowler.
When and How to Refactor? Refactoring Patterns Alexander Vakrilov Telerik Corporation Senior Developer and Team Leader.
Best Practices. Contents Bad Practices Good Practices.
Refactoring1 Improving the structure of existing code.
The effectiveness of refactoring based on a compatibility testing taxonomy and a dependency graph Steve Counsell and Robert Hierons, Brunel University,
Refactoring Deciding what to make a superclass or interface is difficult. Some of these refactorings are helpful. Some research items include Inheritance.
Introduction to Refactoring Jim Cooper Falafel Software.
Informatics 122 Software Design II
Incremental Design Why incremental design? Goal of incremental design Tools for incremental design  UML diagrams  Design principles  Design patterns.
Software Engineering CS3003 Lecture 4 Code bad smells and refactoring.
Refactoring and such ● (Side note) Specialization ● Key terms ● Abstraction, state, persistence and association and their relationship to software development.
Refactoring: Code Smells. Admin Notes REGISTER FOR BLACKBOARD Watch blackboard site for updates on class as hurricane season approaches.
Refactoring. Refactoring Overview  What is refactoring?  What are four good reasons to refactor?  When should you refactor?  What is a bad smell (relative.
Refactoring 2. Admin Blackboard Quiz Acknowledgements Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of.
REFACTORINGREFACTORING. Realities Code evolves substantially during development Requirements changes 1%-4% per month on a project Current methodologies.
Relationships Relationships between objects and between classes.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
NJIT 1 Test Driven Development and Refactoring Larman, Chapter 21.
Refactoring II Dealing with Polymorphism. Switch in Rental Switches on Movie! class Rental … public double getCharge() { double result = 0; switch (getMovie().getPriceCode()){
1 Software Maintenance and Evolution CSSE 575: Session 2, Part 1 Refactoring Principles Steve Chenoweth Office Phone: (812) Cell: (937)
1 Software Maintenance and Evolution CSSE 575: Session 3, Part 3 Dealing with Generalization Steve Chenoweth Office Phone: (812) Cell: (937)
Refactoring. Mathematics: Factor ● fac·tor – One of two or more quantities that divides a given quantity without a remainder, e.g., 2 and 3 are factors.
CSC 4700 Software Engineering
CSSE 375 Organizing Data – Part 2 Shawn and Steve Continue the same quiz!
Refactoring1 Improving the structure of existing code.
Pertemuan 12 Refactoring Mata kuliah: T0144 – Advanced Topics in Software Engineering Tahun: 2010.
Refactoring. 2 Process of changing a software system in such a way that it does not alter the external behavior of the code, yet improves its internal.
CSSE 375 Organizing Data – Part 1 Shawn and Steve Q1.
Software Construction and Evolution - CSSE 375 Dealing with Generalization Steve and Shawn Left – In the 1990 movie “The Freshman,” Matthew Broderick,
Module 9. Dealing with Generalization Course: Refactoring.
A (Very) Simple Example Consolidate duplicate conditional fragments if (isSpecialDeal()) { total = price * 0.95; send (); } else { total = price * 0.98;
Principles and examples
Catalog of Refactoring
The Movement To Objects
Steve Chenoweth Office Phone: (812) Cell: (937)
Steve Chenoweth Office Phone: (812) Cell: (937)
Software Construction and Evolution - CSSE 375 Composing Methods
بازآرایی برنامه Code Refactoring
Code Smells 1.
Improving the structure of existing code
Refactoring and Code Smells
Refactoring Types Blake Duncan.
Refactoring.
Refactoring.
Presentation transcript:

Module 3. Smells Between Classes Course: Refactoring

Overview: Smells Between Classes Data Inheritance Responsibility Accommodating Change Library Classes

Lesson: Data Primitive Obsession Data Class Data Clump Temporary Field

Primitive Obsession Symptoms  Uses of the primitive or near-primitive types (int, float, String, etc.)  Constants and enumerations representing small integers  String constants representing field names Causes  Missing class  Simulated types  Simulated field accessors What to Do  Replace Data Value with Object  Replace Type Code with Class; Replace Type Code with Subclass; Replace Type Code with State/Strategy; Replace Conditional with Polymorphism  Replace Array with Object Payoff  Improves communication.

Data Class Symptoms  class consists only of public data members  depend on the mutability and representation of the class Causes  realize that some data is part of an independent object What to Do  Encapsulate Field  Remove Setting Methods  Encapsulate Collection  Look at each client of the object. Payoff  Improves communication.

Data Clump Symptoms  the same items frequently appear together in classes and parameter lists.  there are groups of fields and methods together within the class  groups of field names start or end with similar substrings. Causes  The items are typically part of some other entity What to Do  Extract Class.  Introduce Parameter Object  Preserve Whole Object  Move Method, etc Payoff  Improves communication. May expose duplication. Usually reduces size.

Temporary Field Symptoms  A field is set only at certain times, and it is null (or unused) at other times. Causes  an algorithm that passes around information through the fields What to Do  Extract Class Payoff  Improves communication and clarity.

Exercise: Alternative Representations Proper Names (Challenging)

Lesson: Inheritance Refused Bequest Inappropriate Intimacy (Subclass Form) Lazy Class See also these other smells related to inheritance:  Simulated Inheritance (Switch Statement)  Parallel Inheritance Hierarchies  Combinatorial Explosion

Refused Bequest Symptoms  honest refusal  implicit refusal  through a handle to the subclasses.  Inheritance doesn't really make sense Causes  a class may inherit without really intending the class to be substitutable  a conscious decision to let subclasses deny use of some features What to Do  you might decide to leave it as is.  Replace Inheritance with Delegation.  Extract Subclass, Push Down Field, and Push Down Method

Inappropriate Intimacy (Subclass Form) Symptoms  A class accesses internal (should-be-private) parts of its parent. Causes  Parent and child classes be more coupled together What to Do  Self Encapsulate Field.  Form Template Method.  Replace Inheritance with Delegation. Payoff  Reduces duplication.  Often improves communication.  May reduce size.

Lazy Class Symptoms  A class isn't doing. Causes  all responsibilities have been moved to other places What to Do  Collapse Hierarchy.  Inline Class. Payoff  Reduces size. Improves communication. Improves simplicity.

Exercise: Collection classes Real application

Lesson: Responsibility Feature Envy Inappropriate Intimacy (General Form) Message Chains Middle Man

Feature Envy Symptoms  A method seems to be focused on manipulating the data of other classes rather than its own. Causes  This is very common among clients of current and former data classes, but you can see it for any class and its clients. What to Do  Move Method  Extract Method

Inappropriate Intimacy (General Form) Symptoms  One class accesses internal (should-be-private) parts of another class Causes  The two classes probably became intertwined a little at a time.  There may be a missing class that should mediate between them. What to Do  Move Method and Move Field  Extract Class and Hide Delegate  Change Bidirectional Reference to Unidirectional  If a subclass is too coupled to its parent Self Encapsulate Field. Form Template Method. Replace Inheritance with Delegation.

Message Chains Symptoms  You see calls of the form: a.b().c().d() Causes  the problem is that this couples both the objects and the path to get to them. What to Do  Extract Method and Move Method  Use Hide Delegate

Middle Man Symptoms  object:f() { delegate.f(); } Causes  result from applying Hide Delegate to address Message Chains What to Do  Remove Middle Man  Replace Delegation with Inheritance. Payoff  Reduces size.

Exercise: Middle Man Account Cart

Exercise: Hide delegate

Lesson: Accommodating Change Divergent Change Shotgun Surgery Parallel Inheritance Hierarchies Combinatorial Explosion

Divergent Change Symptoms  You find yourself changing the same class for different reasons. Causes  The class picks up more responsibilities as it evolves What to Do  Extract Class  Extract Superclass or Extract Subclass

Shotgun Surgery Symptoms  Simple change requires to change several classes. Causes  One responsibility is split among several classes.  Elimination Divergent Change. What to Do  Extract Class  Move Field and Move Method.

Parallel Inheritance Hierarchies Symptoms  required subclass in another hierarchy.  subclasses have the same prefix  a special case of Shotgun Surgery Causes  The hierarchies probably grew in parallel, a class and its pair being needed at the same time. What to Do  Move Field and Move Method Payoff  Reduces duplication. May improve communication. May reduce size.

Combinatorial Explosion Symptoms  you have to introduce multiple classes at various points of the hierarchy.  you notice that each layer of the hierarchy uses a common set of words Causes  independent decisions instead get implemented via a hierarchy. What to Do  Replace Inheritance with Delegation  Tease Apart Inheritance

Exercise: Divergent Changes (csv writer) Shotgun Surgery Documents

Exercise: Shotgun Surgery In code you have access to, find examples of this problem. Some frequent candidates:  Configuration information.  Logging.  Persistence.  Sometimes it takes two calls on an object to get something common done, and this two-step process is used in several places.

Lesson: Library Classes Incomplete Library Class

Symptoms  library has no some features Causes  the author of the library class didn't anticipate your need What to Do  contact owner  Introduce Foreign Method  Introduce Local Extension  introduce a layer covering the library

Exercise:

Redraw this as a UML package diagram showing dependencies. Explain how the bulk of your code does or does not depend on the library code in each of these situations. What effects does this layering have in terms of:  Conceptual integrity?  Portability?  Performance?  Testing?

Exercise:

Review Data Inheritance Responsibility Accommodating Change Library Classes

Gen-A-Refactoring (—) in combinations that don't make sense, (+) in those that are in Fowler's Refactoring catalog (*) in those that make sense but aren't in the catalog.