Refactoring and Code Smells

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

Module 7. Simplifying Conditional Expressions Course: Refactoring.
Clean code. Motivation Total cost = the cost of developing + maintenance cost Maintenance cost = cost of understanding + cost of changes + cost of testing.
A practical guide John E. Boal TestDrivenDeveloper.com.
© 2010 Shawn A. Bohner Software Construction and Evolution - CSSE 375 Even more Bad Smells in Code Shawn & Steve Q1 Shawn & Steve Hint 
Refactoring and Code Smells
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.
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
Maintenance Refactoring and Code Smells. Where are we? Over the semester we have talked about Software Engineering. The overall goal of software engineering.
COMS W4156: Advanced Software Engineering Prof. Gail Kaiser October 18, 2011COMS W41561.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Advanced Programing practices
Refactoring Cristescu Marilena. Definitions Loose Usage: Reorganize a program(or something) As a noun: a change made to the internal structure of some.
Software Refactoring Part I: Introduction Bartosz Walter Advanced Object-Oriented Design Lecture 5.
LECTURE 38: REFACTORING CSC 395 – Software Engineering.
Refactoring Improving the structure of existing code Refactoring1.
Refactoring (continued) Source: "Refactoring: Improving the Design of Existing Code", Martin Fowler.
Refactoring1 Refactoring DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING CONCORDIA UNIVERSITY February 6, 2009.
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.
Introduction to Refactoring Jim Cooper Falafel Software.
Informatics 122 Software Design II
1 CSC/ECE 517 Fall 2010 Lec. 3 Overview of Eclipse Lectures Lecture 2 “Lecture 0” Lecture 3 1.Overview 2.Installing and Running 3.Building and Running.
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.
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()){
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)
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.
Refactoring1 Improving the structure of existing code.
Pertemuan 12 Refactoring Mata kuliah: T0144 – Advanced Topics in Software Engineering Tahun: 2010.
Software Construction and Evolution - CSSE 375 Simplifying Conditionals Shawn & Steve.
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,
Refactoring. DCS – SWC 2 Refactoring ”A change made to the internal structure of software to make it easier to understand and cheaper to modify without.
Refactoring (1). Software Evolution Cope with change Feature bloat Design decay Code duplications “Pattern time is refactoring time” Make future changes.
A (Very) Simple Example Consolidate duplicate conditional fragments if (isSpecialDeal()) { total = price * 0.95; send (); } else { total = price * 0.98;
Principles and examples
Module Road Map Refactoring Why Refactoring? Examples
Reviewing Code A guide to smelling another developer’s source code.
Steve Chenoweth Office Phone: (812) Cell: (937)
Lecture 9-2: Interacting with the Superclass (super);
Un</br>able’s MySecretSecrets
SWEN-610 Foundations of Software Engineering
بازآرایی برنامه Code Refactoring
Overview of Eclipse Lectures
Refactoring III 27-Nov-18.
Code Smells 1.
Improving the structure of existing code
Refactoring and Code Smells
Refactoring III 25-Dec-18.
Advanced Programming Behnam Hatami Fall 2017.
Refactoring Types Blake Duncan.
Refactoring.
Refactoring and Code Smells
Advanced Programing practices
Refactoring and Code Smells
OF NULLNESS ANNOTATIONS
Refactoring.
Refactoring and Code Smells
Presentation transcript:

Refactoring and Code Smells Maintenance Refactoring and Code Smells

Where are we? Over the semester we have talked about Software Engineering. The overall goal of software engineering is to create high quality software efficiently. What if you don’t though? There are always pressures and reasons that software isn’t great Changing requirements Budget constraints Practice

Refactoring Definition: Refactoring modifies software to improve its readability, maintainability, and extensibility without changing what it actually does. External behavior does NOT change Internal structure is improved; easier to maintain in the future

Motivation We refactor because we understand getting the design right the first time is hard and you get many benefits from refactoring: Code size is often reduced Confusing code is restructured into simpler code Both of these greatly improve maintainability! Which is required because requirements always change!

Refactoring Example Introduce Null Object Motivation: You have many checks for null Technique: Replace the null value with a null object. Customer c = findCustomer(...); ... if (customer == null) { name = “occupant” } else { name = customer.getName() } …

Refactoring Example public class NullCustomer extends Customer { public String getName() { return “occupant” } ------------------------------------------------------------ Customer c = findCustomer() name = c.getName() Completely eliminated the if statement by replacing checks for null with a null object that does the right thing for “null” values.

Refactoring example: polymorphism Motivation: You have a conditional that chooses different behavior depending on the type of an object. Technique: Move each leg of the conditional to an overriding method in a subclass. Make the original method abstract. double getSpeed() { switch (_type) { case EUROPEAN: return getBaseSpeed(); case AFRICAN: return getBaseSpeed() - getLoadFactor() * _numberOfCoconuts; case NORWEGIAN_BLUE: return (_isNailed) ? 0 : getBaseSpeed(_voltage); } throw new RuntimeException ("Should be unreachable"); }

Refactoring example: polymorphism

When to refactor When you add functionality When you need to fix a bug Before you add new features, make sure your design and current code is “good” this will help the new code be easier to write When you need to fix a bug When you do a peer review

Identifying code smells Martin Fowler uses “code smells” to identify when to refactor. Code smells are bad things done in code, somewhat like bad patterns in code Many people have tied code smells to the specific refactorings to fix the smell

Code smells Duplicated Code Long Method bad because if you modify one instance of duplicated code but not the others, you (may) have introduced a bug! Long Method long methods are more difficult to understand performance concerns with respect to lots of short methods are largely obsolete

Code Smells Large Class Long Parameter List Divergent Change classes try to do too much, which reduces cohesion Long Parameter List hard to understand, can become inconsistent Divergent Change Related to cohesion: symptom: one type of change requires changing one subset of methods; another type of change requires changing another subset

Code Smells Lazy Class Speculative Generality Temporary Field A class that no longer “pays its way” e.g. may be a class that was downsized by a previous refactoring, or represented planned functionality that did not pan out Speculative Generality “Oh I think we need the ability to do this kind of thing someday” Temporary Field An attribute of an object is only set in certain circumstances; but an object should need all of its attributes

Code Smells Data Class Refused Bequest Comments (!) These are classes that have fields, getting and setting methods for the fields, and nothing else; they are data holders, but objects should be about data AND behavior Refused Bequest A subclass ignores most of the functionality provided by its superclass Subclass may not pass the “IS-A” test Comments (!) Comments are sometimes used to hide bad code “…comments often are used as a deodorant” (!)

Danger! Refactoring CAN introduce problems, because anytime you modify software you may introduce bugs! Management thus says: Refactoring adds risk! It’s expensive – we’re spending time in development, but not “seeing” any external differences? And we still have to retest? Why are we doing this?

Adding safety Remember that making these changes incurs some risk of introducing bugs! To reduce that risk You must test constantly – using automated tests wherever possible Use refactoring patterns – I’ve shown you two… there are more.. many more! http://www.refactoring.com/catalog/index.html Use tools! Netbeans and Eclipse both support basic refactoring (http://wiki.netbeans.org/Refactoring)

Quiz review Both refactoring and debugging improve code. How are they different? Explain the code smells and their solutions: Data class Long method Long parameter list Duplicate code Large class Divergent change Refused Bequest Comments Lazy class Speculative generality Temporary field

In-class exercise Explore using Eclipse to complete the refactoring in this assignment (from Tom Horton, Uva) http://www.cs.virginia.edu/~horton/cs494/s05/slides/lab-exercise-refactoring.htm Submit a zip folder of your modified files on Blackboard; due tomorrow (Wednesday 12/2) at midnight