Refactoring – III Measured Smells. Smells Covered 1. Comments 2. Long method 3. Large Class 462.

Slides:



Advertisements
Similar presentations
Module 7. Simplifying Conditional Expressions Course: Refactoring.
Advertisements

Clean code. Motivation Total cost = the cost of developing + maintenance cost Maintenance cost = cost of understanding + cost of changes + cost of testing.
1 Software Maintenance and Evolution CSSE 575: Session 2, Part 2 Composing Methods Steve Chenoweth Office Phone: (812) Cell: (937)
Refactoring Overview  What is refactoring?  What are four good reasons to refactor?  When should you refactor?  What is a bad smell (relative to refactoring.
Software Construction and Evolution - CSSE 375 Bad Smells in Code Shawn Bohner & Steve Chenoweth.
Introduction to Refactoring Excerpted from ‘What is Refactoring?’ by William C. Wake and Refactoring: Improving the Design of Existing Code by Martin Fowler.
REFACTORING Improving the Design of Existing Code Atakan Şimşek e
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.
Algorithm Programming Coding Advices Bar-Ilan University תשס " ו by Moshe Fresko.
What Is a Factory Pattern?.  Factories are classes that create or construct something.  In the case of object-oriented code languages, factories construct.
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 5 CIS 6101 Software Processes and Metrics.
Chapter 7 Designing Classes. Class Design When we are developing a piece of software, we want to design the software We don’t want to just sit down and.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Refactoring Cristescu Marilena. Definitions Loose Usage: Reorganize a program(or something) As a noun: a change made to the internal structure of some.
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Software Refactoring Part I: Introduction Bartosz Walter Advanced Object-Oriented Design Lecture 5.
The Java Programming Language
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.
Refactoring Improving the structure of existing code Refactoring1.
Small changes to code to improve it. Refactoring Defined A change made to the internal structure of software to make it easier to understand and cheaper.
Chapter 22 Developer testing Peter J. Lane. Testing can be difficult for developers to follow  Testing’s goal runs counter to the goals of the other.
Refactoring1 Improving the structure of existing code.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
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.
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.
M1G Introduction to Programming 2 5. Completing the program.
1 Software Maintenance and Evolution CSSE 575: Session 3, Part 3 Dealing with Generalization Steve Chenoweth Office Phone: (812) Cell: (937)
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring.
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.
Refactoring1 Improving the structure of existing code.
Software Construction and Evolution - CSSE 375 Simplifying Conditionals Shawn & Steve.
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
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.
Module 9. Dealing with Generalization Course: Refactoring.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
Module 9: Operator overloading #1 2000/01Scientific Computing in OOCourse code 3C59 Module 9: Operator Overloading In this module we will cover Overloading.
Catalog of Refactoring (5) Simplifying Conditional Expressions.
Introduction to Exceptions in Java CS201, SW Development Methods.
CSCE 240 – Intro to Software Engineering Lecture 3.
Catalog of Refactoring (1) Composing Methods. Code Smells Long methods Dubious temporary variables Dubious methods.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
ICONFINDER ICONFINDER Founded Django based web application -PostgreSQL -Elasticsearch -Amazon Elastic Compute.
Catalog of Refactoring (6) Making Method Calls Simpler.
A (Very) Simple Example Consolidate duplicate conditional fragments if (isSpecialDeal()) { total = price * 0.95; send (); } else { total = price * 0.98;
Principles and examples
Summary prepared by Kirk Scott
Catalog of Refactoring
Module Road Map Refactoring Why Refactoring? Examples
C++ coding standard suggestion… Separate reasoning from action, in every block. Hi, this talk is to suggest a rule (or guideline) to simplify C++ code.
Refactoring and Code Smells
Refactoring with inline temp, method, and class
Software Construction and Evolution - CSSE 375 Composing Methods
Code Smells 1.
Improving the structure of existing code
Refactoring and Code Smells
Refactoring Types Blake Duncan.
Refactoring and Code Smells
Refactoring.
Refactoring and Code Smells
Presentation transcript:

Refactoring – III Measured Smells

Smells Covered 1. Comments 2. Long method 3. Large Class 462

1. Comments Symptoms: symbols (// or /*) appear on code. Some IDEs color them. Causes: Present for many reasons; Author realizes that something isn’t as clear and adds a comment. – Some comments may be helpful Tell why something is done a particular way (or not) Cite an algorithm not obvious (when a simpler algorithm won’t do) Other times: comment not necessary; goal of a routine may be communicated via its name 463

Comments What to do: – Extract Method: When comment explains block of code, can often use this one to pull the block into a separate method. – Comment will often suggest a name for the new method – Rename Method: When comment explains what a method does better than the method’s name use comment as basis of the new name – Introduce Assertion: When comment explains preconditions, consider this one to replace the comment with code. Payoff: – Improves communication. May expose duplication Contraindications – Don’t delete comments pulling their own weight. 464

Comments – Extract Method Void printOwing (double amount){ printBanner(); // print details System.out.println (“name:” + name); System.out.println (“amount:” + amount); Change to: void printOwing (double amount){ printBanner(); printDetails (amount); }// end printOwning() void printDetails(double amount) { System.out.println (“name:” + name); System.out.println (“amount:” + amount); }// end printDetails() 465

Comments – Extract Method Motivation Extract Method is a very common refactoring. Look at long method or code that needs a comment to understand its purpose. Turn fragment of code into its own method. Prefer short, well-named methods; several reasons. First, it increases the chances that other methods can use a method when the method is finely grained. Second, it allows the higher-level methods to read more like a series of comments. – Overriding also is easier when the methods finely grained. 466

Mechanics of Extract Method 1. Create new method Name after the intention of the method – (name it by what it does, not by how it does it). If code you want to extract is very simple, such as a single message or function call, can extract if name of new method will reveal intention of the code in a better way. If you can’t come up with a more meaningful name, don’t extract the code. Copy the extracted code from the source method into the new target method. 467

Mechanics (continued) 2. Scan the extracted code for references to any variables that are local in scope to the source method. These are local variables and parameters to the method. See whether any temporary variables are used only within this extracted code. If so, declare them in the target method as temporary variables. 468

Mechanics (continued) Scope of Variables: Look to see whether any of these local-scope variables are modified by the extracted code. – If one variable is modified, see whether you can treat extracted code as a query and assign result to variable concerned. If this is awkward, or if there is more than one such variable, you can’t extract the method as it stands. – You may need to use Split Temporary Variable and try again. 469

Mechanics Pass Variables- Pass variables to target method as parameters local-scope variables read from extracted code. Compile after dealing with all locally-scoped variables. Replace extracted code in source with call to target method. If you moved temporary variables over to target method, look to see whether they were declared outside of the extracted code. If so, you can now remove the declaration. Compile and test. 4610

Comments – Rename Method The name of a method does not reveal its purpose. 02Change the name of the method. 4611

Comments – Rename Method Author advocates small methods to factor complex processes. Done badly, can lead to merry dance to find out what all the little methods do. Key: name methods that communicate their intention. – Good way: Think what comment for method would be. – Turn comment into name of method – If you see a badly named method, CHANGE IT! 4612

Comments – Rename Method Motivation Remember your code is for a human first and a computer second. – Humans need good names. Good naming is a skill and is key to being a truly skillful programmer. Same applies to other aspects of the signature. If reordering parameters clarifies matters, do it. (see Add Parameter and Remove Parameter ). 4613

Comments – Rename Method Mechanics Check to see whether the method signature is implemented by a superclass or subclass. If so, perform these steps for each implementation. –Declare a new method with the new name. –Copy old body of code to new name; make alterations. –  Compile. –Change body of old method so it calls new one. –  Compile and test. –Find all references to old method name and change them to refer to the new one. –  Compile and test after each change. –Remove the old method. If the old method is part of the interface and you cannot remove it, leave it in place and mark it as deprecated. –  Compile and test. 4614

Comments – Rename Method Example Have a method to get a person’s telephone number: public String getTelephoneNumber() { return ("(" + _officeAreaCode + ") " + _officeNumber); } Want to rename method to getOfficeTelephoneNumber. Create new method and copy body to new method. Change Old method to call the new one: class Person... public String getTelephoneNumber(){ return getOfficeTelephoneNumber(); } public String getOfficeTelephoneNumber() { return ("(" + _officeAreaCode + ") " + _officeNumber); } Find callers of old method, Switch them to call the new one. After switching, can remove the old method. 4615

Comments – Introduce Assertion A section of code assumes something about the state of the program. Make the assumption explicit with an assertion. double getExpenseLimit() { // should have either expense limit or a primary project return (_expenseLimit != NULL_EXPENSE) ? expenseLimit: primaryProject.getMemberExpenseLimit(); }// end getExpenseLimit() double getExpenseLimit() { Assert.isTrue (expenseLimit != NULL_EXPENSE || primaryProject != null); return (expenseLimit != NULL_EXPENSE) ? expenseLimit: primaryProject.getMemberExpenseLimit(); }// end getExpenseLimit() 4616

II. Refactoring – Long Method Symptoms – Large number of lines. Be immediately suspicious of any method with more than 5 to 10 lines) Causes: – A method starts down a path and, rather than break the flow or identify the helper classes, the author adds “one more thing”. – Code is often easier to write than it is to read – So there’s a temptation to write blocks that are too big. 4617

Refactoring – Long Method (cont.) What to do: – Use Extract Method to break method into smaller pieces. – Look for comments or white space delineating interesting blocks. – Extract methods semantically meaningful, not just introduce a function call every seven lines. Payoff: – Improves communications. – May expose duplication. – Often helps new classes and abstractions emerge. 4618

Refactoring – Long Method (cont.) Discussion: – Performance issues due to number of calls? Most of the time, this is a nonissue. – By getting code as clean as possible before worrying about performance, may gain big insights to restructure systems and algorithms in a way that dramatically increases performance. Contraindications : – Sometimes longer method is best way to express something. – Like almost all smells, length is a warning sign – not a guarantee- of problem. 4619

III. Refactoring – Large Class Symptoms: – Large number of instance variables – Large number of methods – Large number of lines Causes: – Large classes get bit a little bit at a time. – Author keeps adding just one more capability to a class until eventually it grows too big. – Sometimes the problem is a lack of insight into the parts that make up the whole class. – In any case, the class represents too many responsibilities folded together. 4620

Refactoring – Large Class What to do: – In general, trying to break up the class. – If class has Long Methods, address that smell first. – To break up the class, three approaches are most common: Extract Class – If you can identify a new class that has part of this class’s responsibilities Extract Subclass, if you can divide responsibilities between the class and a new subclass Extract Interface, if you can identify subsets of features that clients use. – Sometimes class is big because it’s a GUI class, and it represents not only the display component, but model as well. Payoff: – Improves communication. May expose duplication 4621

IIIa. Refactoring – Extract Class Here you have one class doing work of two. Create a new class and Move relevant fields and methods from old class into new class. 4622

Refactoring – Extract Class Motivation Have heard a class should be a crisp abstraction, handle a few clear responsibilities, or similar. In practice, classes grow. Often we add a responsibility to a class feeling it’s not worth a separate class, but as that responsibility grows and breeds, class becomes too complicated. Soon, class is as crisp as a micro-waved duck. 4623

Refactoring – Extract Class Motivation Such a class is – one with many methods and quite a lot of data. – A class that is too big to understand easily. Consider where it can be split, and you split it. 4624

Where to Split – Good Signs:  A subset of the data and a subset of the methods seem to go together.  Subsets of data that usually change together or are particularly dependent on each other. Useful test: – What would happen if you removed a piece of data or a method. – What other fields and methods would become nonsense?

Refactoring – Extract Class Mechanics Decide how to split the responsibilities of the class. Create a new class to express the split-off responsibilities. If the responsibilities of the old class no longer match its name, rename the old class. Make a link from the old to the new class. You may need a two-way link. But don’t make the back link until you find you need it. More: 4626

Refactoring – Extract Class Mechanics Use Move Field on each field you wish to move.Move Field Compile and test after each move. Use Move Method to move methods over from old to new. Start with lower-level methods (called rather than calling) and build to the higher level.Move Method Compile and test after each move. Review and reduce the interfaces of each class. Decide whether to expose the new class. If you do expose the class, decide whether to expose it as a reference object or as an immutable value object. 4627

Refactoring – Extract Class Example class Person... public String getName() { return name; } public String getTelephoneNumber() { return ("(" + officeAreaCode + ") " + officeNumber); } public String getOfficeAreaCode() { return officeAreaCode; } public void setOfficeAreaCode(String arg) {officeAreaCode = arg; } public String getOfficeNumber() { return officeNumber; } public void setOfficeNumber(String arg) { officeNumber = arg; } private String name; private String officeAreaCode; private String officeNumber; In this case I can separate the telephone number behavior into its own class. I start by defining a telephone number class: 4628

In this case I can separate the telephone number behavior into its own class. I start by defining a telephone number class: class TelephoneNumber { }// end class That was easy! No problems. I next make a link from the person to the telephone number: class Person private TelephoneNumber officeTelephone = new TelephoneNumber(); That too was easy. Note all the while your program still runs! 4629

Now I use Move Field on one of the fields: class TelephoneNumbe r { String getAreaCode() return areaCode; }// end TelephoneNumber() void setAreaCode (String arg) { areaCode = arg; }// end setAreaCode() private String areaCode; }// end class TelephoneNumber class Person... public String getTelephoneNumber() { return ("(" + getOfficeAreaCode() + ") " + officeNumber); }// end getTelephoneNumber() String getOfficeAreaCode() { return officeTelephone.getAreaCode(); }// end getOffice AreaCode() void setOfficeAreaCode(String arg) { officeTelephone.setAreaCode(arg); }// end setOFficeAreaCode() 4630

I can then move the other field and use Move Method on the telephone number: class Person... public String getName() { return name; } public String getTelephoneNumber(){ return officeTelephone.getTelephoneNumber(); } TelephoneNumber getOfficeTelephone() { return officeTelephone; } private String name; private TelephoneNumber officeTelephone = new TelephoneNumber(); class TelephoneNumbe r... public String getTelephoneNumber() { return ("(" + _areaCode + ") " + number); } String getAreaCode() { return areaCode; } void setAreaCode(String arg) { areaCode = arg; } String getNumber() { return number; } void setNumber(String arg) { number = arg; } private String number; private String areaCode; 4631

V. Conclusion The smells in this set of slides are the easiest to identify. They’re not necessarily the easiest to fix. There are other metrics that have been applied to software. Many of them are simply refinements of code length. Pay attention when things feel like they’re getting too big. There is not a one-to-one relationship between refactorings and smells; we’ll run into the same refactorings again. – For example, Extract Method is a tool that can fix many problems. Finally, remember a smell is an indication of a potential problem, not a guarantee of an actual problem. You will occasionally find false positives – things that smell to you, but are actually better than the alternatives. But most code has plenty of real smells that can keep you busy. 4632