1 Software Maintenance and Evolution CSSE 575: Session 3, Part 1 Simplifying Conditionals Steve Chenoweth Office Phone: (812) 877-8974 Cell: (937) 657-3885.

Slides:



Advertisements
Similar presentations
Chapter 4 Computation Bjarne Stroustrup
Advertisements

Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
Module 7. Simplifying Conditional Expressions Course: Refactoring.
You want me to do what??? Refactoring legacy applications aka : fixing someone else’s “bad” code Niel Zeeman Team Foundation Consulting
Software Testing and Maintenance 1 Today’s Agenda  Course Evaluation  HW 4 Return  HW 5 Correction  Quiz 4 Next Class  Software Refactoring.
1 Software Maintenance and Evolution CSSE 575: Session 2, Part 2 Composing Methods Steve Chenoweth Office Phone: (812) Cell: (937)
Software Construction and Evolution - CSSE 375 Bad Smells in Code Shawn Bohner & Steve Chenoweth.
1 Software Maintenance and Evolution CSSE 575: Session 3, Part 4 Big Refactorings Steve Chenoweth Office Phone: (812) Cell: (937)
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
1 Software Maintenance and Evolution CSSE 575: Session 2, Part 3 Moving Features Between Objects Steve Chenoweth Office Phone: (812) Cell: (937)
1 Software Maintenance and Evolution CSSE 575: Session 1, Part 4 Even more Bad Smells in Code Steve Chenoweth Office Phone: (812) Cell: (937)
The switch statement Week 5. The switch statement Java Method Coding CONCEPTS COVERED THIS WEEK.
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.
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
Chapter 9: Coupling & Cohesion Omar Meqdadi SE 273 Lecture 9 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Advanced Programing practices
Chapter 6: Code Refactoring Omar Meqdadi SE 3860 Lecture 6 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
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.
Selection Control Structures Simple Program Design Third Edition A Step-by-Step Approach 4.
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.
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
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.
Refactoring1 Improving the structure of existing code.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
Review the following: if-else One branch if Conditional operators Logical operators Switch statement Conditional expression operator Nested ifs if –else.
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.
Com S 362: Object-Oriented Analysis and Design Refactoring.
Refactoring Conditionals Lesson Five: Conditionals.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
1 Software Maintenance and Evolution CSSE 575: Session 2, Part 1 Refactoring Principles Steve Chenoweth Office Phone: (812) Cell: (937)
Software Construction and Evolution - CSSE 375 Making Method Calls Simpler Shawn and Steve Below – “Be the character!” The late acting teacher Lee Strasberg.
1 Software Maintenance and Evolution CSSE 575: Session 3, Part 3 Dealing with Generalization Steve Chenoweth Office Phone: (812) Cell: (937)
CONDITIONALS. Boolean values Boolean value is either true or false It is name after the British mathemetician, George Boole who first formulated Boolean.
Programming Logic and Design, Introductory, Fourth Edition1 Understanding the Three Basic Structures Structure: a basic unit of programming logic Any program.
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.
Refactoring1 Improving the structure of existing code.
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.
Software Construction and Evolution - CSSE 375 Simplifying Conditionals Shawn & Steve.
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.
Chapter 9: Coupling & Cohesion Omar Meqdadi SE 273 Lecture 9 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Catalog of Refactoring (5) Simplifying Conditional Expressions.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Code Refactoring Milan Vukoje Soprex SkfOffice2 SkfOffice3 Big5 Quality oriented We are hiring…
Catalog of Refactoring (1) Composing Methods. Code Smells Long methods Dubious temporary variables Dubious methods.
Refactoring (1). Software Evolution Cope with change Feature bloat Design decay Code duplications “Pattern time is refactoring time” Make future changes.
A (Very) Simple Example Consolidate duplicate conditional fragments if (isSpecialDeal()) { total = price * 0.95; send (); } else { total = price * 0.98;
Steve Chenoweth Office Phone: (812) Cell: (937)
Java Programming Fifth Edition
Steve Chenoweth Office Phone: (812) Cell: (937)
Programming Logic and Design Eighth Edition
Software Construction and Evolution - CSSE 375 Composing Methods
Refactoring III 27-Nov-18.
//code refactoring Rename Method Introduce Assertion
Understanding the Three Basic Structures
Improving the structure of existing code
Refactoring III 25-Dec-18.
Refactoring Types Blake Duncan.
Computer Science Core Concepts
Refactoring Strategies
Advanced Programing practices
Decisions, decisions, decisions
Refactoring.
Presentation transcript:

1 Software Maintenance and Evolution CSSE 575: Session 3, Part 1 Simplifying Conditionals Steve Chenoweth Office Phone: (812) Cell: (937) hulman.edu One way to refactor conditionals, from onditionDispatcherWithCommand.html

2 Recall: Basic Rule of Refactoring Refactor the “low hanging fruit” – G ets you the most value for the least investment Consider it Algebra for Software code Pretty low to the ground is – Simplifying Conditionals!

3 Simplifying Conditionals Conditional logic can get tricky and refactorings can be used to simplify it Some Bad Code Smells – Long Method – Switch Statements – Temporary Field 1. Decompose Conditional 2. Consolidate Conditional Expression 3. Consolidate Duplicate Conditional Fragments 4. Remove Control Flag 5. Replace Nested Conditional with Guard Clauses 6. Replace Conditional with Polymorphism 7. Introduce Null Object 8. Introduce Assertion 1. Decompose Conditional 2. Consolidate Conditional Expression 3. Consolidate Duplicate Conditional Fragments 4. Remove Control Flag 5. Replace Nested Conditional with Guard Clauses 6. Replace Conditional with Polymorphism 7. Introduce Null Object 8. Introduce Assertion We’ll discuss the ones in red!

4 Decompose Conditional Situation: You have a complicated conditional (if- then-else) statement Solution: Extract methods from the condition, and then the “then”, and “else” parts if (date.before (SUMMER_START) || date.after(SUMMER_END)) charge = quantity * _winterRate + _winterServiceCharge; else charge = quantity * _summerRate; if (notSummer(date)) charge = winterCharge(quantity); else charge = summerCharge (quantity); Decompose Conditional

5 Decompose Conditional: Motivation The code (both the condition checks and the actions) tells us what happens, but can easily obscure why it happens Want our intention made clearer – Decompose it and replace chunks of code with a method calls named after the intention Receive further benefit by doing this for the conditional part and each of the alternatives – This way you highlight the condition and make it clear what you are branching on – Also highlights the reason for the branching Decompose Conditional

6 Consolidate Conditional Expression Situation: You have a sequence of conditional tests with the same result Solution: Combine them into a single conditional expression and extract it double disabilityAmount() { if (_seniority < 2) return 0; if (_monthsDisabled > 12) return 0; if (_isPartTime) return 0; // compute the disability amount double disabilityAmount() { if (isNotEligibleForDisability()) return 0; // compute the disability amount Consolidate Conditional Expression

7 Consolidate Duplicate Conditional Fragments Situation: The same fragment of code is in all branches of a conditional expression Solution: Move it outside of the expression if (isSpecialDeal()) { total = price * 0.95; send(); } else { total = price * 0.98; send(); } if (isSpecialDeal()) total = price * 0.95; else total = price * 0.98; send(); Consolidate Duplicate Conditional Fragments

8 Remove Control Flag Situation: You have a variable that is acting as a control flag for a series of boolean expressions Solution: Use a break or return instead … done = false; while not done { // do something … if (complete) done = true; // next step of loop … … while … { // do something … if (complete) return; // next step of loop … Remove Control Flag

9 Discussion: Refactor checkSecurity boolean checkSecurity(String[] people) { boolean found = false; for (int i = 0; i < people.length; i++) { if (! found) { switch (people[i].inTerroristLists()) { case Real_IRA: { sendAlert(Real_IRA, people[i].name); found = true; } case Ulster_Volunteer_Force: { sendAlert(UVF, people[i].name); found = true; } return found; } Remove Control Flag The Playmobil Security Checkpoint, from m/2010/12/02/bad-worst- toys-christma/

10 Find Control Flags & Replace with returns boolean checkSecurity(String[] people) { for (int i = 0; i < people.length; i++) { switch (people[i].inTerroristLists()) { case Real_IRA: { sendAlert(Real_IRA, people[i].name); return TRUE; } case Ulster_Volunteer_Force: { sendAlert(UVF, people[i].name); return TRUE; } return FALSE; } Remove Control Flag

11 Replace Conditional with Polymorphism class Real_IRA... void inTerroristLists(string name) { sendAlert(Real_IRA, name) return TRUE; } class UVF... void inTerroristLists(string name) { sendAlert(UVF, name) return TRUE; } … class TerroristType... abstract void inTerroristLists(string name) ; Declare superclass method abstract Replace Conditional with Polymorphism