Docsity

Programming Language Technologies: Interactive Development, Testing, and Debugging - Prof., Study notes of Programming Languages

Toggle sidebar
bg1
CMSC 433 – Programming Language
Technologies and Paradigms
Spring 2004
Interactive Development Environments,
Testing, and Debugging
February 12, 2004
Administrivia
•Reading: Liskov ch 5, 8
•Project 2 posted
–JUnit and testing
Interactive Development Environments
•A system that covers many development tasks
–Editor – usually with nice syntax coloring, indentation
–Compiler – automatic compilation, errors linked to code
–Debugger – step through source code
–Etc... – Testing, search, code transformations, ...
•Examples: DrJava, NetBeans, Eclipse, Visual Studio,
emacs
Dr. Java
•Light-weight IDE
• Editing
–Syntax coloring, auto-indent, brace matching
• Testing
–Integrates with Junit testing framework
•Uses suite() or auto-generated suite
–Interaction panel allows interactive method invocations
•Debugging
–Integrates with Java debugger
–Interactions panel also useful
Debugging
•My program doesn’t work: why?
•Use the scientific method:
–Study the data
•Some tests work, some don’t
–Hypothesize what could be wrong
–Run experiments to check your hypotheses
•Testing!
–Iterate
Starting to Debug
•What are the symptoms of the misbehavior?
–Input/output
–Stack trace (from thrown exception)
•Where did the program fail?
•What could have led to this failure?
•Test possible causes, narrow down the problem
pf3
pf4
pf5

Partial preview of the text

Download Programming Language Technologies: Interactive Development, Testing, and Debugging - Prof. and more Study notes Programming Languages in PDF only on Docsity!

CMSC 433 – Programming Language

Technologies and Paradigms

Spring 2004

Interactive Development Environments,

Testing, and Debugging

February 12, 2004

Administrivia

• Reading: Liskov ch 5, 8

• Project 2 posted

  • JUnit and testing

Interactive Development Environments

• A system that covers many development tasks

  • Editor – usually with nice syntax coloring, indentation
  • Compiler – automatic compilation, errors linked to code
  • Debugger – step through source code
  • Etc... – Testing, search, code transformations, ...

• Examples: DrJava, NetBeans, Eclipse, Visual Studio,

emacs

Dr. Java

• Light-weight IDE

• Editing

  • Syntax coloring, auto-indent, brace matching

• Testing

  • Integrates with Junit testing framework
    • Uses suite() or auto-generated suite
  • Interaction panel allows interactive method invocations

• Debugging

  • Integrates with Java debugger
  • Interactions panel also useful

Debugging

• My program doesn’t work: why?

• Use the scientific method:

  • Study the data
    • Some tests work, some don’t
  • Hypothesize what could be wrong
  • Run experiments to check your hypotheses
    • Testing!
  • Iterate

Starting to Debug

• What are the symptoms of the misbehavior?

  • Input/output
  • Stack trace (from thrown exception)

• Where did the program fail?

• What could have led to this failure?

• Test possible causes, narrow down the problem

Checking that Properties Hold

• Print statements

  • Check whether values are correct
    • E.g., look at value of i to check if i > 0
  • Check whether control-flow is correct
    • E.g., see if f() is called after g()

• Automatic debugger

  • Allows you to step through the program interactively
  • Verify expected properties
    • Don’t need to put in print statements and recompile
  • Use as part of testing

Dr. Java Interactions Pane

• Can evaluate Java expressions interactively

  • Can bind variables, execute expressions/statements

• Benefits

  • Make sure that methods work as expected
  • Test invariants by constructing expressions not in

program text

  • Combines with interactive debugger

Dr. Java’s Automatic Debugger

• Set execution breakpoints

• Step through execution

  • into, over, and out of method calls

• Examine the stack

• Examine variable contents

• Set watchpoints

  • Notified when variable contents change

Using the Debugger

• Set debug mode to on

  • Turns on debug panel with state information

• Set break point(s) in Java source

• Run the program

Tips

• Make bug reproducible

  • If it’s not reproducible, what does that imply?

• Boil down to smallest program that reproduces

bug

  • Reveals the core problem

• Explain problem to someone else (i.e., instructor

or TA)

  • Explaining may reveal the flaw in your logic

• Keep notes: don’t make the same mistake twice

Defensive Programming

  • Assume that other methods/classes are broken
    • They will mis-use your interface public Vector(int initialCapacity, int capacityIncrement) { super(); if (initialCapacity < 0) throw new IllegalArgumentException( "Illegal Capacity: "+ initialCapacity); ... }
  • Goal: Identify errors as soon as possible

Implementing the Rep Invariant

• Interesting idea: Write a function to check the rep

public boolean repOK() { ...check for duplicates in elts... }

• Where can you use this?

  • Can add wherever you expect rep to hold
  • Can call during unit testing

• Cost?

Exposing the Rep

• Be careful of exposing the representation

class IntSet { int[] elts; int[] getElements () { return elts; } }

• Why?

  • Other people may rely on implementation details
  • Clients may violate the rep invariant

Polymorphism

• Recall that B is a subtype of A if, everywhere you

expect an A, you can accept a B

  • Subtypes come from subclassing with extends
  • Subtypes come from interfaces with implements

• This is a kind of type polymorphism

  • Methods can accept objects of many types, not just one
  • This is usually called subtype polymorphism

Polymorphism Using Object

class IntegerStack { class Entry { Integer elt; Entry next; Entry(Integer i, Entry n) { elt = i; next = n; } } Entry theStack; void push(Integer i) { theStack = new Entry(i, theStack); } Integer pop() throws EmptyStackException { if (theStack == null) throw new EmptyStackException(); else { Integer i = theStack.elt; theStack = theStack.next; return i; }}}

IntegerStack Client

IntegerStack is = new IntegerStack(); Integer i; is.push(new Integer(3)); is.push(new Integer(4)); i = is.pop();

• This is OK, but what if we want other kinds of

stacks?

  • Need to make one XStack for each kind of X
  • Problems: Code bloat, maintainability nightmare

Polymorphism Using Object

class Stack { class Entry { Object elt; Entry next; Entry(Object i, Entry n) { elt = i; next = n; } } Entry theStack; void push(Object i) { theStack = new Entry(i, theStack); } Object pop() throws EmptyStackException { if (theStack == null) throw new EmptyStackException(); else { Object i = theStack.elt; theStack = theStack.next; return i; }}}

Stack Client

Stack is = new Stack(); Integer i; is.push(new Integer(3)); is.push(new Integer(4)); i = (Integer) is.pop();

• Now Stacks are reusable

  • push() works the same
  • But now pop() returns an Object
    • Have to downcast back to Integer
    • Not checked until run-time

General Problem

• When we move from an X container to an Object

container

  • Methods that take X’s as input parameters are OK
    • If you’re allowed to pass Object in, you can pass any X in
  • Methods that return X’s as results require downcasts
    • You only get Objects out, which you need to cast down to X

• This is a general feature of subtype polymorphism

Parametric Polymorphism (for Classes)

• Idea: We can parameterize the Stack class by its

element type

• Syntax:

  • Class declaration: class A { ... }
    • A is the class name, as before
    • T is a type variable, can be used in body of class (...)
  • Client usage declaration: A x;
    • We instantiate A with the Integer type

Parametric Polymorphism for Stack

class Stack { class Entry { Element elt; Entry next; Entry(Element i, Entry n) { elt = i; next = n; } } Entry theStack; void push(Element i) { theStack = new Entry(i, theStack); } Element pop() throws EmptyStackException { if (theStack == null) throw new EmptyStackException(); else { Element i = theStack.elt; theStack = theStack.next; return i; }}}

Stack Client

Stack is = new Stack(); Integer i; is.push(new Integer(3)); is.push(new Integer(4)); i = is.pop();

  • No downcasts
  • Type-checked at compile time
  • No need to duplicate Stack code for every usage

Parametric Polymorphism for Procedures

• Suppose B is a subtype of A

1. static A id(A x) { return x; }

2. static A id(B x) { return x; }

3. static B id(A x) { return x; }

4. static B id(B x) { return x; }

• Can’t pass an A to 2 or 4

• 3 doesn’t type check

• Can pass a B to 1 but you get an A out

Using with Legacy Code

• Translation via type erasure

  • class A ==> class A

• Thus class A is available as a “raw type”

  • class A { ... }
  • class B { A x; }

• Sometimes useful with legacy code, but...

• Dangerous feature to use, plus unsafe

  • Relies on implementation of generics, not semantics