Small changes to code to improve it

Slides:



Advertisements
Similar presentations
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Advertisements

12-Dec-14 Refactoring IV. Previously discussed bad smells Duplicated code — and other forms of redundancy Long method — use short methods that delegate.
Refactoring Overview  What is refactoring?  What are four good reasons to refactor?  When should you refactor?  What is a bad smell (relative to refactoring.
1 Software Maintenance and Evolution CSSE 575: Session 2, Part 3 Moving Features Between Objects 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.
REFACTORING Improving the Design of Existing Code Atakan Şimşek e
1 v1.6 08/02/2006 Overview of Eclipse Lectures 1.Overview 2.Installing and Running 3.Building and Running Java Classes 4.Refactoring 5.Debugging 6.Testing.
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 to Patterns
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.
TOOLS FOR DESIGN AND DEVELOPMENT ENVIRONMENTS. Case study - ECLIPSE Lecture notes 7.
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.
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)
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.
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.
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.
Problem of the Day  Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)
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.
Industrial Project (236504) Advanced programming tools for refactoring Java code in Eclipse Student: Alexander Libov Supervisor: Dr. Ran Ettinger, IBM.
1 Software Maintenance and Evolution CSSE 575: Session 3, Part 3 Dealing with Generalization Steve Chenoweth Office Phone: (812) Cell: (937)
Refactoring Mehdi Einali Advanced Programming in Java 1.
SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring.
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.
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.
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.
Module 9. Dealing with Generalization Course: Refactoring.
Refactoring (1). Software Evolution Cope with change Feature bloat Design decay Code duplications “Pattern time is refactoring time” Make future changes.
Principles and examples
Catalog of Refactoring
Module Road Map Refactoring Why Refactoring? Examples
Literally means to “re-see”
A tree set Our SearchTree class is essentially a set.
Implementing ArrayList Part 1
Advanced Programming in Java
CS212: Object Oriented Analysis and Design
Array.
Advanced Programming Behnam Hatami Fall 2017.
Small changes to code to improve it
Object Oriented Programming COP3330 / CGS5409
Overview of Eclipse Lectures
A tree set Our SearchTree class is essentially a set.
Introduction to Software Testing (2nd edition) Chapter 4 TDD Example
Improving the structure of existing code
תירגול 9 אובייקטים.
Refactoring and Code Smells
Advanced Programming Behnam Hatami Fall 2017.
Refactoring.
Refactoring and Code Smells
Advanced Programing practices
Refactoring Low Level/High Level.
Refactoring.
10.3 Bubble Sort Chapter 10 - Sorting.
Refactoring and Code Smells
Presentation transcript:

Small changes to code to improve it 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 to modify without changing its observable behavior — Refactoring, Martin Fowler. et. al. , page 53 Martin wrote the book: Refactoring Improving the Design of Existing Code Refactoring owes a lot to a Refactoring: William Opdyke's PhD thesis

Write Drafts and Revise All good writing is based upon revision. — Jacques Barzun, Simple & Direct, 4th Edition Revision means "to look at again" Example: Before 4-July, 1776, Thomas Jefferson's draft of the Declaration of Independence had this  "We hold these truths to be sacred and undeniable, ..." He let his friend Benjamin Franklin look over this draft to make changes including this: "We hold these truths to be self-evident, ..." Total of 47 revisions, then Congress made 39 more http://www.weeklyreader.com/googledocs/pdfs/revisionfiles.pdf

Refactoring Happens In larger systems (frameworks), code will be read and modified more frequently than it will be written Refactoring typically involves Removing duplicated or dead code Simplifying complex code Clarifying unclear code It turns out that removing code can actually be a good thing: your system has fewer lines of code Easier to develop, maintain, and change Refactoring can be risky, unless you do it in small steps and have automated tests that can be run anytime

A few examples A few refactorings we've seen (they have names) Encapsulate Field (do this virtually always) add getters and/or setters to access a field Rename Method (often) Extract SuperClass (recall Shape and Fruit) You have two classes with similar features Create a superclass and move common features to the superclass Compose Method A variety of refactorings can happen at the method level

Composed Method Some refactoring attempts to "compose" methods A composed method is relatively short by using calls to a small number of coherent elements Easier to understand what the method does Some refactorings for composing methods Extract Method: replace complex code with a nice name Inline Method: use code that's as easy to read as method Code demo: Compose insertElement in ArrayPriorityList

Before public void insertElementAt(int index, E el) { // If need to grow array, grow it if (size() == data.length) { Object[] temp = new Object[data.length + 20]; for (int i = 0; i < size; i++) temp[i] = data[i]; data = temp; } // Shift array elements right to make room for (int j = size; j > index; j--) { data[j] = data[j - 1]; // Add new element at the provided index data[index] = el; size++;

After Is the new insertElementAt more readable? public void insertElementAt(int index, E el) { if (shouldGrowArray()) gowArray(); makeRoomAt(index); addAt(index, el); } Is the new insertElementAt more readable? Vote: Yes: ____ Nay: _____ Are the extra private methods in the way?

private void addAt(int index, E el) { data[index] = el; size++; } private void makeRoomAt(int index) { for (int j = size; j > index; j--) { data[j] = data[j - 1]; private void gowArray() { Object[] temp = new Object[data.length + 20]; for (int i = 0; i < size; i++) temp[i] = data[i]; data = temp;

Replace Nested Conditional with Guard Clauses see link private int boggleScoreOf(String word) { int sum = 0; if (next.length() == 3 || next.length() == 4) sum = 1; else if (next.length() == 5) sum = 2; else if (next.length() == 6) sum = 3; else if (next.length() == 7) sum = 5; else if (next.length() >= 8) sum = 11; else sum = 0; return sum; }

Refactoring Catalog http://www.refactoring.com/catalog/ Check out Martin's Refactoring Catalog from the refactoring page http://www.refactoring.com/catalog/