A practical guide John E. Boal TestDrivenDeveloper.com.

Slides:



Advertisements
Similar presentations
Clean code. Motivation Total cost = the cost of developing + maintenance cost Maintenance cost = cost of understanding + cost of changes + cost of testing.
Advertisements

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.
Search and Recursion pt. 2 CS221 – 2/25/09. How to Implement Binary Search Take a sorted data-set to search and a key to search for Start at the mid-point.
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 to Computers and Programming Quick Review What is a Function? A module of code that performs a specific job.
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.
Algorithm Programming Coding Advices Bar-Ilan University תשס " ו by Moshe Fresko.
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 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Refactoring – III Measured Smells. Smells Covered 1. Comments 2. Long method 3. Large Class 462.
Refactoring Cristescu Marilena. Definitions Loose Usage: Reorganize a program(or something) As a noun: a change made to the internal structure of some.
Sadegh Aliakbary Sharif University of Technology Spring 2012.
LECTURE 38: REFACTORING CSC 395 – Software Engineering.
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.
Improving the Quality of Existing Code Svetlin Nakov Telerik Corporation
Refactoring1 Improving the structure of existing code.
Refactoring Deciding what to make a superclass or interface is difficult. Some of these refactorings are helpful. Some research items include Inheritance.
A Practical Guide To Unit Testing John E. Boal TestDrivenDeveloper.com.
Advanced Programming in Java
CS242.  Reduce Complexity  Introduce an intermediate, understandable abstraction  Avoid code duplication  Support subclassing  Hide sequences  Hide.
Informatics 122 Software Design II
1 CSC/ECE 517 Fall 2010 Lec. 3 Overview of Eclipse Lectures Lecture 2 “Lecture 0” Lecture 3 1.Overview 2.Installing and Running 3.Building and Running.
Incremental Design Why incremental design? Goal of incremental design Tools for incremental design  UML diagrams  Design principles  Design patterns.
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.
Writing Maintainable code Dr. Susan McKeever DT228/3 GUI Programming.
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.
Modern Software Development Using C#.NET Chapter 6: Refactoring.
Software Construction and Evolution - CSSE 375 Making Method Calls Simpler Shawn and Steve Below – “Be the character!” The late acting teacher Lee Strasberg.
Refactoring Mehdi Einali Advanced Programming in Java 1.
SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring.
Principles of Object Oriented Design
Refactoring1 Improving the structure of existing code.
Pertemuan 12 Refactoring Mata kuliah: T0144 – Advanced Topics in Software Engineering Tahun: 2010.
The Last Lecture CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
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.
Continuous Improvement. Start Simple and Continually Improve E.g., Gmail Labels 1.
Module 9. Dealing with Generalization Course: Refactoring.
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.
Catalog of Refactoring (6) Making Method Calls Simpler.
TDD Unit tests from a slightly different point of view Katie Dwyer.
Refactoring (1). Software Evolution Cope with change Feature bloat Design decay Code duplications “Pattern time is refactoring time” Make future changes.
Principles and examples
Module Road Map Refactoring Why Refactoring? Examples
Steve Chenoweth Office Phone: (812) Cell: (937)
A Very Common Series of Techniques
CS 5010 Program Design Paradigms "Bootcamp" Lesson 12.1
Advanced Programming in Java
Refactoring and Code Smells
Overview of Eclipse Lectures
Code Smells 1.
//code refactoring Rename Method Introduce Assertion
Improving the structure of existing code
Refactoring and Code Smells
Advanced Programming Behnam Hatami Fall 2017.
Refactoring Types Blake Duncan.
Refactoring and Code Smells
CS 5010 Program Design Paradigms "Bootcamp" Lesson 12.1
S. Single Responsibility Principle O. L. I. D.
Refactoring and Code Smells
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

A practical guide John E. Boal TestDrivenDeveloper.com

What is Refactoring?  Refactor (verb)  1. Improve the code’s internal structure without changing it’s behavior  The process:  Make one small change to the code that improves it. Recompile and re-test.  Iterate on the above over and over until there are no more improvements to make.

Why should we refactor?  When we need to make a change, clean it up  Clean, clear code is easier to:  Understand  Modify  Test  Maintain  Less likely to have bugs  Lowers risk

Why shouldn’t we refactor?  If it works DON’T FIX IT  If poorly factored code works, has been tested, and does what the customer needs… we shouldn’t change it.  Existing code that has already been tested and is working … if we don’t need to change something we should not touch it.  Refactoring code can introduce risk (in untested [legacy] code  Don’t refactor it if you don’t own it…

How do we know what to refactor?  Look for Smells.  Something is not quite right…  Long methods  Too many parameters  Too much public information  Duplicated code  Code or Design smells are what we are looking for. These areas we need to refactor.

Gimme an example…  public void foo(int x)  {  return (x+1)*(x-1);  }  Could be:  public void foo(int x)  {  return x^2-1;  }

More simple examples  for(int i=0; i < length; i++)  { … }  for(int arrayIndexer=0; arrayIndexer < length; arrayIndexer++)  {…}  And  public string get_setting(…)  {…}  public string LoadSettingFromConfigurationFile(…)  {…}

Techniques - Extract Method  If there is functionality that is needed in more than one place, pull it out into its own method.  If we require writing the same code more than once, we can extract that code we wrote the first time and call the new method from both places.

Techniques – Rename  Renaming a variable, method, or even a class can make code clearer.  Long, descriptive names are good  If we can’t name something explicitly because we can’t aptly describe its function, then it probably is doing too much. Make sure to stick to the single-focus principle.

Decompose Conditional  If we have a complicated condition in an if() statement, we can extract the calculation into a method.  This is another case of Extract Method.

Create Parameter Object  In a method if we have a group of parameters that naturally group together, we can create an object for these and pass that object instead.  Look for the “Too Many Parameters” code smell… this probably should occur if we have more than about 3 parameters on a method

Replace Exception With Test  Don’t throw/catch exceptions when a simple if() statement could do the work.  Exceptions are expensive to set up and slow down performance.  Use a return code or value instead.  Exceptions should only be used in exceptional conditions…

Change Constructor to Factory  If a constructor takes parameters, or does a lot of work in creating the object, using a Factory pattern would be better.  A Factory class encapsulates the logic needed to build the object, so the object class can remain focused on its sole purpose.

Replace Algorithm  Replace the algorithm in the body of a method with a simpler, faster, or clearer algorithm  This is a common refactoring.  Make sure that there are unit tests wrapped around the method before undertaking this refactor… we don’t want to change the behavior…