Refactoring Mehdi Einali Advanced Programming in Java 1.

Slides:



Advertisements
Similar presentations
A practical guide John E. Boal TestDrivenDeveloper.com.
Advertisements

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 By: Brian Smith. What is Refactoring? Definition: a change to the internal structure of software to make it easier to understand and cheaper.
Introduction to Refactoring Excerpted from ‘What is Refactoring?’ by William C. Wake and Refactoring: Improving the Design of Existing Code by Martin Fowler.
1 Introduction. 2 Why? We need to deliver good programs Bugs: results may be catastrophic Long time to get something working Expensive to change: software.
XP and Refactoring David Talby. Development Methodologies The Software Crisis – 84% of software projects are not on time – 31% of software projects never.
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.
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.
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.
© ALEXANDRE CUVA  VERSION 2.00 Test Driven Design.
Refactoring - A disciplined approach to rework for better design.
Software Engineering 1 Object-oriented Analysis and Design Chap 21 Test-Driven Development and Refactoring.
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.
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
Refactoring1 Improving the structure of existing code.
Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer Coding.
A Practical Guide To Unit Testing John E. Boal TestDrivenDeveloper.com.
Advanced Programming in Java
Informatics 122 Software Design II
Chapter 21 Test-Driven Development 1CS6359 Fall 2011 John Cole.
REFACTORINGREFACTORING. Realities Code evolves substantially during development Requirements changes 1%-4% per month on a project Current methodologies.
Module 1. Introducing Course: Refactoring. Overview Definition Principles Building Tests Bad Smells in Code.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
1 Software Maintenance and Evolution CSSE 575: Session 2, Part 1 Refactoring Principles Steve Chenoweth Office Phone: (812) Cell: (937)
Refactoring - 1 CS494: Intro. to Refactoring Readings: –Refactoring for everyone: How and why to use Eclipse's automated refactoring features. By David.
Modern Software Development Using C#.NET Chapter 6: Refactoring.
Scalatest. 2 Test-Driven Development (TDD) TDD is a technique in which you write the tests before you write the code you want to test This seems backward,
1 ECE 750 Topic 8 Meta-programming languages, systems, and applications Evolving Object-Oriented Designs with Refactorings – Lance Tokuda and Don Batory.
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.
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 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.
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.
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.
Refactoring: Improving the Design of Existing Code.
Refactoring (1). Software Evolution Cope with change Feature bloat Design decay Code duplications “Pattern time is refactoring time” Make future changes.
Principles and examples
Steve Chenoweth Office Phone: (812) Cell: (937)
A Very Common Series of Techniques
Advanced Programming in Java
Refactoring and Code Smells
Advanced Programming Behnam Hatami Fall 2017.
Small changes to code to improve it
بازآرایی برنامه Code Refactoring
Improving the structure of existing code
Refactoring and Code Smells
Small changes to code to improve it
Advanced Programming Behnam Hatami Fall 2017.
Refactoring.
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
Refactoring and Code Smells
Advanced Programing practices
Refactoring Low Level/High Level.
Refactoring.
Refactoring and Code Smells
Presentation transcript:

Refactoring Mehdi Einali Advanced Programming in Java 1

2 Tale of Messy code

3 Once upon a time … A team start a project Project got many attention and team has to add new features in short time “I will fix this later”

4 After a while Changes slowed down by messy code As productivity decreases more programmer assigned to project New programmer with messy code results in more messy code

5 rebellion Eventually the team rebels. A new tiger team is selected Best technologies has been chosen Now the two teams are in a race This race can go on for a very long time Tiger team is now under pleasure of comparison with old low feature but working version once upon a time Messy code again and again once upon a time

6 refactoring

7 Refactoring A disciplined way to restructure code in order to improve code quality without changing its behavior A change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior.

8 Refactoring Refactoring is the process of changing a software system In such a way that it does not alter the external behavior of the code But improves its internal structure It is a disciplined way to clean up code It minimizes the chances of introducing bugs When you refactor, you are improving the design of the code after it has been written.

9 Refactoring By continuously improving the design of code, we make it easier and easier to work with Joshua Kerievsky, Refactoring to Patterns

10 Example Duplicate Code What are the drawbacks? What is the solution? Refactoring: Finding a “Bad Smell” Changing the code to remove the bad smell Some well-known bad smells are reported

11 Bad Smell A bad smell in code Any symptom in the source code that possibly indicates a deeper problem. The term is coined by Kent Beck.

12 Bad Smells Duplicated Code Long Method Large Class Long Parameter List Divergent Change …

13 Refactoring Techniques Extract Method Move Method Variable Class Extract Class Rename Method Variable Class Pull Up Push Down

14 IDE Support Refactoring techniques are widely supported by IDEs

15 The Two Hats Kent Beck's metaphor of two hats Divide your time between two distinct activities adding function refactoring

16 Why Should I Refactor? Improves the Design of Software Makes Software Easier to Understand Helps You Find Bugs Helps You Program Faster Refactoring makes your code more maintainable

17 When Should You Refactor? The Rule of Three: Refactor When You Add Function Refactor When You Need to Fix a Bug Refactor As You Do a Code Review

18 Scanner s = new Scanner(System.in); System.out.println("Rectangle Info."); System.out.print("Enter the width: "); int a1 = s.nextInt(); System.out.print("Enter the length: "); int a2 = s.nextInt(); System.out.println("Rectangle Info."); System.out.print("Enter the width: "); int b1 = s.nextInt(); System.out.print("Enter the length: "); int b2 = s.nextInt(); int x = a1*a2; int y = b1*b2; if(x == y) System.out.println("Equal"); Find bad smells! Refactor the Code!

19 Scanner scanner = new Scanner(System.in); System.out.println("Rectangle Info."); System.out.print("Enter the width: "); int width1 = scanner.nextInt(); System.out.print("Enter the length: "); int length1 = scanner.nextInt(); System.out.println("Rectangle Info."); System.out.print("Enter the width: "); int width2 = scanner.nextInt(); System.out.print("Enter the length: "); int length2 = scanner.nextInt(); int area1 = width1*length1; int area2 = width2*length2; if(area1 == area2) System.out.println("Equal"); Rename…

20 class Rectangle{ private int length, width; public int getLength() { return length; } public void setLength(int length) { this.length = length; } public int getWidth() { return width; } public void setWidth(int width) { this.width = width; } public Rectangle(int length, int width) { this.length = length; this.width = width; } Extract Class…

21 Scanner scanner = new Scanner(System.in); System.out.println("Rectangle Info."); System.out.print("Enter the width: "); int width = scanner.nextInt(); System.out.print("Enter the length: "); int length = scanner.nextInt(); Rectangle rectangle1 = new Rectangle(length, width); System.out.println("Rectangle Info."); System.out.print("Enter the width: "); width = scanner.nextInt(); System.out.print("Enter the length: "); length = scanner.nextInt(); Rectangle rectangle2 = new Rectangle(length, width); int area1 = rectangle1.getWidth()*rectangle1.getLength(); int area2 = rectangle2.getWidth()*rectangle2.getLength(); if(area1 == area2) System.out.println("Equal");

22 class Rectangle{... public int area(){ return length * width; } … int area1 = rectangle1.area(); int area2 = rectangle2.area(); Extract Method…

23 private static Rectangle readRectangle(Scanner scanner) { int width; int length; System.out.println("Rectangle Info."); System.out.print("Enter the width: "); width = scanner.nextInt(); System.out.print("Enter the length: "); length = scanner.nextInt(); Rectangle rectangle2 = new Rectangle(length, width); return rectangle2; } Extract Method…

24 Refactored Code Scanner scanner = new Scanner(System.in); Rectangle rectangle1 = readRectangle(scanner); Rectangle rectangle2 = readRectangle(scanner); int area1 = rectangle1.area(); int area2 = rectangle2.area(); if(area1 == area2) System.out.println("Equal");

25 Clean code

26 Clean code does one thing well Make if hard for bugs to hide

27 Reads like well-written prose Never obscure the designer’s intent

28 Provides one way rather than many ways for doing one thing

29 Each routine you read turn out to be pretty much what you expect

30 (Conclusion) Clean code is Make if hard for bugs to hide Clean code does one thing well Reads like well-written prose Never obscure the designer’s intent Provides one way rather than many ways for doing one thing Each routine you read turn out to be pretty much what you expect

31 notes Clean Programming is some thing like martial art Combination of technique and art. The Art of Computer Programming by Knuth Have different school of thoughts Clean Programming is skill Good learning results in good use forever Changing bad learning is hard Like Driving!

32 Reference Refactoring: improving the design of existing code, Martin Fowler, Kent Beck,John Brant, William Opdyke, Don Roberts (1999) Clean code,A handbook of agile software craftmanship,Robert C Martin,2008, Prentice Hall

33