REFACTORING Improving the Design of Existing Code Atakan Şimşek e1298306.

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.
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 
You want me to do what??? Refactoring legacy applications aka : fixing someone else’s “bad” code Niel Zeeman Team Foundation Consulting
Gregor Gisler-Merz BrownBag Session Refactoring.
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.
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.
Maintenance Refactoring and Code Smells. Where are we? Over the semester we have talked about Software Engineering. The overall goal of software engineering.
What is Refactoring? CSE301 University of Sunderland Harry R. Erwin, PhD.
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.
Refactoring. Refactoring is a step in the software design process.
1 Software Maintenance and Evolution CSSE 575: Session 1, Part 3 Bad Smells in Code - 1 Steve Chenoweth Office Phone: (812) Cell: (937)
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 - A disciplined approach to rework for better design.
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.
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.
SWE 316: Software Design and Architecture Objectives Lecture # 20 Improving the existing design: Refactoring SWE 316: Software Design and Architecture.
When and How to Refactor? Refactoring Patterns Alexander Vakrilov Telerik Corporation Senior Developer and Team Leader.
1 Software Maintenance and Evolution CSSE 575: Session 3, Part 1 Simplifying Conditionals Steve Chenoweth Office Phone: (812) Cell: (937)
Refactoring1 Improving the structure of existing code.
Introduction to Refactoring Jim Cooper Falafel Software.
Informatics 122 Software Design II
Software Engineering CS3003 Lecture 4 Code bad smells and refactoring.
Refactoring and such ● (Side note) Specialization ● Key terms ● Abstraction, state, persistence and association and their relationship to software development.
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)
Software Construction and Evolution - CSSE 375 Making Method Calls Simpler Shawn and Steve Below – “Be the character!” The late acting teacher Lee Strasberg.
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.
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.
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.
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.
Code Refactoring Milan Vukoje Soprex SkfOffice2 SkfOffice3 Big5 Quality oriented We are hiring…
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 and Code Smells
Software Construction and Evolution - CSSE 375 Composing Methods
بازآرایی برنامه Code Refactoring
Code Smells 1.
Improving the structure of existing code
Refactoring and Code Smells
Advanced Programming Behnam Hatami Fall 2017.
Refactoring.
Refactoring and Code Smells
Advanced Programing practices
Refactoring.
Refactoring and Code Smells
Presentation transcript:

REFACTORING Improving the Design of Existing Code Atakan Şimşek e

Outline Definition of Refactoring Why&When Refactor? Refactoring Steps Refactoring Examples Bad Smells Definiton Solution Conclusion

Definition Refactoring (noun): Improving the design of existing code without changing the code's observable behavior.

Some Definitions [Fowler] a change made to the internal structure of software to make it easier to understand cheaper to modify without changing its observable behavior [Beck] Improving the design after it has been written.

Why Refactor ? Easier to understand It becomes easy for others to understand. To find the bugs Finding the Bugs in the program.

To reduce Software maintenance costs To faciliate Future changes

To program faster Code complexity tends to increase rapidly over time(Patching new functionalities) Development slows down Refactoring helps keeping complexity under control Development proceed more rapidly

When to Refactor ? When you add a function Helps you to understand the code when modifying. Sometimes the existing design does not allow you to easily add the feature. When you need to fix a bug If it is difficult to trace an error: Code was not clear enough for you to see the bug in the first place.

When you do a Code Review When you detect a “bad smell” (an indication that something is wrong) in the code

Refactor? or NOT? Code does not work : NOT Code has some bugs: REFACTOR Very Close to Deadline: NOT There are bad smells: REFACTOR

Bad Smells Bad smellProposed refactoring Duplicated CodeExtract Class Long MethodExtract Method Large ClassExtract Subclass Extract Interface Long Parameter ListReplace Parameter with Method Lazy ClassInline Class

Where Can We Use? COSE Producer Side Consumer Side

Conditions of Refactoring Refactoring implies working incrementally Making changes to the program in small steps In between run the unit tests regularly If you make a mistake it's easy to back out.

Steps to Refactoring Analysis, helps to identify the code Identify problems in code by review using bad smells of code Introduce a refactoring Test

Refactorings Add Parameter Change Association Reference to value Value to reference Collapse hierarchy Consolidate conditionals Procedures to objects Decompose conditional Encapsulate collection Encapsulate downcast Encapsulate field Extract class Extract Interface Extract method Extract subclass Extract superclass Form template method Hide delegate Hide method Inline class Inline temp Introduce assertion Introduce explain variable Introduce foreign method

Refactoring examples Change Association Encapsulate Field Replace Number with Constants Compose Conditions Extract Class

Bi-directional Association to Unidirectional We have a two-way association but one class no longer needs features from the other.

Self Encapsulate Field Create getting and setting methods for the field and use only those to access the field. private int _low, _high; boolean includes (int arg) { return arg >= _low && arg <= _high; } private int _low, _high; boolean includes (int arg) { return arg >= getLow() && arg <= getHigh(); } int getLow() {return _low;} int getHigh() {return _high;}

Replace Magic Number with Symbolic Constant Create a constant, name it after the meaning, and replace the number with it. double potentialEnergy(double mass, double height) { return mass * 9.81 * height; } double potentialEnergy(double mass, double height) { return mass * GRAVITATIONAL_CONSTANT * height; } static final double GRAVITATIONAL_CONSTANT = 9.81;

Consolidate conditionals When we have a complicated conditional (if- then-else) statement. double disabilityAmount() { if (_seniority < 2) return 0; if (_monthsDisabled > 12) return 0; if (_isPartTime) return 0 // compute the disability amount double disabilityAmount() { if (isNotEligableForDisability()) return 0; // compute the disability amount

Extract Class You have one class doing work that should be done by two. Create a new class and move the relevant fields

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 Interface Hierarchies Lazy Class Speculative Generality Temporary Field Message Chains Middle Man Inappropriate Intimacy Incomplete Library Class Data Class Refused Bequest

Few solutions to Bad Smells Duplicated Code: If you see the same code structure in more than one place

Duplicated Code(cont.) Bugs copied as well Increase maintenance cost solution: perform EXTRACT METHOD and invoke the code from both places.

Long Parameter List An object invokes a method, then passes the result as a parameter for a method. solution: Use REPLACE PARAMETER with METHOD Remove the parameter and let the receiver invoke the same method with sender.

Long Method The longer a procedure is the more difficult it is to understand. solution: perform EXTRACT METHOD

Conclusion: Refactoring is useful to any program that has at least one of the following shortcomings: Programs that are hard to read Programs that have bad smells Programs that require additional behavior

References: More Information can be found Refactoring: Improving the Design of Existing Code By Martin Fowler