“Blink-factoring”: Software engineering meets pop psychology Presented by Jamie Hohman EECS 816: Object-Oriented Software Development April 22, 2008.

Slides:



Advertisements
Similar presentations
12-Dec-14 Refactoring IV. Previously discussed bad smells Duplicated code — and other forms of redundancy Long method — use short methods that delegate.
Advertisements

About Me – Frank Xu Education ▫ North Dakota State University  Ph.D. in Software Engineering ▫ Towson University  MS in Computer Science ▫ Southeast.
© 2010 Shawn A. Bohner Software Construction and Evolution - CSSE 375 Even more Bad Smells in Code Shawn & Steve Q1 Shawn & Steve Hint 
You want me to do what??? Refactoring legacy applications aka : fixing someone else’s “bad” code Niel Zeeman Team Foundation Consulting
Refactoring and Code Smells
Gregor Gisler-Merz BrownBag Session Refactoring.
Refactoring Overview  What is refactoring?  What are four good reasons to refactor?  When should you refactor?  What is a bad smell (relative to refactoring.
Lectures 17 and 18 A Refactoring Micro-Example FOR0383 Software Quality Assurance 5/16/20151Dr Andy Brooks Refactoring is really easy using this tool.
Software Construction and Evolution - CSSE 375 Bad Smells in Code Shawn Bohner & Steve Chenoweth.
1 Software Maintenance and Evolution CSSE 575: Session 1, Part 4 Even more Bad Smells in Code 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.
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
Leiden University. The university to discover. Leiden University. The university to discover. Leiden University. The university to discover. Software Engineering.
Maintenance Refactoring and Code Smells. Where are we? Over the semester we have talked about Software Engineering. The overall goal of software engineering.
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.
1 Software Maintenance and Evolution CSSE 575: Session 1, Part 3 Bad Smells in Code - 1 Steve Chenoweth Office Phone: (812) Cell: (937)
LECTURE 38: REFACTORING CSC 395 – Software Engineering.
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.
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.
Testing. 2 Overview Testing and debugging are important activities in software development. Techniques and tools are introduced. Material borrowed here.
Refactoring (continued) Source: "Refactoring: Improving the Design of Existing Code", Martin Fowler.
Best Practices. Contents Bad Practices Good Practices.
Refactoring1 Improving the structure of existing code.
Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer Coding.
Refactoring Deciding what to make a superclass or interface is difficult. Some of these refactorings are helpful. Some research items include Inheritance.
Introduction to Refactoring Jim Cooper Falafel Software.
Informatics 122 Software Design II
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 and such ● (Side note) Specialization ● Key terms ● Abstraction, state, persistence and association and their relationship to software development.
Refactoring: Code Smells. Admin Notes REGISTER FOR BLACKBOARD Watch blackboard site for updates on class as hurricane season approaches.
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.
Refactoring II Dealing with Polymorphism. Switch in Rental Switches on Movie! class Rental … public double getCharge() { double result = 0; switch (getMovie().getPriceCode()){
Module 3. Smells Between Classes Course: Refactoring.
1 Software Maintenance and Evolution CSSE 575: Session 2, Part 1 Refactoring Principles Steve Chenoweth Office Phone: (812) Cell: (937)
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.
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.
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
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.
Refactoring: Improving the Design of Existing Code.
A (Very) Simple Example Consolidate duplicate conditional fragments if (isSpecialDeal()) { total = price * 0.95; send (); } else { total = price * 0.98;
Principles and examples
Steve Chenoweth Office Phone: (812) Cell: (937)
Steve Chenoweth Office Phone: (812) Cell: (937)
Refactoring and Code Smells
Software Construction and Evolution - CSSE 375 Composing Methods
بازآرایی برنامه Code Refactoring
Refactoring III 27-Nov-18.
Code Smells 1.
Improving the structure of existing code
Refactoring and Code Smells
Refactoring III 25-Dec-18.
Refactoring.
Refactoring and Code Smells
Refactoring.
Refactoring and Code Smells
Presentation transcript:

“Blink-factoring”: Software engineering meets pop psychology Presented by Jamie Hohman EECS 816: Object-Oriented Software Development April 22, 2008

2 Contents Defining Refactoring Defining Blink Identifying types of “smelly” code Learning refactoring solutions for this code Deciding when and when not to refactor Understanding the role of testing Learning about tools available for refactoring Breaking down examples throughout

3 What is Refactoring? Is either a noun (the actual code changes) or a verb (the act of applying the changes) Represents a structured and tested method of reversing software decay Makes no observable changes in functionality Helps future developers find bugs and add new functionality faster Ensures code says things once and only once

4 Why are developers wary? Don’t know how or when Automated tools can detect “fishy” areas No short term benefits Refactoring results in bugs found in common code, fixed once, and results in smaller code Constitutes overhead Pays for itself in more maintainable code Risk of newly introduced errors Good practices help eliminate these Automated tools help reduce the manual, error-prone tasks Needs to reach The Tipping Point in an organization to be fully acceptedThe Tipping Point

5 What smells in here? void funcA() { int x, y = 2; x = y * y; printf(“%d”, x); } void funcB() { int x, y = 4; x = y * y; funcC(x); } Extract Method void funcA() { … x = sqr(y); … } void funcB() { … x = sqr(y); … } int sqr(int x) { return x * x; } Duplicate Code

6 Contents Defining Refactoring Defining Blink Identifying types of “smelly” code Learning refactoring solutions for this code Deciding when and when not to refactor Understanding the role of testing Learning about tools available for refactoring Breaking down examples throughout

7 What is Blink? Pop psychology book by Malcolm Gladwell, a staff writer for The New Yorker Subtitle is “The Power of Thinking Without Thinking” Subconscious is better at realizing something quickly than conscious is at realizing it slowly This explains how we identify “smelly code” Amateurs have a hard time explaining it; experts do not

8 What smells in here? void funcA() { int temp1, temp2, temp3; // Do stuff with temp1 … // Do stuff with temp2 // and temp 3 … //Do lots more stuff } Extract Method void DoStuff1() { int temp; // Do stuff here } void DoStuff2and3() { int temp, temp2; // Do more stuff } int DoLotsMore(int x) { // Encapsuplate more // changes here } Long Method

9 Contents Defining Refactoring Defining Blink Identifying types of “smelly” code Learning refactoring solutions for this code Deciding when and when not to refactor Understanding the role of testing Learning about tools available for refactoring Breaking down examples throughout

10 When to refactor? At any time, but only in short bursts as needed 3 strikes, you refactor 1. Write it. Good job! 2. Second time, read and wince at duplication or poor structure 3. Third time, get fed up and refactor When modifying any existing code by adding functionality, fixing bugs, or during code reviews

11 When not to refactor? Database support often means being tied to a schema Either abstract this away or don’t refactor Public interfaces should not change, published interfaces cannot change Make the old function call the new one Trying to fix inherently flawed design When code is beyond help When approaching a deadline

12 Design and Performance Refactoring complements upfront design Software is malleable so do it simply first and then refactor in flexibility as needed Find a reasonable solution, not the solution Most of the time, the more complicated, flexible solution is overkill Performance suffers from refactoring, right? Not necessarily; according to Demeyer, modern compiler optimizations make virtual functions equal to branching Doesn’t matter most of the time Can concentrate on optimization in small, well-defined areas Binary refactoring (Tilevich, Smaragadakis) – WHAT?!?! – is exclusively for performance

13 Binary refactoring: a study in contradictions “Premature optimization is the root of all evil.” – C.A.R. Hoare Money, Microsoft and MU are finally off the hook Not to increase performance, but to not force source code changes to increase performance Example 1: Objects that make sense to be stored in a database as a whole but only parts need to be transmitted Example 2: Replace virtual function call with static or inline function call

14 What smells in here? Extract Class or Extract Subclass See Example Large Class

15 Contents Defining Refactoring Defining Blink Identifying types of “smelly” code Learning refactoring solutions for this code Deciding when and when not to refactor Understanding the role of testing Learning about tools available for refactoring Breaking down examples throughout

16 How to test? Critical since functionality must remain the same Development time in descending order: 1. Debugging 2. Analysis & design 3. Writing code JUnit helps organize testing code in classes and run suites of tests Only for unit testing Don’t try to get all the bugs; build tests that are easy to run and are designed to get most of the bugs

17 What smells in here? void funcA( int param1, int param2, char* param3, float param4, float param5, void* param6) { int temp1, temp2, temp3; // Do stuff with all // this data } Introduce Parameter Object class newObj { public: int getParam1(); int getParam2(); char* getParam3(); float getParam4(); float getParam5(); void* getParam6(); } void funcA(newObj obj) { int temp1, temp2, temp3; // Do stuff with all // this data } Long Parameter List

18 Contents Defining Refactoring Defining Blink Identifying types of “smelly” code Learning refactoring solutions for this code Deciding when and when not to refactor Understanding the role of testing Learning about tools available for refactoring Breaking down examples throughout

19 How are tools useful? Refactoring Browser was originally designed for Smalltalk Requires a program database and parse tree Must also be accurate, speedy, undo- able, and integrated with other tools “Can we fix it? Yes, we can!”

20 Demonstration Refactoring in Visual Studio 2005 with Visual Assist X (

21 What smells in here? void funcA(acctObj acct) { int payment; payment = acct.getBalance(); payment /= acct.getTerm(); payment *= acct.getRate(); // More stuff with // payment } Extract Method -> Move Method void funcA(acctObj acct) { int payment; payment = acct.getPayment(); // More stuff with // payment } Class acctObj { … int getPayment(); //new! … } Feature Envy

22 What smells in here? void funcA(acctObj acct) { switch (getAnimalType()) { case DOG: bark(); break; case COW: moo(); break; // etc. } Extract Method -> Move Method -> Replace Type Code with Subclass -> Replace Conditional with Polymorphism void funcA(acctObj acct) { animal.speak(); } Switch Statements

23 More “smelly” code Divergent Change – any change to handle a variation should change a single class and everything within that class Extract Class Shotgun Surgery – opposite of above; to accommodate a change, must make lots of small changes to different classes Move Method, Move Field, Inline Class

24 Some more “smelly” code Data Clumps – data (like children) enjoy hanging around in groups; move these to a new class Extract Class, Introduce Parameter Object Primitive Obsession – group related primitives in a function into a new class Replace Data Value with Object, Replace Type Code with Class Parallel Inheritance Hierarchies – special case of shotgun surgery where subclass of one class requires subclass of another class every time

25 Even more “smelly” code Lazy Class – classes (like teenagers) enjoy doing nothing if given the chance; must pull its own weight Collapse Hierarchy, Inline Class Speculative Generality – somebody over- engineered or over-designed -> too complicated Collapse Hierarchy, Inline Class, Remove Parameter Temporary Field – a member variable is used in some cases but not others Extract Class, Introduce Null Object

26 The last of the “smelly” code Message Chains – object asks another object, which asks another object, … Hide Delegate, Extract Method, Move Method Middle Man – half of an object’s methods call other objects Remove Middle Man, Inline Method, Replace Delegation with Inheritance Inappropriate Intimacy – spend too much time delving into private parts Move Method, Move Field, Change Bidirectional Association to Unidirectional, Extract Class, Replace Inheritance with Delegation Alternative Classes with Different Interfaces, Incomplete Library Class, Data Class, Refused Bequest

27 Watch out for comments (huh?!?) Comments are often used as deodorant on this smelly code Refactoring eliminates the smelly code so that the comments are superfluous Replace each commented block of code with a new method using Extract Method and Introduce Assertion

28 Questions

29 Bibliography Demeyer, S., “Refactor conditionals into polymorphism: what’s the performance cost of introducing virtual calls?”, IEEE International Conference on Software Maintenance, Pgs , Fowler, M., Refactoring: Improving the Design of Existing Code, Addison-Wesley, 1999, First Edition. Gladwell, M., Blink: The Power of Thinking Without Thinking, Back Bay Books, 2005, First Edition. Tilevich, E., Smaragdakis, Y., “Binary refactoring: improving code behind the scenes”, International Conference on Software Engineering, Pgs , 2005, ACM.