Programming Tools David Rabinowitz.

Slides:



Advertisements
Similar presentations
You want me to do what??? Refactoring legacy applications aka : fixing someone else’s “bad” code Niel Zeeman Team Foundation Consulting
Advertisements

1 Software Maintenance and Evolution CSSE 575: Session 2, Part 3 Moving Features Between Objects Steve Chenoweth Office Phone: (812) Cell: (937)
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
Programming Tools David Rabinowitz. March 3rd, 2004 Object Oriented Design Course 2 This Lecture Personal Productivity Tools And how to use them Refactoring.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
Introduction to Refactoring Excerpted from ‘What is Refactoring?’ by William C. Wake and Refactoring: Improving the Design of Existing Code by Martin Fowler.
XP and Refactoring David Talby. Development Methodologies The Software Crisis – 84% of software projects are not on time – 31% of software projects never.
REFACTORING Improving the Design of Existing Code Atakan Şimşek e
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 v1.6 08/02/2006 Overview of Eclipse Lectures 1.Overview 2.Installing and Running 3.Building and Running Java Classes 4.Refactoring 5.Debugging 6.Testing.
Sadegh Aliakbary Sharif University of Technology Spring 2012.
The Java Programming Language
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
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.
TOOLS FOR DESIGN AND DEVELOPMENT ENVIRONMENTS. Case study - ECLIPSE Lecture notes 7.
Refactoring Improving the structure of existing code Refactoring1.
Best Practices. Contents Bad Practices Good Practices.
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.
Advanced Programming in Java
Introduction to Refactoring Jim Cooper Falafel Software.
1 CSC/ECE 517 Fall 2010 Lec. 3 Overview of Eclipse Lectures Lecture 2 “Lecture 0” Lecture 3 1.Overview 2.Installing and Running 3.Building and Running.
Software Engineering CS3003 Lecture 4 Code bad smells and refactoring.
Refactoring 101 William C. Wake
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.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Session 7 Methods Strings Constructors this Inheritance.
Module 3. Smells Between Classes Course: Refactoring.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Chapter 5 Introduction to Defining Classes
Design Patterns David Talby. This Lecture Re-routing method calls Chain of Responsibility Coding partial algorithms Template Method The Singleton Pattern.
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)
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.
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.
Tools Shane Cantrell Zach Crisman. Tools CVS FogBUGZ (Bug Tracking) Unit Testing (JUnit, Jakarta Cactus) Catalyst Web Tools (BB and Surveys) Mailman ( .
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Catalog of Refactoring (6) Making Method Calls Simpler.
Principles and examples
Memory Management.
Lecture 1b- Introduction
Working with Java.
Module Road Map Refactoring Why Refactoring? Examples
Java Primer 1: Types, Classes and Operators
Seminar in automatic tools for analyzing programs with dynamic memory
A Very Common Series of Techniques
Overview of Eclipse Lectures
Java Programming Language
Chapter 1: Computer Systems
Object Oriented Programming
Improving the structure of existing code
Generic programming in Java
Refactoring and Code Smells
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
Focus of the Course Object-Oriented Software Development
Refactoring and Code Smells
Java IDE Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
Refactoring.
Refactoring and Code Smells
Presentation transcript:

Programming Tools David Rabinowitz

Object Oriented Design Course This Lecture Personal Productivity Tools And how to use them Refactoring Static Analysis & Metrics Profiling November 7, 2018 Object Oriented Design Course

Object Oriented Design Course Refactoring Improving the design of existing code, without changing its observable behavior Here’s the Extract Method refactoring: After: void f() { … computeScore(); } computeScore(int[] a) { // code cut & pasted here Before: void f(int[] a) { … // compute score score = initial_score; for (int i=0; i<a.length; i++) score += a[i] * delta; } November 7, 2018 Object Oriented Design Course

Object Oriented Design Course Why? Why Refactor? Improve software design Make software easier to understand Help find bugs Help program faster Preconditions Working code Good set of unit tests November 7, 2018 Object Oriented Design Course

Object Oriented Design Course When? When to refactor Before adding functionality Before fixing a bug During code review When not to refactor During adding functionality During fixing a bug No good set of unit tests Small programs (usually) November 7, 2018 Object Oriented Design Course

Object Oriented Design Course David Talby November 7, 2018 Code Smells “If it stinks, change it” Duplicate code Switch statements Long method Data class Long parameter list Primitive obsession Temporary field … November 7, 2018 Object Oriented Design Course Object Oriented Design Course

Documented Refactorings There’s a catalog Fowler’s book www.refactoring.com/catalog There are many others Way to learn good OOD principles Pay attention to the mechanics November 7, 2018 Object Oriented Design Course

Automated Refactorings Eclipse’s ‘Refactor’ menu automates thing Undo & Redo Physical Structure Class Level Structure Structure Inside a Class Wizards & Preview Windows Included Other tools exist: see refactorit.com November 7, 2018 Object Oriented Design Course

Automated Refactoring Example The ‘Rename’ Refactoring renames any Java element, and references to it: November 7, 2018 Object Oriented Design Course

Automated Refactoring Example II You can preview your changes: November 7, 2018 Object Oriented Design Course

Object Oriented Design Course Encapsulate Field Before: public String name; After: private String name; public String getName() { return name; } public void setName(String n) { name = n; November 7, 2018 Object Oriented Design Course

Encapsulate Field in Eclipse November 7, 2018 Object Oriented Design Course

Object Oriented Design Course Introduce Null Object Before: if (project == null) plan = Plan.default(); else plan = project.getPlan(); After: class NullProject implements Project { public Plan getPlan() { return Plan.default(); } // other Project methods This is the Null Object Pattern November 7, 2018 Object Oriented Design Course

Object Oriented Design Course Parameterize Method Before: class Server { handleGet(…) handlePut(…) handleSet(…) } After: class Server { handle(EventType et, …) } November 7, 2018 Object Oriented Design Course

Object Oriented Design Course Extract Subclass November 7, 2018 Object Oriented Design Course

Object Oriented Design Course Extract Interface November 7, 2018 Object Oriented Design Course

Extract Interface in Eclipse November 7, 2018 Object Oriented Design Course

Object Oriented Design Course Pull Up Method November 7, 2018 Object Oriented Design Course

Replace Type Code with State/Strategy November 7, 2018 Object Oriented Design Course

Replace Inheritance with Delegation November 7, 2018 Object Oriented Design Course

Object Oriented Design Course Hide Delegate Obeys the Law of Demeter November 7, 2018 Object Oriented Design Course

Separate Query from Modifier Database Database getNextResultAndAdvanceIndex getNextResult AdvanceIndex Obeys Command-Query Separation November 7, 2018 Object Oriented Design Course

Introduce Local Extension Alternative: Introduce Foreign Method November 7, 2018 Object Oriented Design Course

The opposites are there too Inline method (extract method) Replace Parameter with Explicit Methods (Parameterize Method) Collapse Hierarchy (Extract subclass) Remove middle man (Hide delegate) Push down method (pull up method) Replace delegation with inheritance November 7, 2018 Object Oriented Design Course

More useful Refactorings in Eclipse Rename Move Change Method Signature Use Supertype where possible Extract Constant Introduce Factory … November 7, 2018 Object Oriented Design Course

Object Oriented Design Course How to Refactor Recognize the smells Refactor in small discrete steps Test after each step Refactor in pairs Use documented refactorings Don’t mix with adding functionality or fixing a bug November 7, 2018 Object Oriented Design Course

Object Oriented Design Course Static Code Analysis Programs that help gain understanding of your code Find areas in the code with Possible Bugs “Fishy” Design Inconsistent Style It’s no replacement for testing Finding (non-trivial) bugs is undecidable November 7, 2018 Object Oriented Design Course

Object Oriented Design Course Why is it so important? November 7, 2018 Object Oriented Design Course

Object Oriented Design Course Available Tools Commercial Lint for C and C++ (see gimpel.com) JTest (parasoft.com) Free Eclipse Plugins JLint (artho.com) CPD – Copy Paste Detector PMD CheckStyle JDepend – Metrics November 7, 2018 Object Oriented Design Course

Object Oriented Design Course Lint Looks for over 800 C/C++ Issues Things that compilers either miss or allow Specific C++ Errors, for example: Throwing from a destructor Not checking for NULL argument in ‘delete’ Order of initializations / constructors Non-virtual over-riden methods Macro scanning Incorrect parameter passing, Side effects, … November 7, 2018 Object Oriented Design Course

Object Oriented Design Course Lint II Value Tracking Division by zero, null dereference, out-of-bounds, memory leaks, double deallocation, … Casting & Values Loss of sign, truncations, Assignment in ‘if’, … Specific C Issues printf() arguments, order of evaluation: a[i] = i++; Style Indentation, suspicious semi-colons (a > b); , … Hundreds of other issues November 7, 2018 Object Oriented Design Course

Object Oriented Design Course JTest Checks for 380 Java & Style Issues Can automatically correct 160 of these Extensible by user-defined issues Supports metrics as well Number of bytes, classes, lines, methods, … Issue = Deviation from acceptable metric range Some issues are shared with C/C++ Values, Casting, Unreachable code, Indentation, Comments, Initialization, Exceptions, … November 7, 2018 Object Oriented Design Course

Object Oriented Design Course JTest II Other Java Specific Issues Portability Security Optimization Garbage Collection Threads and Synchronization Internationalization Servlets / EJBs Naming Conventions … November 7, 2018 Object Oriented Design Course

CPD – Copy Paste Detector Works with Java, C, C++ and PHP http://pmd. sourceforge.net/ cpd.html From the examples: A 307 lines(!) of duplicated code in Apache 2 November 7, 2018 Object Oriented Design Course

Object Oriented Design Course                       PMD For Java code Checks Unused local variables / parameters / private methods Empty catch blocks Empty 'if' statements Duplicate import statements Classes which could be Singletons Short/long variable and method names And many many more … November 7, 2018 Object Oriented Design Course

Object Oriented Design Course CheckStyle Similar to PMD Javadoc Comments, Naming Conventions, Headers, Imports, Size Violations, Whitespace, Modifiers, Blocks, Coding Problems, Class Design, Duplicate Code November 7, 2018 Object Oriented Design Course

Object Oriented Design Course JDepend Calculates metrics for java packages Calculated metrics CC - Concrete Class Count The number of concrete classes in this package. AC - Abstract Class Count The number of abstract classes or interfaces in this package. November 7, 2018 Object Oriented Design Course

Object Oriented Design Course JDepend (2) Ca - Afferent Couplings The number of packages that depend on classes in this package. "How will changes to me impact the rest of the project?" Ce - Efferent Couplings The number of other packages that classes in this package depend upon. "How sensitive am I to changes in other packages in the project?" November 7, 2018 Object Oriented Design Course

Object Oriented Design Course JDepend (3) A - Abstractness (0-1) Ratio (0.0-1.0) of Abstract Classes (and interfaces) in this package. AC/(CC+AC) I - Instability (0-1) Ratio (0.0-1.0) of Efferent Coupling to Total Coupling. Ce/(Ce+Ca). D - Distance from the Main Sequence (0-1) Cyclic - If the package contains a dependency cycle November 7, 2018 Object Oriented Design Course

Object Oriented Design Course The main sequence November 7, 2018 Object Oriented Design Course

Object Oriented Design Course Examples – Pet Store November 7, 2018 Object Oriented Design Course

Object Oriented Design Course Examples – Pet Store (2) November 7, 2018 Object Oriented Design Course

How to improve the rating? November 7, 2018 Object Oriented Design Course

Object Oriented Design Course Profiling A profiler is a program that can track the performance of another program Used to solve performance problems “How come a simple file viewer take 30 seconds to start, and over 2 minutes to find text in a medium text file?” Used to solve memory problems “Why does my text editor take 50MB on startup, and 300MB after a hour of work?” November 7, 2018 Object Oriented Design Course

Object Oriented Design Course David Talby November 7, 2018 Performance Tuning How can I make my program faster? The 80 / 20 Principle 80% of the time is spent in 20% of the code Key Issue: Find the bottlenecks Classic Mistake: Assume the bottlenecks You can’t know where they’ll be Classic Mistake II: Optimize in Advance Start with the right design, then optimize C++  Delphi strings Dates November 7, 2018 Object Oriented Design Course Object Oriented Design Course

Performance Tuning Process Step 1: Identify the bottlenecks Use a profiler! Find & measure the bottlenecks Step 2: Decide how to solve bottlenecks Make them faster (new algorithm, data str.) Call them less often (caching, lazy execution) Step 3: Measure again Only way to make sure improvement happened November 7, 2018 Object Oriented Design Course

Eclipse Profiler Plugin We’ll demonstrate on the (free!) Eclipse Profiler Plugin What is tracked CPU Memory usage Number of objects Object graph Call graph November 7, 2018 Object Oriented Design Course

Object Oriented Design Course Call Graph November 7, 2018 Object Oriented Design Course

Object Oriented Design Course Call hint November 7, 2018 Object Oriented Design Course

Object Oriented Design Course Callers November 7, 2018 Object Oriented Design Course

Object Oriented Design Course Callees November 7, 2018 Object Oriented Design Course

Object Oriented Design Course Callers and callees November 7, 2018 Object Oriented Design Course

Object Oriented Design Course CPU Profiling How many invocations were? How much time have we spent in a package / class / method? Finds the bottlenecks Just sort by time or number of invocations November 7, 2018 Object Oriented Design Course

Object Oriented Design Course Packages November 7, 2018 Object Oriented Design Course

Object Oriented Design Course Classes November 7, 2018 Object Oriented Design Course

Object Oriented Design Course Methods November 7, 2018 Object Oriented Design Course

Object Oriented Design Course Memory How much memory does the program take? Are there memory leaks? November 7, 2018 Object Oriented Design Course

Object Oriented Design Course Memory Monitor November 7, 2018 Object Oriented Design Course

Object Oriented Design Course Profiling - summery How does my application behave? What are the critical paths? Where are the bottlenecks? Do I have memory leaks? Java users – you are not exempted! November 7, 2018 Object Oriented Design Course

Object Oriented Design Course Summary Personal Productivity Tools Refactoring Static Analysis & Metrics Profilers Use them! There’s more – see Eclipse Plugins November 7, 2018 Object Oriented Design Course