Steve Chenoweth Office Phone: (812) Cell: (937)

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

© 2010 Shawn A. Bohner Software Construction and Evolution - CSSE 375 Even more Bad Smells in Code Shawn & Steve Q1 Shawn & Steve Hint 
Refactoring Overview  What is refactoring?  What are four good reasons to refactor?  When should you refactor?  What is a bad smell (relative to refactoring.
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 3, Part 4 Big Refactorings Steve Chenoweth Office Phone: (812) Cell: (937)
1 Software Maintenance and Evolution CSSE 575: Session 2, Part 3 Moving Features Between Objects Steve Chenoweth Office Phone: (812) Cell: (937)
1 Software Maintenance and Evolution CSSE 575: Session 1, Part 4 Even more Bad Smells in Code Steve Chenoweth Office Phone: (812) Cell: (937)
1 Software Maintenance and Evolution CSSE 575: Session 6, Part 4 Breaking Dependencies 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.
25-Jun-15 Refactoring III. General philosophy A refactoring is just a way of rearranging code Refactorings are used to solve problems If there’s no problem,
REFACTORING Improving the Design of Existing Code Atakan Şimşek e
12-Jul-15 Refactoring. 2 Refactoring is: restructuring (rearranging) code......in a series of small, semantics-preserving transformations (i.e. the code.
Refactoring to Patterns Ravindra Chilaka Extreme Programming Seminar XP 2004 b.
Leiden University. The university to discover. Leiden University. The university to discover. Leiden University. The university to discover. Software Engineering.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
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.
1 Software Maintenance and Evolution CSSE 575: Session 1, Part 3 Bad Smells in Code - 1 Steve Chenoweth Office Phone: (812) Cell: (937)
Refactoring - A disciplined approach to rework for better design.
Refactoring Improving the structure of existing code Refactoring1.
17-Oct-15 Refactoring. 2 Refactoring is: restructuring (rearranging) code......in a series of small, semantics-preserving transformations (i.e. the code.
Chapter 7: Bad Code Smells Omar Meqdadi SE 3860 Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
CSSE 374: More GRASP’ing for Object Responsibilities
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.
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: 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.
Refactoring II Dealing with Polymorphism. Switch in Rental Switches on Movie! class Rental … public double getCharge() { double result = 0; switch (getMovie().getPriceCode()){
Module 3. Smells Between Classes Course: Refactoring.
Software Testing and Maintenance Agile Software Development Jeff Offutt SWE 437 George Mason University 2008 Based on Agile Estimating and Planning, Cohn,
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)
SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring.
CSC 4700 Software Engineering
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.
10-Jun-16.  Refactoring is: ◦ restructuring (rearranging) code... ◦...in a series of small, semantics-preserving transformations (i.e. the code keeps.
A (Very) Simple Example Consolidate duplicate conditional fragments if (isSpecialDeal()) { total = price * 0.95; send (); } else { total = price * 0.98;
Principles and examples
Steve Chenoweth Office Phone: (812) Cell: (937)
Steve Chenoweth Office Phone: (812) Cell: (937)
Inheritance and Polymorphism
Intent (Thanks to Jim Fawcett for the slides)
Refactoring and Code Smells
Software Construction and Evolution - CSSE 375 Composing Methods
Refactoring III 27-Nov-18.
Code Smells 1.
Improving the structure of existing code
Refactoring and Code Smells
Refactoring III 25-Dec-18.
Refactoring.
Refactoring and Code Smells
Refactoring and Code Smells
HFOOAD Chapter 5 Interlude
Refactoring.
Refactoring and Code Smells
Presentation transcript:

Software Maintenance and Evolution CSSE 575: Session 1, Part 4 Even more Bad Smells in Code Steve Chenoweth Office Phone: (812) 877-8974 Cell: (937) 657-3885 Email: chenowet@rose-hulman.edu What is the most prevalent bad smell on the stink parade (i.e., the bad smell that is most often encountered)? http://www.denverscleaningservice.com/3-countries-where-its-okay-to-smell-bad/

Refactoring Indicators: 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 Lazy Class Parallel Interface Hierarchies Speculative Generality Temporary Field Message Chains Middle Man Inappropriate Intimacy Incomplete Library Class Data Class Refused Bequest Alternative Classes w/ varied interfaces Comments Talk to the right column today…

Parallel Inheritance Hierarchies Situation: Every time a subclass of one class is made, another subclass must also be made Special case of Shotgun Surgery Solution: General Strategy is to eliminate the duplication to ensure that instances of one hierarchy refer to instances of the other (Move Method, Move Field)  As before – we’ll focus on the situations for now. Your job in class will be to FIND the smells in your code.

Speculative Generality Situation: Presence of hooks and special cases for things that are not required, but may have been anticipated. Extra classes and features add to complexity Spotted by when only users are test cases Solution: Remove unused classes and methods. If you really do need it later, you can add it back in. (Collapse hierarchy, inline class, remove parameter) We might need this one day… What is the problem that is represented by speculative generality?

Message Chains Situation: Client asks for a sub-object, that asks for a sub-object, … Multi-layer “drill down” may result in sub-sub-sub-objects being passed back to requesting client Solution: Rethink abstraction and examine why deeply nested subpart is surfacing Why is the subpart so simple that it’s useful far from home? Use Hide Delegate to resurface objects and remove unnecessary indirections What is the solution for dealing with Message Chains bad smell?

Middle Man Situation: Too many levels of indirection If too many of a class’s methods beg services of delegate sub-objects, the basic abstraction is probably compromised Solution: Remove unnecessary levels of indirection using Remove Middle Man or Replace Delegation with Inheritance. “All hard problems in software engineering can be solved by an extra level of indirection.” DPs pretty well all boil down to this, albeit in quite clever and elegant ways. What is the problem with too much indirection? [[Abstraction is probably compromised and too much delegation is going on]]

Timeout to look at another situation… What’s wrong with this switcheroo? class Animal { final int MAMMAL = 0, BIRD = 1, REPTILE = 2; int myKind; // set in constructor ... String getSkin() { switch (myKind) { case MAMMAL: return "hair"; case BIRD: return "feathers"; case REPTILE: return "scales"; default: return "integument"; } } } Switch statement could be subclasses instead. As noted in class, you need to consider if this particular one is really “bad.” If you see this same sort of switch, for these cases, all over your code, then it really needs to become subclasses. If it happens once, and avoids duplicating code for each case, then maybe it’s ok!

This helps… class Animal { String getSkin() { return "integument"; } } class Mammal extends Animal { String getSkin() { return "hair"; } } class Bird extends Animal { String getSkin() { return "feathers"; } } class Reptile extends Animal { String getSkin() { return "scales"; } } In a lot of cases, the original switch statement was in some other class. So, the getSkin method probably needs to move to the Animal class instead of wherever it was originally, with that abstraction or default overridden by what you see here, for each subclass.

How is this an improvement? Adding a new animal type, such as Amphibian, does not require revising and recompiling existing code Mammals, birds, and reptiles are likely to differ in other ways, and we’ve already separated them out (so we won’t need more switch statements) We’ve gotten rid of the flags we needed to tell one kind of animal from another Basically, we’re now using Objects the way they were meant to be used

More situations: Inappropriate Intimacy Situation: Sharing of secrets between classes, especially outside the sanctioned bounds of inheritance e.g., public variables, indiscriminate definitions of get/set methods Solution: Rethink basic abstraction and introduce appropriate use of get/set methods. Merge classes when intimacy warranted. (Move/Extract Method/Field, Change Bidirectional Association to Unidirectional, Hide Delegate) Leads to data coupling, intimate knowledge of internal structures and implementation decisions Makes clients brittle, hard to evolve, easy to break. What is the problem with inappropriate intimacy between classes and what does this situation lead to?

Alternative Classes with Different Interfaces Situation: Classes/methods seem to implement the same or similar abstraction, yet are otherwise unrelated Not against overloading, just haphazard design Solution: Move the classes “closer” together. Find a common interface. Find a common subpart and remove it. (Extract [Super]Class, Move Method, Rename Method)

Data Class Situation: Class consists of (simple) data fields and simple accessor/mutator methods only Only dumb data holders Often, you find clients using only get/set methods Solution: Examine client usage patterns and abstract some commonalities of usage into methods and move some behavior into data class. (Encapsulate Field/Collection, Remove Setting Method, Extract/Move Method) “Data classes are like children. They are OK as a starting point, but to participate as a grownup object, they need to take on some responsibility.” What is the solution to the Data Class problem? You don’t HAVE to have complex algorithms in every class where you keep data. But, you should at least ask if these methods are elsewhere and really belong here.

Refused Bequest Situation: Subclass inherits methods/data, but doesn’t seem to use some of them Fowler says this is not as bad a smell as others 9 times out of 10, this smell is not worth cleaning Address it when there is confusion or problems Solution: Create a new sibling class and push all the unused methods into the sibling – this way abstract parent contains commonality. Then use delegation (Push down Method/Field, Replace Inheritance with Delegation) In a sense, this might be a good sign: The parent manages the commonalities and the child manages the differences. Might want to look at typical client use to see if clients think child is-a parent When is Refused Bequest needing to be addressed?

Comments Situation: Long comments are often a sign of opaque, complicated, inscrutable code If comments are not simply rationale, consider restructuring code to be more self-evident Solution: Make methods short and use long identifiers. Ensure comments largely document rationale. (Extract Method, Rename Method) Agile methods discourage use of comments (!), though, obviously they are necessary for some kinds of clarification. What is the sign that comments are a bad smell?

Temporary Field Situation: An object in which an instance variable is set only in certain circumstances Such code is difficult to understand since you expect a class to need all it’s variables Solution: Create a legitimate home for the orphan variables (Extract Class) Put all the code that concerns the variable into a component and eliminate conditional code (Introduce Null Object)

Comment caution The coding world is currently divided into two camps: You should be able to code “descriptively” in such a way that you rarely need comments. And, comments themselves are a pain because they don’t get updated when the code’s changed. You must use descriptive comments or, two years later, no one will be able to decrypt what the code does.

Incomplete Library Class Situation: Library class doesn’t have the functions that you need and it is difficult to modify for your purpose Reuse touted as valuable – reuse ill-designed is troublesome… Solution: Special purpose tools to make the class “usable” by introducing foreign methods and extension (Introduce Foreign Method, Introduce Local Extension) What are the special purpose refactoring tools to make a challenging library class usable? [[Introduce Foreign Method, Introduce Local Extension]]

Assignment and Milestone Reminders Readings in Fowler – Catch up: Focus on Chapter 3 – Bad Smells. Do these all make sense now? Stay up: Read Ch 2 for next week – “principles in refactoring” Delve into: Read Ch 6, 7, 8 – specific refactoring methods Journal turnin on Moodle. Mon 7 AM? Milestone 1 – ID your project. Mon 7 AM? Next week, we’ll start implementing refactoring. You also can review the general “solutions” noted on this week’s “bad smells” slides, as a start.