CMSC 433 – Programming LanguageTechnologies and ParadigmsSpring 2004Interactive Development Environments,Testing, and DebuggingFebruary 12, 2004Administrivia• Reading: Liskov ch 5, 8• Project 2 posted– JUnit and testingInteractive 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,emacsDr. 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 usefulDebugging• 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!– IterateStarting 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 problemChecking 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 testingDr. 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 inprogram text– Combines with interactive debuggerDr. 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 changeUsing the Debugger• Set debug mode to on– Turns on debug panel with state information• Set break point(s) in Java source• Run the programTips• Make bug reproducible– If it’s not reproducible, what does that imply?• Boil down to smallest program that reproducesbug– Reveals the core problem• Explain problem to someone else (i.e., instructoror TA)– Explaining may reveal the flaw in your logic• Keep notes: don’t make the same mistake twiceDefensive Programming• Assume that other methods/classes are broken– They will mis-use your interfacepublic Vector(int initialCapacity, int capacityIncrement){ super(); if (initialCapacity < 0) throw new IllegalArgumentException( "Illegal Capacity: "+ initialCapacity); ... }• Goal: Identify errors as soon as possibleAvoiding Errors• Codify your assumptions– Include checks when entering/exiting functions,iterating on loops• Test as you go– Using Junit– Using the on-line debugger• Re-test when you fix a bug– Be sure you didn’t introduce a new bug• Do not ignore possible error states– Deal with exceptions appropriatelyCMSC 433 – Programming LanguageTechnologies and ParadigmsSpring 2004Abstraction and Parametric PolymorphismFebruary 12, 2004Data Abstraction• Data abstraction = objects + operations– List + { addFirst, addLast, removeFirst, ... }– Set + { add, contains, ...}• Categories of operations– Constructors (creators/producers)– Mutators– ObserversAbstraction Function• Specification for data structure is abstract• Implementation of data structure is concrete• How do you know if implementation meets thespec?• Abstraction function : concrete abstract– Relates implementation to abstractionExampleclass IntSet { int[] elts; ... }– AF(s) = { s.elts[i] | 0 <= i <= elts.length }• You always need an abstraction function whenyou build a data abstraction– Often it’s implicitRepresentation Invariant(s)• Properties of data structure that must always hold– After the constructor has finished– Before and after each operation class IntSet { // rep inv: elts contains no duplicates int[] elts; ... }• Part of the (internal) specificationImplementing the Rep Invariant• Interesting idea: Write a function to check the reppublic 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 representationclass IntSet { int[] elts; int[] getElements () { return elts; }}• Why?– Other people may rely on implementation details– Clients may violate the rep invariantPolymorphism• Recall that B is a subtype of A if, everywhere youexpect 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 polymorphismPolymorphism Using Objectclass 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 ClientIntegerStack 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 ofstacks?– Need to make one XStack for each kind of X– Problems: Code bloat, maintainability nightmarePolymorphism Using Objectclass 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 ClientStack is = new Stack();Integer i;is.push(new
View Full Document