Suppose you had to read the source code of java

Info icon This preview shows pages 2–5. Sign up to view the full content.

• Suppose you had to read the source code of Java libraries in order to use them! • The same applies to developers of different parts of the libraries. A client cares only about what the code does, not how it does it. Source code is ambiguous even though it may appear unambiguous and concrete. • Which details of code’s behavior are essential, and which are incidental? • Code invariably gets rewritten. • Client needs to know what they can rely on. • What properties will be maintained over time? • What properties might be changed by future optimization, improved algorithms, or just bug fixes? • Implementer needs to know what features the client depends on, and which can be changed. 2 The Role of a Specification With a specification, • the client (the user of a method or a class) agrees to rely only on information in the description/specification of the method or class for their part, and • the implementer promises to support everything in the specification, and out- side of the specification has perfect liberty to make implementation decisions. CPEN 221 – Fall 2016
Image of page 2

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

The Need for Specifications 3 Specifications facilitate change by reducing the Medusa effect : it is the specifications that should be turned to stone rather than the implementation. Sadly, much code that is written lacks clear specifications. Clients have to work out what a method/class does by using it and observing the results, and this leads to bugs because programs have unclear dependencies (that reduce simplicity and flexibility). 2.1 Behavioural equivalence Consider these two methods. Are they the same or different? static int findA ( int [] a, int val) { for ( int i = 0 ; i < a. length ; i++) { if (a[i] == val) return i; } return a. length ; } static int findB ( int [] a, int val) { for ( int i = a. length - 1 ; i >= 0 ; i--) { if (a[i] == val) return i; } return - 1 ; } Of course the code is different, so in that sense they are different. Our question is whether we could substitute one implementation for the other. Not only do these meth- ods have different code, they actually have different behaviour: • when val is missing, findA returns the length of a and findB returns -1; • when val appears twice, findA returns the lower index and findB returns the higher. But when val occurs at exactly one index of the array, the two methods behave the same. It may be that clients never rely on the behaviour in the other cases. So the notion of equivalence is in the eye of the beholder, that is, the client. In order to make it possible to substitute one implementation for another, and to know when this is acceptable, we need a specification that states exactly what the client depends on. In this case, our specification might be: CPEN 221 – Fall 2016
Image of page 3
The Need for Specifications 4 requires: val occurs in a effects: returns index i such that a[i] = val 3 The Structure of a Specification A specification of a method consists of several clauses: • a precondition , indicated by the keyword requires • a postcondition , indicated by the keyword effects The precondition is an obligation on the client (i.e., the caller of the method). It’s a condition over the state in which the method is invoked. If the precondition does not
Image of page 4

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Image of page 5
This is the end of the preview. Sign up to access the rest of the document.
  • Fall '17
  • satish
  • Method, Val, specification

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern