//code refactoring Rename Method Introduce Assertion

Slides:



Advertisements
Similar presentations
Module 7. Simplifying Conditional Expressions Course: Refactoring.
Advertisements

Clean code. Motivation Total cost = the cost of developing + maintenance cost Maintenance cost = cost of understanding + cost of changes + cost of testing.
A practical guide John E. Boal TestDrivenDeveloper.com.
© The McGraw-Hill Companies, 2006 Chapter 15. © The McGraw-Hill Companies, 2006 Exceptions an exception is an event that occurs during the life of a program.
API Design CPSC 315 – Programming Studio Fall 2008 Follows Kernighan and Pike, The Practice of Programming and Joshua Bloch’s Library-Centric Software.
Introduction to Refactoring Excerpted from ‘What is Refactoring?’ by William C. Wake and Refactoring: Improving the Design of Existing Code by Martin Fowler.
An Object-Oriented Approach to Programming Logic and Design Chapter 6 Looping.
Ch 111 Chapter 11 Advanced Batch Files. Ch 112 Overview This chapter focuses on batch file commands that allow you to:  write sophisticated batch files.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Testing CSE 140 University of Washington 1. Testing Programming to analyze data is powerful It’s useless if the results are not correct Correctness is.
 Java has the logical operators and, or, and not, symbolized by &&, ||, and !, respectively.  Logical or means one or the other or both conditions hold.
Refactoring Improving the structure of existing code Refactoring1.
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.
Refactoring 2. Admin Blackboard Quiz Acknowledgements Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of.
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
Assertions and triggers1. 2 Constraints Attribute-based CHECK constraints create table … ( postcode number(4) check (postcode > 0) ); Checked at update.
SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring.
Testing CSE 160 University of Washington 1. Testing Programming to analyze data is powerful It’s useless (or worse!) if the results are not correct Correctness.
Refactoring1 Improving the structure of existing code.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
Catalog of Refactoring (5) Simplifying Conditional Expressions.
Catalog of Refactoring (6) Making Method Calls Simpler.
Refactoring Presentation Yash Pant. Overview 3 Refactoring Types: 1. Replace Error Code with Exception 2. Split Temporary Variable 3. Remove Middle Man.
How Bad Is Oops?. When we make a decision while hypothesis testing (to reject or to do not reject the H O ) we determine which kind of error we have made.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais1.
The Object-Oriented Thought Process Chapter 03
Chapter 6 CS 3370 – C++ Functions.
Chapter 2 Section 2 Absolute Value
Data Types Variables are used in programs to store items of data e.g a name, a high score, an exam mark. The data stored in a variable is entered from.
Logger, Assert and Invariants
Testing UW CSE 160 Winter 2017.
Chapter 5 Conclusion CIS 61.
C ODEBREAKER Class discussion.
Array.
Testing UW CSE 160 Spring 2018.
Software Construction
Objects First with Java
Giving instructions on how to do something
Testing UW CSE 160 Winter 2016.
Testing Hypotheses about Proportions
Continuous Random Variable
Please use speaker notes for additional information!
Asking Questions Diego Aguirre.
Improving the structure of existing code
Part B – Structured Exception Handling
Playing Games.
Test Case Test case Describes an input Description and an expected output Description. Test case ID Section 1: Before execution Section 2: After execution.
Conditional Execution
Debugging “Why you were up till 2AM”
Please use speaker notes for additional information!
Object-Oriented Programming Using C++ Second Edition
Mutation Testing The Mutants are Coming! Copyright © 2017 – Curt Hill.
Boolean Expressions to Make Comparisons
Programming Logic and Design Fifth Edition, Comprehensive
CMSC 202 Exceptions 2nd Lecture.
Exceptions 25-Apr-19.
Exceptions 22-Apr-19.
Exceptions 10-May-19.
Using C++ Arithmetic Operators and Control Structures
Information Hiding and Encapsulation Section 4.2
Learning Objectives The student will analyze why individuals choose not to express or manage feelings in situations; e.g., using anger to manipulate.
Review of Previous Lesson
Exceptions 5-Jul-19.
CMSC 202 Exceptions 2nd Lecture.
Exception Handling.
„Lambda expressions, Optional”
Software Construction
Identities.
Presentation transcript:

//code refactoring Rename Method Introduce Assertion Consolidate Duplicate Conditional Fragments //code refactoring By Cyndy Marie Ejanda

//code refactoring Rename Method (a) (b) Problem The name of a method does not explain what the method does. Solution Rename the method. (a) (b) The name of a method does not reveal its purpose. Change the name of the method. //code refactoring Refactoring: Improving the Design of Existing Code

//code refactoring Rename Method When Poorly written method names Method functionality grew Why Code Readability How (one way to do it) Create a new method with a new name Copy the code of the old method to it Delete all the code in the old method Insert a call for the new method When: Poorly written method names Well named method but its functionality grew or changed; the method name stopped being a good descriptor Why: Code readability – method name should reflect the method’s functionality How: Couple ways of doing it Create a new method with a new name. Copy the code of the old method to it. Delete all the code in the old method and, instead of it, insert a call for the new method. Find all references to the old method and replace them with references to the new one. Delete the old method. If the old method is part of a public interface, do not perform this step. Instead, mark the old method as deprecated. //code refactoring https://sourcemaking.com/refactoring/rename-method

//code refactoring Introduce Assertion Problem For a portion of code to work correctly, certain conditions or values must be true. Solution Replace these assumptions with specific assertion checks. //code refactoring Refactoring: Improving the Design of Existing Code

//code refactoring Introduce Assertion When Certain conditions or values must be true. Why Avoid fatal consequences and data corruption Drawbacks Exception vs Assertion When: Say that a portion of code assumes something about, for example, the current condition of an object or value of a parameter or local variable. Usually this assumption will always hold true except in the event of an error. Why: If an assumption is not true and the code therefore gives the wrong result, it is better to stop execution before this causes fatal consequences and data corruption. This also means that you neglected to write a necessary test when devising ways to perform testing of the program. Drawbacks: Sometimes an exception is more appropriate than a simple assertion. You can select the necessary class of the exception and let the remaining code handle it correctly. When is an exception better than a simple assertion? If the exception can be caused by actions of the user or system and you can handle the exception. On the other hand, ordinary unnamed and unhandled exceptions are basically equivalent to simple assertions – you do not handle them and they are caused exclusively as the result of a program bug that never should have occurred. //code refactoring https://sourcemaking.com/refactoring/introduce-assertion

//code refactoring Consolidate Duplicate Conditional Fragments Problem Identical code can be found in all branches of a conditional. Solution Move the code outside of the conditional. Sometimes you find the same code executed in all legs of a conditional. In that case you should move the code to outside the conditional. This makes clearer what varies and what stays the same. //code refactoring Refactoring: Improving the Design of Existing Code

//code refactoring Consolidate Duplicate Conditional Fragments When you find the same code executed in all legs of a conditional Why Code deduplication How Identify code that is executed the same way regardless of the condition. If the common code is at the beginning, move it to before the conditional. If the common code is at the end, move it to after the conditional. When: Duplicate code is found inside all branches of a conditional, often as the result of evolution of the code within the conditional branches. Why: Code deduplication – avoid redundancy; make things more clear Drawbacks: If the duplicated code is at the beginning of the conditional branches, move the code to a place before the conditional. If the code is executed at the end of the branches, place it after the conditional. If the duplicate code is randomly situated inside the branches, first try to move the code to the beginning or end of the branch, depending on whether it changes the result of the subsequent code. If appropriate and the duplicate code is longer than one line, try using Extract Method. //code refactoring https://sourcemaking.com/refactoring/consolidate-duplicate-conditional-fragments

//code refactoring Rename Method Introduce Assertion Code Readability Introduce Assertion Avoid fatal consequences and data corruption Consolidate Duplicate Conditional Fragments Code deduplication //code refactoring