Code Refactoring Milan Vukoje www.vukoje.net. Soprex SkfOffice2 SkfOffice3 Big5 Quality oriented We are hiring…

Slides:



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

A practical guide John E. Boal TestDrivenDeveloper.com.
You want me to do what??? Refactoring legacy applications aka : fixing someone else’s “bad” code Niel Zeeman Team Foundation Consulting
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.
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.
Refactoring By: Brian Smith. What is Refactoring? Definition: a change to the internal structure of software to make it easier to understand and cheaper.
XP2007b Extreme Code Quality Dongsu Park Venkatesh BS.
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
Maintenance Refactoring and Code Smells. Where are we? Over the semester we have talked about Software Engineering. The overall goal of software engineering.
Program Refactoring Mitch Soden Union College. Agenda Definition –Unit Testing –Examples Why Refactor? Limitations Just Another SW Eng Practice? Automation.
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.
Sadegh Aliakbary Sharif University of Technology Spring 2012.
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.
Software Engineering 1 Object-oriented Analysis and Design Chap 21 Test-Driven Development and Refactoring.
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.
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.
Refactoring An Automated Tool for the Tiger Language Leslie A Hensley
1 Software Maintenance and Evolution CSSE 575: Session 3, Part 1 Simplifying Conditionals Steve Chenoweth Office Phone: (812) Cell: (937)
Informatics 122 Software Design II
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.
NJIT 1 Test Driven Development and Refactoring Larman, Chapter 21.
Com S 362: Object-Oriented Analysis and Design Refactoring.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Refactoring Conditionals Lesson Five: Conditionals.
1 Software Maintenance and Evolution CSSE 575: Session 2, Part 1 Refactoring Principles Steve Chenoweth Office Phone: (812) Cell: (937)
Refactoring Mehdi Einali Advanced Programming in Java 1.
Refactoring Advanced Software Engineering Dr Nuha El-Khalili.
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.
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.
Refactoring Constants and Variables Lesson Three: Constants and Variables.
CSSE 375 Organizing Data – Part 1 Shawn and Steve Q1.
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.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
Catalog of Refactoring (5) Simplifying Conditional Expressions.
Coding Standard & Code Review Milan Vukoje
Catalog of Refactoring (1) Composing Methods. Code Smells Long methods Dubious temporary variables Dubious methods.
ICONFINDER ICONFINDER Founded Django based web application -PostgreSQL -Elasticsearch -Amazon Elastic Compute.
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
Refactoring Refactoring.
Steve Chenoweth Office Phone: (812) Cell: (937)
Refactoring and Code Smells
Refactoring with inline temp, method, and class
Software Construction and Evolution - CSSE 375 Composing Methods
Refactoring III 27-Nov-18.
Refactoring and Code Smells
Refactoring III 25-Dec-18.
Advanced Programming Behnam Hatami Fall 2017.
Refactoring Strategies
Refactoring.
Refactoring and Code Smells
Advanced Programing practices
Refactoring.
Refactoring and Code Smells
Presentation transcript:

Code Refactoring Milan Vukoje

Soprex SkfOffice2 SkfOffice3 Big5 Quality oriented We are hiring…

Agenda Why is (clean) code important? Refactoring (definition and samples) Tools When to and when not to refactor? Summary

Is code important? Is construction relatively mechanical process? Only activity that’s guaranteed to be done 50-65% of overall effort 50-75% of overall errors

Coding Horror Stress Fear Cargo cult programming “Just in case” coding Unusual software bugs (Heisenbug, Mandelbug, Schroedinbug… )

Code Example

Technical Debt – If you can get today's work done today, but you do it in such a way that you can't possibly get tomorrow's work done tomorrow, then you lose. – Kent Beck When software organization chooses a design or construction approach that's expedient in the short term but that increases complexity and is more costly in the long term. Unintentional and intentional debt

Refactoring Refactoring - a change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior. Set of rules and techniques for enhancing code while reducing chances for error Refactoring: Improving the Design of Existing Code --Martin Fowler

Composing Methods 1. Extract Method void printOwing(double amount) { printBanner(); //print details System.out.println ("name:" + _name); System.out.println ("amount" + amount); } void printOwing(double amount) { printBanner(); printDetails(amount); } void printDetails (double amount) { System.out.println ("name:" + _name); System.out.println ("amount" + amount); }

Composing Methods 2. Inline Method int getRating() { return (moreThanFiveLateDeliveries()) ? 2 : 1; } boolean moreThanFiveLateDeliveries() { return _numberOfLateDeliveries > 5; } int getRating() { return (_numberOfLateDeliveries > 5) ? 2 : 1; }

Managing temps 1. Inline Temp double basePrice = anOrder.basePrice(); return (basePrice > 1000) return (anOrder.basePrice() > 1000)

Managing temps 2. Replace Temp with Query double basePrice = _quantity * _itemPrice; if (basePrice > 1000) return basePrice * 0.95; else return basePrice * 0.98; if (basePrice() > 1000) return basePrice() * 0.95; else return basePrice() * 0.98;... double basePrice() { return _quantity * _itemPrice; }

Managing temps 3. Introduce Explaining Variable if ( (platform.toUpperCase().indexOf("MAC") > -1) && (browser.toUpperCase().indexOf("IE") > -1) && wasInitialized() && resize > 0 ) { // do something } final boolean isMacOs = platform.toUpperCase().indexOf("MAC") > -1; final boolean isIEBrowser = browser.toUpperCase().indexOf("IE") > -1; final boolean wasResized = resize > 0; if (isMacOs && isIEBrowser && wasInitialized() && wasResized) { // do something }

Simplifying conditionals 1. Decompose Conditional if (date.before (SUMMER_START) || date.after(SUMMER_END)) charge = quantity * _winterRate + _winterServiceCharge; else charge = quantity * _summerRate; if (notSummer(date)) charge = winterCharge(quantity); else charge = summerCharge (quantity);

Simplifying conditionals 2. Consolidate Duplicate Fragments if (isSpecialDeal()) { total = price * 0.95; send(); } else { total = price * 0.98; send(); } if (isSpecialDeal()) total = price * 0.95; else total = price * 0.98; send();

Simplifying conditionals 3. Consolidate Conditional Expression 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

Moving Features between objects Replace Method with Method Object Extract/Inline Class Pull Up/Down Field/Method Extract Subclass/ Superclass/Interface Collapse Hierarchy

Benefits Less code – less bugs Readable business logic – better business domain understanding Self explaining code – Less documentation and faster changes Better design – Higher encapsulation and reusability – Cleaner concepts and structure

Visual Studio 2008

Need for refactoring Why change something that works? – We want programs that are easy to read, that have all logic specified in one and only one place, that do not allow changes to endanger existing behavior, and that allow conditional logic to be expressed as simply as possible. --Kent Beck Micro design - clear API and logical structures Code evolution - Embracing change Avoiding coding horror by managing complexity Agile methods (XP, TDD, Scrum)

When Should You Refactor? Not having enough time usually is a sign that you need to do some refactoring. – Martin Fowler Refactor all the time in little steps Refactor when you: – add function – fix a bug – do a code review Refactoring and Unit Tests?

Code smells Duplicated Code Long Method Large Class Switch Statements Lazy Class Speculative Generality Temporary Field Message Chains …

When you shouldn’t refactor? Avoid refactoring: Databases Published interfaces Code is so full of bugs that you cannot stabilize it When you are very close to a deadline When you are not sure Don’t overdo it (no silver bullet) – Perfect is the enemy of good, and good is what we want

Performance and Refactoring Performance optimization = obscure code Optimized for humans = slower code but easier tuning 10% optimized code is usually enough

What Do I Tell My Manager? If the manager is quality oriented, then the thing to stress is the quality aspects. Tons of studies show that technical reviews are an important way to reduce bugs and thus speed up development. Don't tell!?

Summary 1.Programming is hard and complex 2.(Clean) Code is very important 3.Refactoring can help you achieve clean code and better design. 4.Refactoring should be used wisely 1.Don’t over do it (no silver bullet) 2.Know when not to refactor 3.Changes must not cause new bugs

Questions? Milan Vukoje

Molimo vas da popunite ankete! Please fill out the evaluations! Vaše mišljenje čini osnovu sledeće Sinergije i omogućava nam da oblikujemo sadržaj u skladu sa Vašim željama. Svi posetioci koji popune ankete ulaze u nagradnu igru Your opinion forms the next Sinergija conference, and it provides us with the means to shape its content to best suit you. All attendees that fill out the evaluations are taking part in drawing of special prizes