Software Engineering CS3003 Lecture 4 Code bad smells and 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

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 
Refactoring and Code Smells
Refactoring Overview  What is refactoring?  What are four good reasons to refactor?  When should you refactor?  What is a bad smell (relative to refactoring.
REFACTORING. What is refactoring ? In refactoring, you start with the basic code and make it better. Change the internal structure of the existing code.
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.
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
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.
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.
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.
Software Refactoring Part I: Introduction Bartosz Walter Advanced Object-Oriented Design Lecture 5.
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.
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.
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.
Refactoring (continued) Source: "Refactoring: Improving the Design of Existing Code", Martin Fowler.
Best Practices. Contents Bad Practices Good Practices.
Refactoring1 Improving the structure of existing code.
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.
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.
Chapter 21 Test-Driven Development 1CS6359 Fall 2011 John Cole.
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)
Refactoring Advanced Software Engineering Dr Nuha El-Khalili.
CSC 4700 Software Engineering
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.
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.
ICONFINDER ICONFINDER Founded Django based web application -PostgreSQL -Elasticsearch -Amazon Elastic Compute.
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)
Refactoring SENG 301 © Refactoring: Improving the Design of Existing Code, Martin Fowler, Kent Beck Source:
Refactoring and Code Smells
Software Construction and Evolution - CSSE 375 Composing Methods
بازآرایی برنامه Code Refactoring
Refactoring III 27-Nov-18.
Code Smells 1.
Improving the structure of existing code
Refactoring and Code Smells
Refactoring III 25-Dec-18.
Refactoring Types Blake Duncan.
Refactoring.
Refactoring and Code Smells
Refactoring and Code Smells
Refactoring.
Refactoring and Code Smells
Presentation transcript:

Software Engineering CS3003 Lecture 4 Code bad smells and refactoring

2 Structure of this lecture This lecture will answer the questions: What is refactoring? What are the benefits of refactoring? What are Code Bad Smells?

What is refactoring? ‘The process of changing a software system in such a way that it does not alter the external behaviour of the code, yet improves its internal structure’(Fowler et al 1999) Does not change behaviour and user should not notice any difference Constant regression testing important Is a form of preventative maintenance Important in an XP environment as code changes frequently 3

actor.html actor.html 4

Refactoring Start with an existing code base and improve the internal structure. For example:  Reduce duplicate code  Improve cohesion, reduce coupling  Improve understandability, maintainability, etc. Fowler et al 1999 suggest:  22 Code Bad Smells – bad structures in code  72 Refactorings – how to eliminate CBSs 5

How do you do refactoring? Fowler says refactoring is not something you should dedicate two weeks every six months to…rather, you should do it as you develop. Refactor when you:  recognize a warning sign (a “bad smell”)  add a function  fix a bug  do a code review (as part of constant pairing)  The third time you change a piece of code need to refactor Many tools support refactoring, e.g. Eclipse, NetBeans Problem automating many code bad smells. 6

Some example refactorings… See for full list (maintained by Fowler) Extract Method void printOwing() { printBanner(); //print details System.out.println("name:" + _name); System.out.println("amount" + getOutstanding()); } Refactored to… void printOwing() { printBanner(); printDetails(getOutstanding()); } void printDetails(double outstanding) { System.out.println("name:" + _name); System.out.println("amount" + outstanding); } 7

What are Code Bad Smells? de-smells.html de-smells.html 8

Code Bad Smells 1. Duplicated Code: Same code structure happen in more than one place. 2.Long Method: A method is too long. 3.Large Class: A class is trying to do too much, it often shows up as too many instance variables. 4.Long Parameter List: A method needs passing too many parameters. 5.Divergent Change: Divergent change occurs when one class is commonly changed in different ways for different reasons. 6.Shotgun Surgery: Shotgun surgery is similar to divergent change but is the opposite. Every time you make a kind of change, you have to make a lot of little changes to a lot of different classes. 7.Feature Envy: The whole point of objects is that they are a technique to package data with the processes used on that data. A Feature Envy is a method that seems more interested in a class other than the one it actually is in. 8.Data Clumps: Some data items together in lots of places: fields in a couple of classes, parameters in many method signatures. 9.Primitive Obsession: Primitive types are over used in software. Small classes should be used to in place of primitive types in some situation. 10.Switch Statements: Switch statements often lead to duplication. Most times you see a switch statement you should consider polymorphism. 11.Parallel Inheritance Hierarchies: Parallel inheritance hierarchies is really a special case of shotgun surgery. In this case, every time you make a subclass of one class, you also have to make a subclass of another. You can recognize this smell because the prefixes of the class names in one hierarchy are the same as the prefixes in another hierarchy. 12.Lazy Class: Each class you create costs money to maintain and understand. A class that isn't doing enough to pay for itself should be eliminated. 13.Speculative Generality: If a machinery was being used, it would be worth it. But if it isn't, it isn't. The machinery just gets in the way, so get rid of it. 14.Temporary Field: Sometimes you see an object in which an instance variable is set only in certain circumstances. Such code is difficult to understand, because you expect an object to need all of its variables. 15.Message Chains: You see message chains when a client asks one object for an other object, which the client then asks for yet another object, which the client then asks for yet another object, and so on. Navigating this way means the client is coupled to the structure of the navigation. Any change to the intermediate relationships causes the client to have to change. 16.Middle Man: You look at a class's interface and find half the methods are delegating to this other class. It may mean problems. 17.Inappropriate Intimacy: Sometimes classes become far too intimate and spend too much time delving in each others’ private parts. 18.Alternative Classes with Different Interfaces: Classes are doing the similar things but with different signatures. 19.Incomplete Library Class: Library classes should be used carefully, especially we don't know whether a library is completed. 20.Data Class: These are classes that have fields, getting and setting methods for the fields, and nothing else. Such classes are dumb data holders and are almost certainly being manipulated in far too much detail by other classes. 21.Refused Bequest: Subclasses get to inherit the methods and data of their parents, but they just use a few of them. 22.Comments: Don't write comments when it is unnecessary. When you feel the need to write a comment, first try to refactor the code so that any comment becomes superfluous. 9

Duplicated Code Bad Smell “Number 1 in the stink parade!” (Fowler) Often the result of cut and pastes –very dangerous. If the same expression is in two methods in the same class make it a private routine and parameterize it (Extract Method refactoring) If the same code is in two related classes put common code into closest mutual ancestor and parameterize 10

Long method Bad Smell Short methods very important for:  Understanding  Making correct changes A sign that trying to do too many things Break up into smaller private methods within the class (Extract method) Often a comment indicates the next major step so this is a good point to “break it up”.  When you see a comment, make a method. 11

Comments Bad Smell “Comments can be used as deodorant” to hide CBS Fowler claims that comments are a sign of:  opaque, complicated, inscrutable code.  code that is in need of explanation but that actually needs restructuring rather than commenting. Fowler advocates:  self-evident coding practices by making methods short and identifiers long  Commenting the decision making process for the solution implemented. 12

13 Reading for the week Sommerville ed9 Chapter 9 Refactoring: Improving the Design of Existing Code, Martin Fowler(et al.), 1999, Addison- Wesley Comments on module so far…