Refactoring Presentation Yash Pant. Overview 3 Refactoring Types: 1. Replace Error Code with Exception 2. Split Temporary Variable 3. Remove Middle Man.

Slides:



Advertisements
Similar presentations
Chapter 16 Exception Handling. What is Exception Handling? A method of handling errors that informs the user of the problem and prevents the program from.
Advertisements

Review Linked list: Doubly linked list, insertback, insertbefore Remove Search.
CSE 332: C++ exceptions Overview of C++ Exceptions Normal program control flow is halted –At the point where an exception is thrown The program call stack.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Nov 10, Fall 2006IAT 8001 Debugging. Nov 10, Fall 2006IAT 8002 How do I know my program is broken?  Compiler Errors –easy to fix!  Runtime Exceptions.
Handling Errors with Exception (in Java) Project 10 CSC 420.
REFACTORING Improving the Design of Existing Code Atakan Şimşek e
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
Maintenance Refactoring and Code Smells. Where are we? Over the semester we have talked about Software Engineering. The overall goal of software engineering.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.
FIRST GADGETEER PROJECT. Where are you? Making a VS project Parts of a C# program Basics of C# syntax Debugging in VS Questions? 2.
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
Testing. 2 Overview Testing and debugging are important activities in software development. Techniques and tools are introduced. Material borrowed here.
Best Practices. Contents Bad Practices Good Practices.
1 Software Maintenance and Evolution CSSE 575: Session 3, Part 1 Simplifying Conditionals Steve Chenoweth Office Phone: (812) Cell: (937)
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
CS101 Computer Programming I Chapter 4 Extra Examples.
Refactoring 2. Admin Blackboard Quiz Acknowledgements Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of.
ECE 122 Feb. 1, Introduction to Eclipse Java Statements Declaration Assignment Method calls.
Exceptions. Why exceptions? We often strive for writing portable reusable code; we are able to detect errors, however our code may be used for many different.
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
Software Construction and Evolution - CSSE 375 Making Method Calls Simpler Shawn and Steve Below – “Be the character!” The late acting teacher Lee Strasberg.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Chapter 8 Functions in Depth. Chapter 8 A programmer-defined function is a block of statements, or a subprogram, that is written to perform a specific.
1 SWIFT 2.0: New features Dzianis Astravukh AUGUST 5, 2015.
CHAPTER 10 ERROR HANDLING & DEBUGGING JavaScript can be hard to learn. Everyone makes mistakes when writing it.
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.
Bugs CS100 how to prevent them, how to find them and how to terminate them.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Introduction to Exceptions in Java CS201, SW Development Methods.
1 Handling Errors and Exceptions Chapter 6. 2 Objectives You will be able to: 1. Use the try, catch, and finally statements to handle exceptions. 2. Raise.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
16 Exception Handling.
User-Written Functions
Logger, Assert and Invariants
Testing and Debugging.
Doubly Linked List Review - We are writing this code
CS/ENGRD 2110 Spring 2018 Lecture 5: Local vars; Inside-out rule; constructors
Arrays & Functions Lesson xx
Learning Objectives What else in C++ Bitwise operator
Test Driven Lasse Koskela Chapter 2: Beginning TDD
Exceptions with Functions
//code refactoring Rename Method Introduce Assertion
Programming in Java Assertion.
CMSC 202 Lesson 22 Templates I.
Exception Handling Oo28.
Chapter 8 Errors and Exceptions.
More Linked Lists and Loops Based on slides by Ethan Apter
Exceptions CSCE 121 J. Michael Moore
Refactoring and Code Smells
Exceptions.
slides adapted from Marty Stepp and Hélène Martin
Assignment Operators Topics Increment and Decrement Operators
Problems Debugging is fine and dandy, but remember we divided problems into compile-time problems and runtime problems? Debugging only copes with the former.
Templates I CMSC 202.
slides created by Marty Stepp
Tenth step for Learning C++ Programming
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
slides adapted from Marty Stepp
CMSC 202 Exceptions.
Ian VanBuren Refactoring.
Assignment Operators Topics Increment and Decrement Operators
Refactoring and Code Smells
slides created by Marty Stepp
Presentation transcript:

Refactoring Presentation Yash Pant

Overview 3 Refactoring Types: 1. Replace Error Code with Exception 2. Split Temporary Variable 3. Remove Middle Man

Replace Error Code with Exception Motivation: When code execution goes wrong, you have to handle errors. If you return an error code when something fails, the program will stop. Solution: Throw an exception instead of returning an error code, and let an exception handler manage the error. Benefit: Program execution won’t end if there’s an error, your handler can take care of whatever fails. You can eliminate conditional statements used to check for error conditions.

Replace Error Code with Exception - Example int authenticate(String credentials) { if (users.get(credentials) == null) return -1; else return 0; } Refactor to: void authenticate(String credentials) throws LoginException { if (users.get(credentials) == null) throw new LoginException(); }

Split Temporary Variable Motivation: Temporary variables can be reused many times to hold different values. If you use the same temp variable to hold values with different meanings, the code becomes hard to understand. Solution: Use a different variables for different values, so that each variable is responsible to hold just 1 value. Benefit: Code is more readable. Code is easier to change and maintain

Split Temporary Variable - Example double temp = revenue – costs; System.out.println(“Profit = ” + temp); temp = (costs / revenue) * 100; System.out.println(“Margin = ” + temp + “%”) Refactor to: double profit = revenue – costs; System.out.println(“Profit = ” + profit); double margin = (costs / revenue) * 100; System.out.println(“Margin = ” + margin + “%”)

Remove Middle Man Motivation: When your code becomes large, you can have too many classes that hand off tasks to one another. Solution: Delete the unnecessary methods and have them called directly instead of going through a chain of method calls Benefit: Code becomes easier to trace and debug Code is easier to maintain

Remove Middle Man - Example ShippingTracker Class Package getArrivalTime() Truck Refactor to: ShippingTracker Class Package getTruck() Truck getArrivalTime()