Repeated Arguments. 26 April 2013 OSU CSE 1

Similar documents
Java Interfaces. 6 April 2016 OSU CSE 1

Array. 9 January 2015 OSU CSE 1

Set4. 8 February 2019 OSU CSE 1

Kernel Implementations III. 6 May 2013 OSU CSE 1

CSE 341, Autumn 2015, Ruby Introduction Summary

Java Collections Framework. 24 April 2013 OSU CSE 1

University of Texas at Arlington, TX, USA

Aggregation. Introduction to Computer Science I. Overview (1): Overview (2): CSE 1020 Summer Bill Kapralos. Bill Kapralos.

COMP 110/L Lecture 13. Kyle Dewey

Objects and Iterators

Class object initialization block destructor Class object

CS112 Lecture: Working with Numbers

Kernel Implementations I. 27 June 2013 OSU CSE 1

Compiler Design Spring 2017

Two-Level Designs. Chapter 881. Introduction. Experimental Design. Experimental Design Definitions. Alias. Blocking

Functional Programming in Java. CSE 219 Department of Computer Science, Stony Brook University

And Even More and More C++ Fundamentals of Computer Science

CS558 Programming Languages

Final Exam. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

1B1b Classes in Java Part I

DISTRIBUTIVE LATTICES

CSE 374 Programming Concepts & Tools

For this chapter, switch languages in DrRacket to Advanced Student Language.

Final Exam. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Semantic Analysis and Type Checking

Constructors for classes

Before We Begin. Class Implementation II. Introduction to Computer Science II. Overview (1): Administrative Details (1):

5) Attacker causes damage Different to gaining control. For example, the attacker might quit after gaining control.

Programming Languages

Next: What s in a name? Programming Languages. Data model in functional PL. What s in a name? CSE 130 : Fall Lecture 13: What s in a Name?

DECLARAING AND INITIALIZING POINTERS

CSE 11 Midterm Fall 2012

Chapter 5 Names, Bindings, Type Checking, and Scopes

Procedure and Object- Oriented Abstraction

CSE341: Programming Languages Lecture 11 Type Inference. Dan Grossman Spring 2016

CS1150 Principles of Computer Science Methods

CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output

CSE 143. Lecture 9: introduction to recursion reading: 12.1

Verification and Validation. Ian Sommerville 2004 Software Engineering, 7th edition. Chapter 22 Slide 1

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Fall 2011

write the functions inside as well as outside the class. So in C++, they are called member functions and not methods.

CS 105 Perl: Perl subroutines and Disciplined Perl

The compilation process is driven by the syntactic structure of the program as discovered by the parser

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 Lecture 4 Shell Variables, More Shell Scripts (Thanks to Hal Perkins)

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

Patterns in Java Unfinished Symmetry

Introducing Wybe a language for everyone

CSE 374: Programming Concepts and Tools. Eric Mullen Spring 2017 Lecture 4: More Shell Scripts

CIS 341 Final Examination 4 May 2017

Bouml Tutorial. The tutorial must be read in order because I will not repeat each time the general commands to call a menu etc...

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

C++ Lecture 5 Arrays. CSci 588: Data Structures, Algorithms and Software Design.

CMSC 330: Organization of Programming Languages. Lets, Tuples, Records

From Java to C++ From Java to C++ CSE250 Lecture Notes Weeks 1 2, part of 3. Kenneth W. Regan University at Buffalo (SUNY) September 10, 2009

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

6. Java Errors and Exceptions. Errors, runtime-exceptions, checked-exceptions, exception handling, special case: resources

CS558 Programming Languages

Motivation was to facilitate development of systems software, especially OS development.

Verfying the SSH TLP with ProVerif

6. Java Errors and Exceptions

Chapter 6 Introduction to Defining Classes

objects

Lecture 5: Methods CS2301

CS558 Programming Languages

Wednesday, October 15, 14. Functions

Compiler Errors. Flash CS4 Professional ActionScript 3.0 Language Reference. 1 of 18 9/6/2010 9:40 PM

EECS168 Exam 3 Review

Programming Languages & Paradigms PROP HT Course Council. Subprograms. Meeting on friday! Subprograms, abstractions, encapsulation, ADT

Objectives. Order (sort) the elements of an array Search an array for a particular item Define, use multidimensional array

Separate Compilation Model

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

Java Input/Output. 11 April 2013 OSU CSE 1

Iron Workers District Council of Western New York and Vicinity Benefit Funds. iremit Online Remittance Instructions

Maltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers

Motivation was to facilitate development of systems software, especially OS development.

Instantiation of Template class

Introduction to Object-Oriented Programming

CS1150 Principles of Computer Science Methods

Brocade Network OS DATA CENTER. Target Path Selection Guide August 7, 2017

[ANALYSIS ASSIGNMENT 10]

CS 314 Principles of Programming Languages

Pass by Value. Pass by Value. Our programs are littered with function calls like f (x, 5).

Iteration Preview of Coming Attractions The Need to Get Your Act Together A Process for Developing While Statements

System Software Assignment 1 Runtime Support for Procedures

COMP 2355 Introduction to Systems Programming

CSE 11 Midterm Fall 2008

Primitive vs Reference

Errors and Exceptions

Fortgeschrittene objektorientierte Programmierung (Advanced Object-Oriented Programming)

Arrays. 1 Index mapping

Programming Languages and Techniques (CIS120)

6.001 Notes: Section 4.1

Introduction to Programming Using Java (98-388)

9. Java Errors and Exceptions

Protection Levels and Constructors The 'const' Keyword

Verification and Validation. Ian Sommerville 2004 Software Engineering, 7th edition. Chapter 22 Slide 1

Final CSE 131B Spring 2004

Overriding המחלקה למדעי המחשב עזאם מרעי אוניברסיטת בן-גוריון

Transcription:

Repeated Arguments 26 April 2013 OSU CSE 1

Sources of Aliasing Aliased references for mutable types can cause trouble, so it is important to know how aliases might arise One way (which is easy to recognize and easy to record in a tracing table, using ) is the simple assignment of one reference variable to another There are other sources of aliasing as well... 26 April 2013 OSU CSE 2

Aliasing from Parameter Passing Because a formal parameter of a reference type is initialized by copying the corresponding argument s reference value (which is tantamount to assignment of the argument to the formal parameter), parameter passing is another source of aliasing 26 April 2013 OSU CSE 3

Example Consider this method: /** * Adds 1 to the first number and 2 to the * second. *... * @updates x, y * @ensures * x = #x + 1 and y = #y + 2 */ private static void foo(naturalnumber x, NaturalNumber y) {...} 26 April 2013 OSU CSE 4

Example Consider this method: /** * Adds 1 to the first number and 2 to the * second. *... * @updates x, y * @ensures * x = #x + 1 and y = #y + 2 */ private static void foo(naturalnumber x, NaturalNumber y) {...} How would you implement this contract specification? 26 April 2013 OSU CSE 5

Example: A Call Consider this call of the method: NaturalNumber a = new NaturalNumber2(10); NaturalNumber b = new NaturalNumber2(319); foo(a, b); How does this get executed, and what values result for a and b? 26 April 2013 OSU CSE 6

How Calls Work In Java public static void foo( NaturalNumber x, NaturalNumber y) {... } 10 319 NaturalNumber a = new NaturalNumber2(10); NaturalNumber b = new NaturalNumber2(319); foo(a, b); 26 April 2013 OSU CSE 7

How Calls Work In Java public static void foo( NaturalNumber x, NaturalNumber y) {... } 10 319 NaturalNumber a = new NaturalNumber2(10); NaturalNumber b = new NaturalNumber2(319); foo(a, b); 26 April 2013 OSU CSE 8

How Calls Work In Java public static void foo( NaturalNumber x, NaturalNumber y) {... } 10 319 NaturalNumber a = new NaturalNumber2(10); NaturalNumber b = new NaturalNumber2(319); foo(a, b); 26 April 2013 OSU CSE 9

How Calls Work In Java public static void foo( NaturalNumber x, NaturalNumber y) {... } 11 321 NaturalNumber a = new NaturalNumber2(10); NaturalNumber b = new NaturalNumber2(319); foo(a, b); 26 April 2013 OSU CSE 10

How Calls Work In Java public static void foo( NaturalNumber x, NaturalNumber y) {... } 11 321 NaturalNumber a = new NaturalNumber2(10); NaturalNumber b = new NaturalNumber2(319); foo(a, b); 26 April 2013 OSU CSE 11

Note: Harmless Aliasing Aliases are created, but since the method body for foo only has access to the variables x and y (i.e., the variables used as arguments in the client code, a and b, are not in scope while the body of foo is executing), these aliases cause no trouble for reasoning 26 April 2013 OSU CSE 12

Example: A Different Call Now consider this call of the method: NaturalNumber a = new NaturalNumber2(10); foo(a, a); How does this happen, and what value results for a? 26 April 2013 OSU CSE 13

How Calls Work In Java public static void foo( NaturalNumber x, NaturalNumber y) {... } 10 NaturalNumber a = new NaturalNumber2(10); foo(a, a); 26 April 2013 OSU CSE 14

How Calls Work In Java public static void foo( NaturalNumber x, NaturalNumber y) {... } 10 NaturalNumber a = new NaturalNumber2(10); foo(a, a); 26 April 2013 OSU CSE 15

How Calls Work In Java public static void foo( NaturalNumber x, NaturalNumber y) {... } 10 NaturalNumber a = new NaturalNumber2(10); foo(a, a); 26 April 2013 OSU CSE 16

How Calls Work In Java public static void foo( NaturalNumber x, NaturalNumber y) {... } 13 NaturalNumber a = new NaturalNumber2(10); foo(a, a); 26 April 2013 OSU CSE 17

How Calls Work In Java public static void foo( NaturalNumber x, NaturalNumber y) {... } 13 Can we really be sure the NaturalNumber a = new NaturalNumber2(10); resulting value is 13? foo(a, a); Perhaps surprisingly, no! 26 April 2013 OSU CSE 18

How Calls Work In Java public static void foo( NaturalNumber x, NaturalNumber y) {... } 13 NaturalNumber a = new NaturalNumber2(10); foo(a, a); 26 April 2013 OSU CSE 19

Note: Harmful Aliasing Here, aliases are created between two variables that are in scope while the method body for foo is executing (i.e., the variables x and y), and these aliases do cause trouble for reasoning Who is at fault for this anomalous outcome? The implementer of foo? The client of foo? 26 April 2013 OSU CSE 20

What Outcome Was Expected? Code State a = 10 foo(a, a); 26 April 2013 OSU CSE 21

What Outcome Was Expected? Consult the contract for foo, substituting Code a for both parameters x and y; it ensures: a = 11 and a = 12 a = 10 State foo(a, a); 26 April 2013 OSU CSE 22

What Outcome Was Expected? Code Can we really have this outcome? a = 10 State foo(a, a); a = 11 a = 12 26 April 2013 OSU CSE 23

Repeated Arguments In this case, it would be impossible for any implementation of foo to produce the outcome supposedly ensured according to its contract! The trouble arising from repeated arguments (i.e., a call like foo(a, a)) is not just in Java; it is a problem in any language with mutable types 26 April 2013 OSU CSE 24

The Receiver Is An Argument Note that the reference value of the receiver of a call (to an instance method) is copied to the formal parameter known as this Hence, there is a repeated argument if the receiver is also passed as another argument to such a call Example: n.add(n); 26 April 2013 OSU CSE 25

The Receiver Is An Argument Does this call double n, as Note that the reference you might value expect of the from receiver of a call (to an instance using informal method) reasoning is copied to the formal parameter and known wishful naming as this to predict the outcome? Hence, there is a repeated argument if the receiver is also passed as another argument to such a call Example: n.add(n); 26 April 2013 OSU CSE 26

The Receiver Is An Argument Note that the reference Why, given value the contract of the for receiver of a call (to an instance add, can method) this call simply is copied to the formal parameter not known be a good as idea? this Hence, there is a repeated argument if the receiver is also passed as another argument to such a call Example: n.add(n); 26 April 2013 OSU CSE 27

Best Practice for Repeated Arguments Never pass any variable of a mutable reference type as an argument twice or more to a single method call Remember that the receiver is an argument Checkstyle and FindBugs do not warn you about this! 26 April 2013 OSU CSE 28