Computer Science 313 – Advanced Programming Topics.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Computer Science 313 – Advanced Programming Topics.
Software Engineering and Design Principles Chapter 1.
Road Map Introduction to object oriented programming. Classes
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Chapter 16 Programming and Languages: Telling the Computer What to Do.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Web Based Programming Section 6 James King 12 August 2003.
GENERAL CONCEPTS OF OOPS INTRODUCTION With rapidly changing world and highly competitive and versatile nature of industry, the operations are becoming.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Types for Programs and Proofs Lecture 1. What are types? int, float, char, …, arrays types of procedures, functions, references, records, objects,...
CSC 212 – Data Structures Lecture 12: Java Review.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Abstraction IS 101Y/CMSC 101 Computational Thinking and Design Tuesday, September 17, 2013 Marie desJardins University of Maryland, Baltimore County.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Week 1 - Friday.  What did we talk about last time?  Our first Java program.
Refactoring Improving the structure of existing code Refactoring1.
CSC 107 – Programming For Science. History of C  Dennis Ritchie developed C from 1969 – 1973  Based upon B (& other) earlier languages  Since its creation,
Problem of the Day  Why are manhole covers round?
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
PHY 107 – Programming For Science. History of C  Dennis Ritchie developed C from 1969 – 1973  Based upon B (& other) earlier languages  Since its creation,
SE: CHAPTER 7 Writing The Program
Computer Science 313 – Advanced Programming Topics.
Problem of the Day  Why are manhole covers round?
Computer Science 313 – Advanced Programming Topics.
Refactoring1 Improving the structure of existing code.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Computer Science 313 – Advanced Programming Topics.
CSC 313 – Advanced Programming Topics. Decorator Pattern Intent.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
CSC 107 – Programming For Science. Announcements  Locations of Macs to use outside of lab time  Can find on Library ground floor (6) & main floor (6)
CSC 107 – Programming For Science. History of C  Dennis Ritchie developed C from 1969 – 1973  While at Bell Labs, created language to develop Unix 
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
CSC 313 – Advanced Programming Topics. Observer Pattern in Java  Java ♥ Observer Pattern & uses everywhere  Find pattern in JButton & ActionListener.
Tom Cargill The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the.
CSC 313 – Advanced Programming Topics. What Is the Factory Method?  Creation details hidden by AbstractCreator  Does effective job of limiting concrete.
Computer Science 313 – Advanced Programming Topics.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Object-Oriented Programming Chapter Chapter
Inheritance 2 Mehdi Einali Advanced Programming in Java 1.
Threads and Singleton. Threads  The JVM allows multiple “threads of execution”  Essentially separate programs running concurrently in one memory space.
SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring.
Refactoring1 Improving the structure of existing code.
Computer Science Reaching Wider Summer School 2012.
Week 12 - Monday.  What did we talk about last time?  Defining classes  Class practice  Lab 11.
Computer Science 313 – Advanced Programming Topics.
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
Java Inheritance in Java. Inheritance Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea.
Eagleson’s Law: Any code of your own that you haven't looked at for 6+ months might as well have been written by someone else.
Bjarne Stroustrup I have always wished that my computer would be as easy to use as my telephone. My wish has come true. I no longer know how to use my.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Problem of the Day  Why are manhole covers round?
Week 4 Object-Oriented Programming (1): Inheritance
Understand the Programming Process
Inheritance Basics Programming with Inheritance
Objects First with Java
MSIS 655 Advanced Business Applications Programming
Improving the structure of existing code
Classes and Objects.
Computer Programming with JAVA
Understand the Programming Process
Applying OO Concepts Using Java
Inheritance.
A First Program.
Presentation transcript:

Computer Science 313 – Advanced Programming Topics

Handling Common Subroutines  Must perform set of actions in many places

Handling Common Subroutines  Must perform set of actions in many places Paste code into each different method Write 1 method called everywhere

Why Write a Separate Method  Fewer errors from not updating 1 copy  Code is much simpler to debug  Optimizing & refactoring code far simpler  Makes code more reusable

Why Copy-and-Paste Code  Calls expensive relative to most code  Calls will take instructions each  Violates assumption of next instruction executed  Compiler may be prevented from optimizing  Unadulterated stupidity making these comments  In many situations, costs do not matter  Only on critical path should we even think about this  Remember Amdahl’s law: 10% slowdown to 20% of code = 0.97 slowdown

Method Inlining  From Michael Hind, a leading IBM researcher  Best solution looks like copy-and-paste code  Easiest way to tell if someone a poseur

What is Inlining?  Replaces method call with actual code  Acts as if written using copy-and-paste  But does not suck when maintaining code public int add(int a, int b) { if (b == 3) { return add3(a); } return a + b; } public int add3(int x) { return x + 3; }

What is Inlining?  Replaces method call with actual code  Acts as if written using copy-and-paste  But does not suck when maintaining code public int add(int a, int b) { if (b == 3) { return add3(a); } return a + b; } public int add3(int x) { return x + 3; }

What is Inlining?  Replaces method call with actual code  Acts as if written using copy-and-paste  But does not suck when maintaining code public int add(int a, int b) { if (b == 3) { return a + 3; } return a + b; } public int add3(int x) { return x + 3; }

C/C++ Inlining Options  Suggest to compiler with inline keyword  Allows programmer to hint at function to inline  Compiler may override if not a good idea inline int add3(int x) { return x + 3; }  Rewrite function as macro using #define  Global search-and-replace performed on code  Compiler has no choice; function forced inline  Create absolute nightmare  Create absolute nightmare for maintenance

Inlining Support in Java  Automatically inlines certain small methods  But only if and when exact definition known  Subclasses cannot change definition later staticfinalprivate  Method must be static, final, or private  Code must be written to use this skill  Rare for people to know details of this  Simple poseur detector from not knowing this

Inlining Support in Java  Automatically inlines certain small methods  But only if and when exact definition known  Subclasses cannot change definition later staticfinalprivate  Method must be static, final, or private  Code must be written to use this skill  Rare for people to know details of this  Simple poseur detector from not knowing this How not to look like an idiot:

Do the Right (Coding) Thing  Breakup code & write only small methods  Small ones private final to get inlined  Methods far easier to write, debug, & reuse  All benefits and no slowdown!

CSC212/213 Professor Fun!  Make methods static when possible  Code becomes easier to read and follow  Ensures everything can be inlined  Make fields private & use get methods  Make accessor final since it cannot be changed  Guess what, the getter code gets inlined

Bad Methods; No Inline public int three() { return 3; } public int two() { return 2; } public int five() { return three() + two(); } public int addToX(int y) { return x + y; } public int getAndUpdateX(int z) { x = x + z; return x; }

Now We Can Inline public STATIC int three() { return 3; } public STATIC int two() { return 2; } public STATIC int five() {return three()+two();} PRIVATE int addToX(int y) { return x + y; } public FINAL int getAndUpdateX(int z) { x = ADD T O X( Z ); return x; }

Simple Java Inlining Results

JIT Advantage  So far, inlining based on static analysis  Information available when program compiled  Only inlines small methods, since they are safe  But calling larger methods also takes time  Java has JIT compiler it can use  Information gathered as program runs  Frequent calls (hot paths) will be found

Next Step To Worry About  Can inline methods on critical path  Even when larger than otherwise allowed  Even if not final, if calls are not polymorphic  JVM does this automatically  So may not hurt to use patterns on critical path!

Inlining Even When We Cannot

For Next Class  Lab #5 on Angel & due week from Fri.  Read pages in book  Command pattern will be focus of reading  Great for when we need to use remote control  How do we use Command pattern?  How does it fit in with other patterns?  Where should this be used?  Is the pattern behavioral, creational, or structural?