Refactoring SENG 301 © Refactoring: Improving the Design of Existing Code, Martin Fowler, Kent Beck Source: https://sourcemaking.com/

Slides:



Advertisements
Similar presentations
Test-Driven Development and Refactoring CPSC 315 – Programming Studio.
Advertisements

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 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
28-Jun-15 Recognizers. 2 Parsers and recognizers Given a grammar (say, in BNF) and a string, A recognizer will tell whether the string belongs to the.
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 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Refactoring. What is Refactoring? “ Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure.
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.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Software Refactoring Part I: Introduction Bartosz Walter Advanced Object-Oriented Design Lecture 5.
Sadegh Aliakbary Sharif University of Technology Spring 2012.
Refactoring Improving the structure of existing code Refactoring1.
Testing. 2 Overview Testing and debugging are important activities in software development. Techniques and tools are introduced. Material borrowed here.
Refactoring1 Refactoring DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING CONCORDIA UNIVERSITY February 6, 2009.
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.
Incremental Design Why incremental design? Goal of incremental design Tools for incremental design  UML diagrams  Design principles  Design patterns.
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.
Module 3. Smells Between Classes Course: Refactoring.
Test-Driven Development Eduard Miric ă. The problem.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
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 Advanced Software Engineering Dr Nuha El-Khalili.
Refactoring1 Improving the structure of existing code.
Pertemuan 12 Refactoring Mata kuliah: T0144 – Advanced Topics in Software Engineering Tahun: 2010.
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.
Module 9. Dealing with Generalization Course: Refactoring.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
ICONFINDER ICONFINDER Founded Django based web application -PostgreSQL -Elasticsearch -Amazon Elastic Compute.
Principles and examples
Generalizing Similar Functions
OOP - Object Oriented Programming
Catalog of Refactoring
Module Road Map Refactoring Why Refactoring? Examples
Reviewing Code A guide to smelling another developer’s source code.
Introduction to Parsing (adapted from CS 164 at Berkeley)
Behavioral Design Patterns
A Very Common Series of Techniques
Advanced Programming in Java
4 (c) parsing.
Relational Algebra Chapter 4, Part A
Refactoring II 21-Sep-18.
Relational Algebra 461 The slides for this text are organized into chapters. This lecture covers relational algebra, from Chapter 4. The relational calculus.
Refactoring and Code Smells
Syntax Analysis Sections :.
CSCE 315 – Programming Studio, Fall 2017 Tanzir Ahmed
Code Smells 1.
Relational Algebra Chapter 4, Sections 4.1 – 4.2
Improving the structure of existing code
Refactoring and Code Smells
Advanced Programming Behnam Hatami Fall 2017.
Refactoring II 5-Feb-19.
CS 240 – Advanced Programming Concepts
Refactoring and Code Smells
Advanced Programing practices
Refactoring and Code Smells
Generalizing Similar Functions
The Invisible Process to help with analysis:
Refactoring.
Refactoring and Code Smells
Presentation transcript:

Refactoring SENG 301 © Refactoring: Improving the Design of Existing Code, Martin Fowler, Kent Beck Source: https://sourcemaking.com/

Learning Objectives By the end of this lecture, you should be able to Define code smell and refactoring Understand why refactoring is used Identify common “basic” code smells Identify or suggest situations where simple refactoring strategies can be used

Technical Debt “Building it this way is stupid, but we did it that way because it was faster” “Ugh, I’ll come back to this later and fix it up” “We should have done X” Technical debt: “the extra development work that arises when code that is easy to implement in the short run is used instead of applying the best overall solution”

Copy and paste Generally in coding, copying and pasting is your enemy » introduction of bugs » introduction of code that you may not understand » introduction of some small technical debt  duplicated code If you must copy and paste, do it manually: have it open on the right side, and then type it out manually – this forces you to go through the processof thinking about what is going on as you’re typing

Refactoring Technique: “Extract Method” If you have a code fragment that can be grouped together, and is frequently used/repeated, move this code into a separate new method, and replace the old code with a call to the method.

Refactoring Refactoring is changing a software system in such a way that it addresses non-functional requirements (e.g. comprehensibility, extensibility, usability, etc.) without alter external behaviour of the code. » Cleaning up code so that it minimizes chances of bugs » Improving design of code after it is written

Problems and Techniques “Code smells” are surface indications that there are deeper problems in the system Most refactoring techniques are fairly straightforward, and there is often really good tool support for these (now) In both the case of code smells and refactoring techniques, there are new ones being “discovered” all the time, so the list of names is fairly long: we’ll look at a small subset of each

Code smell: Duplicated code If you see the same code structure in more than one place, unifying them is a good idea (e.g. propogating bug fixes) » same expression in methods of the same class » same expression in sibling subclasses » code is similar but not the same (are there opportunities here?) » methods do the same thing with a different algorithm

Refactoring Technique: “Pull Up Method” Problem: Your subclasses have methods that perform similar work Solution: Make methods identical, then move to relevant superclass

Refactoring Technique: “Extract Method” If you have a code fragment that can be grouped together, and is frequently used/repeated, move this code into a separate new method, and replace the old code with a call to the method.

Refactoring Technique: Inline Method Situation: Method’s body is as clear as its name Solution: Put method body in the body of its caller and remove method Why does this happen??

Code smell: Feature Envy Situation: A method that seems more interested in a class other than the one it is actually in (i.e. overly coupled) Why: Adds unnecessary complexity to code Notice how getMobilePhoneNumber touches Phone a bunch of times. Can we make this better?

Redesigned Customer and Phone One way to think about this: In the first case, there are three possible points of failure if getMobilePhoneNumber isn’t right In the latter case, there is one point of failure

Refactoring Technique: Move Method Problem: Classes have too much behaviour, or are collaborating too much (i.e. too coupled) Solution: Create a new method with a similar body in the class it uses most; turn the old method into a delegation, or remove it

Code Smell: Middle Man One class delegates many of its requests to another class Beware classes that are merely wrappers over other classes or existing functionality in the framework

“Remove middle man” What’s happened here (for the client code): To get the manager, we can just ask the person object (i.e. John) rather than asking for John’s department, and then the manager for the department

Code Smell: Uncommunicative Name Problem: Name of the method or class does not succinctly describe what the class is for, or what the method does (e.g. When someone other than the developer looks at the code, they don’t know what is going on) Solution: rename or rewrite it!

Refactoring Technique: “Rename” Obvious, but bears repeating: names are one of the most important “signals” in code for comprehension “Sink” was meaningless for most of you Names are super important—methods, fields, local variables, types, etc.!! Avoid name collisions (sometimes happens), and make sure you have consistent ones: e.g. Open & Close vs. Open & Dispose We need to usually worry about subclasses and superclasses when we do this type of refactoring technique

Other Code Smells Long Method: All else equal, shorter methods are generally easier to read, to understand and to troubleshoot Long Parameter List: The more parameters a method has, the more complex it is. Limit parameters to a method, or use an object to combine parameters Dead Code: Ruthlessly delete code that isn't being used. Indecent Exposure: Beware of classes that unnecessarily expose their internals. Aggressively refactor classes to minimize their public surface. You should have a compelling reason for every item you make public. If you don't, hide it. https://blog.codinghorror.com/code-smells/

Tool Support for Refactoring Many IDEs and software tools can help automate some refactoring It’s worthwhile understanding (roughly) how this works, so when the tool messes up, you can figure out why it messed up You will not be examined on this

Consider Suppose I wanted to rename a method “Add” to “Increment”. Assuming that there was a good reason to do this, and the semantics made sense, why would it be a bad idea to simply do a Find+Replace across all the files in my code base for Add to Increment?

while b ≠ 0 if a > b a := a − b else b := b − a return a

Abstract Syntax Trees [code] => Lexer => Parser => Abstract Syntax Tree Abstract Syntax Tree: a tree representation of the abstract syntactic structure of source code written in a programming language. Each node of the tree denotes a construct occurring in the source code. The syntax is "abstract" in not representing every detail appearing in the real syntax. Lexer: takes a stream of characters and identifies “important” chunks of text (usually: eliminates whitespace, comments, etc.) and understands those as “tokens” Parser: identifies patterns in the sequences of tokens, and groups them into programmatic constructs (i.e. “This is a method declaration”, “This is a parameter name”, and so forth)  produces an abstract syntax tree https://en.wikipedia.org/wiki/Abstract_syntax_tree

Code Model Internal representation of the code that (usually) allows programmatic manipulation e.g. var method = project.GetClass(“VendingMachine”).GetMethod(“Dispose”); method.Name = “Close”; method.AddParameter(“int”, “Foobar”); Observe: this really only “works” if the code is complete and compiles.

Tool Support: Analyzing your code as you write Visual Studio and Eclipse are constantly analyzing the code you’re writing (it’s kind of weird) On the VS team, they are very aware of how fast a editor feels, and so performance is paramount Regardless, the output/benefit of this work is the pane on the right side A good one can infer what is intended, even if the code is temporarily “broken” (e.g. missing brace, etc.)

Learning Objectives By the end of this lecture, you should be able to Define code smell and refactoring Understand why refactoring is used Identify common “basic” code smells Identify or suggest situations where simple refactoring strategies can be used