Repeated Arguments 7 January 2019 OSU CSE 1 Sources of Aliasing - - PowerPoint PPT Presentation

repeated arguments
SMART_READER_LITE
LIVE PREVIEW

Repeated Arguments 7 January 2019 OSU CSE 1 Sources of Aliasing - - PowerPoint PPT Presentation

Repeated Arguments 7 January 2019 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


slide-1
SLIDE 1

Repeated Arguments

7 January 2019 OSU CSE 1

slide-2
SLIDE 2

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...

7 January 2019 OSU CSE 2

slide-3
SLIDE 3

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

7 January 2019 OSU CSE 3

slide-4
SLIDE 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) {...}

7 January 2019 OSU CSE 4

slide-5
SLIDE 5

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) {...}

7 January 2019 OSU CSE 5

How would you implement this contract specification?

slide-6
SLIDE 6

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?

7 January 2019 OSU CSE 6

slide-7
SLIDE 7

How Calls Work In Java

public static void foo( NaturalNumber x, NaturalNumber y) { ... } NaturalNumber a = new NaturalNumber2(10); NaturalNumber b = new NaturalNumber2(319); foo(a, b);

7 January 2019 OSU CSE 7

319 10

slide-8
SLIDE 8

How Calls Work In Java

public static void foo( NaturalNumber x, NaturalNumber y) { ... } NaturalNumber a = new NaturalNumber2(10); NaturalNumber b = new NaturalNumber2(319); foo(a, b);

7 January 2019 OSU CSE 8

10 319

slide-9
SLIDE 9

How Calls Work In Java

public static void foo( NaturalNumber x, NaturalNumber y) { ... } NaturalNumber a = new NaturalNumber2(10); NaturalNumber b = new NaturalNumber2(319); foo(a, b);

7 January 2019 OSU CSE 9

10 319

slide-10
SLIDE 10

How Calls Work In Java

public static void foo( NaturalNumber x, NaturalNumber y) { ... } NaturalNumber a = new NaturalNumber2(10); NaturalNumber b = new NaturalNumber2(319); foo(a, b);

7 January 2019 OSU CSE 10

11 321

slide-11
SLIDE 11

How Calls Work In Java

public static void foo( NaturalNumber x, NaturalNumber y) { ... } NaturalNumber a = new NaturalNumber2(10); NaturalNumber b = new NaturalNumber2(319); foo(a, b);

7 January 2019 OSU CSE 11

11 321

slide-12
SLIDE 12

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

7 January 2019 OSU CSE 12

slide-13
SLIDE 13

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?

7 January 2019 OSU CSE 13

slide-14
SLIDE 14

How Calls Work In Java

public static void foo( NaturalNumber x, NaturalNumber y) { ... } NaturalNumber a = new NaturalNumber2(10); foo(a, a);

7 January 2019 OSU CSE 14

10

slide-15
SLIDE 15

How Calls Work In Java

public static void foo( NaturalNumber x, NaturalNumber y) { ... } NaturalNumber a = new NaturalNumber2(10); foo(a, a);

7 January 2019 OSU CSE 15

10

slide-16
SLIDE 16

How Calls Work In Java

public static void foo( NaturalNumber x, NaturalNumber y) { ... } NaturalNumber a = new NaturalNumber2(10); foo(a, a);

7 January 2019 OSU CSE 16

10

slide-17
SLIDE 17

How Calls Work In Java

public static void foo( NaturalNumber x, NaturalNumber y) { ... } NaturalNumber a = new NaturalNumber2(10); foo(a, a);

7 January 2019 OSU CSE 17

13

slide-18
SLIDE 18

How Calls Work In Java

public static void foo( NaturalNumber x, NaturalNumber y) { ... } NaturalNumber a = new NaturalNumber2(10); foo(a, a);

7 January 2019 OSU CSE 18

13 Can we really be sure the resulting value is 13? Perhaps surprisingly, no!

slide-19
SLIDE 19

How Calls Work In Java

public static void foo( NaturalNumber x, NaturalNumber y) { ... } NaturalNumber a = new NaturalNumber2(10); foo(a, a);

7 January 2019 OSU CSE 19

13

slide-20
SLIDE 20

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?

7 January 2019 OSU CSE 20

slide-21
SLIDE 21

What Outcome Was Expected?

7 January 2019 OSU CSE 21

Code State

a = 10 foo(a, a);

slide-22
SLIDE 22

What Outcome Was Expected?

7 January 2019 OSU CSE 22

Code State

a = 10 foo(a, a); Consult the contract for foo, substituting a for both parameters x and y; it ensures: a = 11 and a = 12

slide-23
SLIDE 23

What Outcome Was Expected?

7 January 2019 OSU CSE 23

Code State

a = 10 foo(a, a); a = 11 a = 12 Can we really have this

  • utcome?
slide-24
SLIDE 24

Repeated Arguments

  • In this case, it would be impossible for any

implementation of foo to produce the

  • utcome 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

7 January 2019 OSU CSE 24

slide-25
SLIDE 25

The Receiver Is An Argument

  • Note that the reference value of the receiver
  • f 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);

7 January 2019 OSU CSE 25

slide-26
SLIDE 26

The Receiver Is An Argument

  • Note that the reference value of the receiver
  • f 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);

7 January 2019 OSU CSE 26

Does this call double n, as you might expect from using informal reasoning and “wishful naming” to predict the outcome?

slide-27
SLIDE 27

The Receiver Is An Argument

  • Note that the reference value of the receiver
  • f 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);

7 January 2019 OSU CSE 27

Why, given the contract for add, can this call simply not be a good idea?

slide-28
SLIDE 28

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 SpotBugs do not warn you

about this!

7 January 2019 OSU CSE 28