Leiden University. The university to discover. Leiden University. The university to discover. Leiden University. The university to discover. Software Engineering.

Slides:



Advertisements
Similar presentations
Ch:8 Design Concepts S.W Design should have following quality attribute: Functionality Usability Reliability Performance Supportability (extensibility,
Advertisements

©Ian Sommerville 2000 Software Engineering, 6th edition. Chapter 12Slide 1 Software Design l Objectives To explain how a software design may be represented.
© 2010 Shawn A. Bohner Software Construction and Evolution - CSSE 375 Even more Bad Smells in Code Shawn & Steve Q1 Shawn & Steve Hint 
General OO Concepts and Principles CSE301 University of Sunderland Harry R. Erwin, PhD.
Lecture 9 Improving Software Design CSC301-Winter 2011 – University of Toronto – Department of Computer Science Hesam C. Esfahani
Refactoring Overview  What is refactoring?  What are four good reasons to refactor?  When should you refactor?  What is a bad smell (relative to refactoring.
Classes & Objects Computer Science I Last updated 9/30/10.
Classes and Object- Oriented... tMyn1 Classes and Object-Oriented Programming The essence of object-oriented programming is that you write programs in.
OOP Design Patterns Chapters Design Patterns The main idea behind design patterns is to extract the high level interactions between objects and.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
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)
Slide 1 Systems Analysis & Design CS183 Spring Semester 2008 Dr. Jonathan Y. Clark Course Website:
Design Patterns CS is not simply about programming
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.
REFACTORING Improving the Design of Existing Code Atakan Şimşek e
Chapter 8 Object Design Reuse and Patterns. Finding Objects The hardest problems in object-oriented system development are: –Identifying objects –Decomposing.
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 Design Patterns: someone has already.
13-Jul-15 Refactoring II. Books Design Patterns is the classic book by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides Basically a catalog.
Chapter 22 Object-Oriented Design
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Maintenance Refactoring and Code Smells. Where are we? Over the semester we have talked about Software Engineering. The overall goal of software engineering.
Design Patterns Discussion of pages: xi-11 Sections: Preface, Forward, Chapter
Why Analysis Process Refer to earlier chapters Models what the system will do makes it easier for understanding no environment considered (hence, system.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Design Patterns.
Introduction to Object-oriented programming and software development Lecture 1.
Software Design Refinement Using Design Patterns Instructor: Dr. Hany H. Ammar Dept. of Computer Science and Electrical Engineering, WVU.
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
CS 325: Software Engineering March 17, 2015 Applying Patterns (Part A) The Façade Pattern The Adapter Pattern Interfaces & Implementations The Strategy.
Chapter Five An Introduction to Design Patterns Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information.
CS 350 – Software Design The Object Paradigm – Chapter 1 If you were tasked to write code to access a description of shapes that were stored in a database.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Software Refactoring Part I: Introduction Bartosz Walter Advanced Object-Oriented Design Lecture 5.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 27. Review UML dynamic view – State Diagrams.
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.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IX Interpreter, Mediator, Template Method recap.
Refactoring1 Improving the structure of existing code.
Refactoring Deciding what to make a superclass or interface is difficult. Some of these refactorings are helpful. Some research items include Inheritance.
Design Principle & Patterns by A.Surasit Samaisut Copyrights : All Rights Reserved.
ECE450S – Software Engineering II
Software Engineering CS3003 Lecture 4 Code bad smells and refactoring.
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.
Module 3. Smells Between Classes Course: Refactoring.
1 Software Maintenance and Evolution CSSE 575: Session 2, Part 1 Refactoring Principles Steve Chenoweth Office Phone: (812) Cell: (937)
More Design Patterns From: Shalloway & Trott, Design Patterns Explained, 2 nd ed.
SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring.
Refactoring1 Improving the structure of existing code.
Refactoring Agile Development Project. Lecture roadmap Refactoring Some issues to address when coding.
Pertemuan 12 Refactoring Mata kuliah: T0144 – Advanced Topics in Software Engineering Tahun: 2010.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Principles and examples
Design Patterns: MORE Examples
Steve Chenoweth Office Phone: (812) Cell: (937)
MPCS – Advanced java Programming
Introduction to Design Patterns
Behavioral Design Patterns
Advanced Programming Behnam Hatami Fall 2017.
Code Smells 1.
Improving the structure of existing code
Refactoring.
Informatics 122 Software Design II
Refactoring.
Presentation transcript:

Leiden University. The university to discover. Leiden University. The university to discover. Leiden University. The university to discover. Software Engineering “Design during construction” Leiden Institute of Advanced Computer Science Lecture Series for BSc. “Computer Science” year 2 (Fall semester 2012)

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 2 | 25 Design Patterns Overview A Recap of the Object Oriented Paradigm Design Patterns  Objective  Types of Patterns  Some Examples of Design Patterns... Abstract Factory Patterm Visitor Pattern Model-View-Controller Pattern Slides based on slides by Bart Kienhuis

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 3 | 25 Refactoring Overview Software Entropy Program starts in a well designed state New functionality is added Deforming the state… Program was not intended for the job Program has become complex Typically Attitude: “If it ain’t broke, don’t fix it”

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 4 | 25 Recap of the Object Oriented Paradigm What is inheritance?  A type of relationship between (at least) two objects in which one is a subtype of the other (which we call supertype). The subtype inherits features of the supertype Why? Reusability through extendability, simplicity and realiability

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 5 | 25 Recap of the Object Oriented Paradigm (2) What is polymorphism?  The ability to create a variable, a function, or an object that has more than one form. Why ? Objects can behave differently, depending on context. This enables simplicity and aids extensibility. What is encapsulation?  Hiding design decisions from other software components. Why? A limited protocol is presented to other software components so they are protected from details that change. This (again) enables simplicity and aids extensibility. “No component in a complex system should depend on the internal details of any other component.” (Dan Ingalls)

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 6 | 25 Recap of the Object Oriented Paradigm (3) A Class is static i.e. its attributes don't change during or after execution of a program Concept (Blueprint)  Car  Human An Object has a limited lifespan i.e. Objects are created and destroyed and its attributes may change Instantiation  My car  John What is the difference between a class and an object?

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 7 | 25 Design Pattern Book Design Patterns: Elements of Reusable Object-Oriented Software by  Gamma et al. (a.ka. the Gang of Four)  Addison Wesley Professional (Nov 10, 1994) ISBN “One of the first, and most well-received books on Design Patterns”

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 8 | 25 The Objective of Design Patterns “Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice.” – Christopher Alexander

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 9 | 25 Why Study Design Patterns? Patterns allow us to Reuse solutions By reusing already established designs, you get a head start on my problems. I do not have to reinvent solutions for commonly recurring problems. Establish common terminology Design patterns provide a a common base of vocabulary and a common viewpoint of the problem during the analysis and design phase of a project. Patterns give you a higher-level perspective on the problem and on the process of design and object orientation. Shalloway et al. (2002) Design Patterns Eplained

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 10 | 25 Why Study Design Patterns? (2) Decide whether you have the “right” design, not just one that works. Improve individual learning and team learning. Improve the modifiability of code. Facilitate adoption of improved design alternatives, even when patterns are not used explicitly. Discover alternatives to large inheritance hierarchies. Shalloway et al. (2002) Design Patterns Eplained

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 11 | 25 Design Pattern Classification Scope: specifies whether the pattern applies primarily to classes or to objects  Class patterns deal with relationships between classes and their subclasses. These relationships are established through inheritance, so they are static— fixed at compile-time.  Object patterns deal with object relationships, which can be changed at run-time and are more dynamic.  Almost all patterns use inheritance to some extent. So the only patterns labeled "class patterns" are those that focus on class relationships.  Note that most patterns are in the Object scope.

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 12 | 25 Design Pattern Classification (2) Purpose Creational Design Patterns  deal with object creation mechanisms, divided into “Object-creational patterns” (deal with object creation) “Class-creational patterns” (deal with class-instantiation) Behavioral Design Patterns Structural Design Patterns Concurrency Patterns  deal with multi-threaded programming paradigm ( )

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 13 | 25 The Facade Pattern Intent You want to simplify how to use an existing system. You need to define your own interface. Problem You need to use only a subset of a complex system. Or you need to interact with the system in a particular way. Solution The Facade presents a new interface for the client of the existing system to use. Participants and Collaborators It presents a specialized interface to the client that makes it easier to use. Consequences The Facade simplifies the use of the required subsystem. However, since the Facade is not complete, certain functionality may be unavailable to the client. Implementation Define a new class (or classes) that has the required interface and have this new class use the existing system.

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 14 | 25 The Facade Pattern (Example)

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 15 | 25 The Facade Pattern (Implementation)

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 16 | 25 (Abstract) Factory Design Pattern Problem: If an application is to be portable, it needs to encapsulate platform dependencies. Solution: Provide an interface for creating families of related or dependent objects without specifying their concrete classes. factory.svg

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 17 | 25 Factory Design Pattern (Implementation) 1. Define an abstract class that specifies which objects are to be made. 2. Then implement one concrete class for each family. Tables or files can also be used to essentially accomplish the same thing. Names of the desired classes can be kept in a database and then switches or run-time type identification (RTTI) can be used to instantiate the correct objects. “Pattern Oriented Design: Design Patterns From Analysis to Implementation” NetObjects

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 18 | 25 Visitor Design Pattern Problem: Many distinct and unrelated operations need to be performed on node objects in a heterogeneous aggregate structure. You want to avoid “polluting” the node classes with these operations. And, you don’t want to have to query the type of each node and cast the pointer to the correct type before performing the desired operation. Solution: Represent an operation to be performed on the elements of an object structure. Visitors lets you define a new operation without changing the classes of the elements on which it operates.

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 19 | 25 Visitor Design Pattern (Another View) “Head First Design Patterns” (2004) Freeman et al.

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 20 | 25 Visitor Design Pattern (Implementation) 1. Make an abstract class that represents the tasks to be performed. 2. Add a method to this class for each concrete class you started with (your original entities). 3. Add a method to the classes that you are working on to call the appropriate method in this task class, giving a reference to itself to this method. “Pattern Oriented Design: Design Patterns From Analysis to Implementation” NetObjects

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 21 | 25 Strategy Design Pattern Intent: Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from the clients that use it. Problem: The selection of an algorithm that needs to be applied depends upon the client making the request or the data being acted upon. If you simply have a rule in place that does not change, you do not need a Strategy pattern. Solution: de-couple your data, your presentation of the data and the logic for handling the events from each other.

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 22 | 25 Strategy Design Pattern (Example)

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 23 | 25 Combining Patterns Model View Controller (MVC)  consists of three kinds of objects.  The Model is the application object  The View is its screen presentation  The Controller defines the way the user interface reacts to user input.  Decoupling increases flexibility and reuse.

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 24 | 25 Combining Patterns (2) MVC is three patterns  Model is an Observer Design Pattern  As it keeps views and controllers updated  Controller and View implement a Strategy Design Pattern  As the interchangable controller describes the behaviour of the view  View uses a Factory Design Pattern  As it manages various kinds of display elements

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 25 | 25 Observer Design Pattern in the MVC

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 26 | 25 Critisism of Design Patterns The need for patterns implies insufficient abstraction ability in the applied language or technique  Many patterns are redundant in other languages Lacks formal foundations Leads to inefficient solutions  design pattern are attempts to standardize what are already accepted best practices. Might lead to unnecessary duplication of code. It is almost always a more efficient solution to use a well-factored implementation rather than a "just barely good enough" design pattern. Does not differ significantly from other abstractions  the Model-View-Controller paradigm is an example of a "pattern" which predates the concept of "design patterns" by several years.

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 27 | 25 Refactoring How to detect the opportunity to Refactor (to add value)? Set of “Smells” that indicate places that would benefit from refactoring Helps software developer to distinguish between good code and bad code Refactoring: Improving the Design of Existing code. Author: Martin Fowler Publisher: Addison Wesley ISBN:

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 28 | 25 Refactoring Refactoring: techniques to do redesign of existing code with little short-term pain. Is about not changing the functionality, but change the structure, order, and names. Happens in small steps  Renaming method  Moving a field from one class to another  Consolidate two similar methods in a superclass Design occurs continuously during design (compare this to the waterfall principle) Related principle: “Design for today, not for tomorrow”

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 29 | 25 Defining Refactoring Refactoring (noun) a change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior. Refactor (verb) to restructure software by applying a series of refactorings without changing its observable behavior.

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 30 | 25 Refactoring improves the design of software Without refactoring the design of the program will decay Poorly designed code usually takes more code to do the same things, often because the code does the same thing in different places Refactoring makes software easier to understand In most software development environments, somebody else will eventually have to read your code Refactoring helps you find bugs Refactoring helps you program faster Why Should You Refactor?

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 31 | 25 Why Should You Refactor? The Rule of three (Don Roberts) Refactor when you add function Helps you to understand the code you are modifying Sometimes the existing design does not allow you to easily add the feature Refactor when you need to fix a bug If you get a bug report its a sign the code needs refactoring because the code was not clear enough for you to see the bug in the first place Refactor as you do a code review Code reviews help spread knowledge through the development team Works best with small review groups XP pair programming is active code review taken to its limit

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 32 | 25 Refactoring and Design Upfront design XP advocates that you code the first approach that comes in to your head, get it working, and then refactor it into shape The point is that refactoring changes the role of upfront design You are no longer looking for the perfect solution when doing design but a reasonable one Refactoring can lead to simpler designs without sacrificing flexibility

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 33 | 25 Refactoring and Performance A common concern with refactoring is its effect on performance Refactoring will make software slower but it also makes the software more amenable to performance tuning The secret to fast software, in all but hard real-time contexts, is to write tunable software first and then to tune it for sufficient speed Profiling ensures that you focus your tuning efforts in the right places

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 34 | 25 Where Did Refactoring Come From? Two of the first people to recognize the importance of refactoring were Kent Beck and Ward Cunningham The worked with Smalltalk from the 80's onward Refactoring has always been an important element in the Smalltalk community Ralph Johnson's work with refactoring and frameworks has been an important contribution Bill Opdyke focused his doctoral research on developing refactoring tools for use in frameworks Outside of Fowler's book, Opdyke's doctoral thesis is the most substantial work in this area

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 35 | 25 Bad Smells in Code Duplicated Code Long Method Large Class Long Parameter List Divergent Change Shotgun Surgery Feature Envy Data Clumps Primitive Obsession Switch Statements Parallel Inheritance Hierarchies Lazy Class Speculative Generality Temporary Field Message Chains Middle Man Inappropriate Intimacy Alternative Classes with Different Interfaces Incomplete Library Class Data Class Learn to identify these smells and you have already become A much better Software Engineer !

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 36 | 25 Duplicated Code If you see the same code structure in more than one place, you can be sure that your program will be better if you find a way to unify them The simplest duplicated code problem is when you have the same expression in two methods of the same class Perform Extract Method and invoke the code from both places Another common duplication problem is having the same expression in two sibling subclasses Perform Extract Method in both classes then Pull Up Field If you have duplicated code in two unrelated classes, consider using Extract Class in one class and then use the new component in the other

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 37 | 25 Long Method The longer a procedure is the more difficult it is to understand Nearly all of the time all you have to do to shorten a method is Extract Method If you try to use Extract Method and end up passing a lot of parameters, you can often use Replace Temp with Query to eliminate the temps and slim down the long list of parameters with Introduce Parameter Object and Preserve Whole Object

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 38 | 25 Large Class When a class is trying to do too much, it often shows up as too many instance variables. When a class has too many instance variables, duplicated code cannot be far behind A class with too much code is also a breeding ground for duplication In both cases Extract Class and Extract Subclass will work

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 39 | 25 Long Parameter List With objects you don't need to pass in everything the method needs, instead you pass in enough so the method can get to everything it needs This is goodness, because long parameter lists are hard to understand, because they inconsistent and difficult to use because you are forever changing them as you need more data Use Replace Parameter with Method when you can get the data in one parameter by making a request of an object you already know about

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 40 | 25 Divergent Change Divergent change occurs when one class is commonly changed in different ways for different reasons If for example you have to change 4 different methods every time the database changes you might have a situation where two objects are better than one To clean this up you identify everything that changes for a particular cause and use Extract Class to put them all together

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 41 | 25 Shotgun Surgery This situation occurs when every time you make a kind of change, you have to make a lot of little changes to a lot of different classes When the changes are all over the place they are hard to find, and its easy to miss an important change. You want to use Move Method and Move Field to put all the changes in a single class If no current class looks like a good candidate then create one Often you can use Inline Class to bring a whole bunch of behavior together

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 42 | 25 Feature Envy A classic smell is a method that seems more interested in a class other in the one that it is in The method clearly wants to be elsewhere, so use Move Method to get it there Sometimes only part of the method suffers from envy so in that case you can use Extract Method on the jealous bit and Move Method to get it home There are several patterns that break this rule Strategy (Gang of Four) Visitor (Gang of Four) Self Delegation (Beck)

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 43 | 25 Data Clumps Often you will see the same three or four data items together in lots of places: Fields in a couple of classes Parameters in many method signatures Bunches of data that hang around together really ought to be made into their own object The first step is to look for where the clumps appear as fiels and use Extract Class to turn the clumps into an object For method parameters use Introduce Parameter Object or Preserve Whole Object to slim them down

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 44 | 25 Primitive Obsession Java has primitives for numbers, but strings and dates, which are primitives in many environment, are classes People new to objects are sometimes reluctant to use small objects for small tasks, such as money classes that combine numbers and currency ranges with an upper and lower, and special strings such as telephone numbers and ZIP codes You can use Replace Data Value with Object on individual data values If the data value has a type code, use Replace Type Code with Class if the value does not effect the behavior If you have conditional that depend on the type code use Replace Type Code with Subclass or Replace Type Code with State/Strategy

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 45 | 25 Switch Statements Most times when you see a switch statement you should consider polymorphism Use Extract Method to extract the switch statement and then Move Method to get it into the class where the polymorphism is needed If you only have a few case that effect a single method then polymorphism is overkill. In this case Replace Parameter with Explicit Methods is a good option If one of your conditional cases is null, try Introduce Null Object

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 46 | 25 Parallel Inheritance Hierarchies Is really a special case of shotgun surgery In this case every time you make a subclass of one class, you have to make a subclass of another The general strategy for eliminating the duplication is to make sure that instances of one hierarchy refer to instance of another If you use Move Method and Move Field, the hierarchy on the referring class disappears

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 47 | 25 Lazy Class Each class you create costs money and time to maintain and understand A class that is not carrying its weight should be eliminated If you have subclasses that are not doing enough try to use Collapse Hierarchy and nearly useless components should be subjected to Inline Class

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 48 | 25 Speculative Generality You get this smell when someone says "I think we need the ability to do this someday" and you need all sorts of hooks and special cases to handle things that are not required This smell is easily detected when the only users of a class or method are test cases If you have abstract classes that are not doing enough then use Collapse Hierarchy Unnecessary delegation can be removed with Inline Class Methods with unused parameters should be subject to Remove Parameter Methods named with odd abstract names should be repaired with Rename Method

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 49 | 25 Temporary Field Sometimes you will see an object in which an instance variable is set only in certain circumstances This can make the code difficult to understand because we usually expect an object to use all of its variables Use Extract Class to create a home for these orphan variables by putting all the code that uses the variable into the component You can also eliminate conditional code by using Introduce Null Object to create an alternative component for when the variables are not valid

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 50 | 25 Message Chains Message chains occur when you see a client that asks one object for another object, which the client then asks for yet another object, which the client then asks for yet another object, etc. This smell may appear as a long line of getThis methods, or as a sequence of temps Navigating this way means the client is structured to the structure of the navigation The move to use in this case is Hide Delegate at various points in the chain

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 51 | 25 Middle Man One the major features of Objects is encapsulation Encapsulation often comes with delegation Sometimes delegation can go to far For example if you find half the methods are delegated to another class it might be time to use Remove Middle Man and talk to the object that really knows what is going on If only a few methods are not doing much, use Inline Method to inline them into the caller If there is additional behavior, you can use Replace Delegation with Inheritance to turn the middle man into a subclass of the real object

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 52 | 25 Inappropriate Intimacy Sometimes classes become far too intimate and spend too much time in each other's private parts Use Move Method and Move Field to separate the pieces to reduce the intimacy If the classes do have common interests, use Extract Class to put the commonality in a safe place or use Hide Delegate to let another class act as a go- between

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 53 | 25 Alternative Classes With Different Interfaces. Most of the time you can use Rename Method on any methods that do the same thing but have different signatures for what they do If this does not go far enough that means the classes are not yet doing enough so keep using Move Method to move behavior to other classes until the protocols are the same If you have to redundantly move code to accomplish this, you may be able to use Extract Superclass

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 54 | 25 Incomplete Library Class Library builders have a tough job The problem is that if the library is insufficient for your needs it is usually impossible to modify a library class to do something that you would like it to do If there are just a couple of methods that you wish the library class had, use Introduce Foreign Method If there is more extra behavior you need, use Introduce Local Extension

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 55 | 25 Data Class Classes that have fields, getting and setting methods, And nothing else Such classes are dumb data holders If in a previous life the classes were public fields, apply Encapsulate Field

Leiden University. The university to discover. Leiden University. The university to discover. “Software Engineering” (B.Sc. Computer Science / Fall 2011) 56 | 25 Refactoring Catalog Each refactoring is described in a particular way: Each refactoring is given a specific Name. A short summary is given in which situation you need to refactor to achieve a better result. More indepth discussion on the motivation The mechanics of how to perform the refactoring. (process in steps) Worked out example