Presentation is loading. Please wait.

Presentation is loading. Please wait.

Well-behaved objects Debugging. 2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Prevention vs Detection.

Similar presentations


Presentation on theme: "Well-behaved objects Debugging. 2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Prevention vs Detection."— Presentation transcript:

1 Well-behaved objects Debugging

2 2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Prevention vs Detection (Developer vs Maintainer) We can lessen the likelihood of errors. Use software engineering techniques, like encapsulation. We can improve the chances of detection. Use software engineering practices, like modularization and documentation. We can develop detection skills.

3 3 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Debugging techniques Manual walkthroughs Print statements Debuggers

4 4 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Modularization and interfaces Applications often consist of different modules. –E.g. so that different teams can work on them. The interface between modules must be clearly specified. –Supports independent concurrent development. –Increases the likelihood of successful integration.

5 5 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Modularization in a calculator Each module does not need to know implementation details of the other. –User controls could be a GUI or a hardware device. –Logic could be hardware or software.

6 6 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Method signatures as an interface // Return the value to be displayed. public int getDisplayValue(); // Call when a digit button is pressed. public void numberPressed(int number); // Call when a plus operator is pressed. public void plus(); // Call when a minus operator is pressed. public void minus(); // Call to complete a calculation. public void equals(); // Call to reset the calculator. public void clear();

7 7 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Debugging It is important to develop code-reading skills. –Debugging will often be performed on others’ code. Techniques and tools exist to support the debugging process. Explore through the calculator-engine project.

8 8 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Manual walkthroughs Relatively underused. –A low-tech approach. –More powerful than appreciated. Get away from the computer! ‘Run’ a program by hand. High-level (Step) or low-level (Step into) views.

9 9 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Tabulating object state An object’s behavior is usually determined by its state. Incorrect behavior is often the result of incorrect state. Tabulate the values of all fields. Document state changes after each method call.

10 10 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Verbal walkthroughs Explain to someone else what the code is doing. –They might spot the error. –The process of explaining might help you to spot it for yourself. Group-based processes exist for conducting formal walkthroughs or inspections.

11 11 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Print statements The most popular technique. No special tools required. All programming languages support them. Only effective if the right methods are documented. Output may be voluminous! Turning off and on requires forethought. –can be turned on and off using Boolean variable

12 12 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Choosing a test strategy Be aware of the available strategies. Choose strategies appropriate to the point of development. Automate whenever possible. –Reduces tedium. –Reduces human error. –Makes (re)testing more likely.

13 13 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Debuggers Debuggers are both language- and environment-specific. –BlueJ has an integrated debugger. Support breakpoints. Step and Step-into controlled execution. Call sequence (stack). Object state.

14 14 Debugger – cont’d Undoubtedly useful facility but should be used in a methodical fashion. E.g. 1.From the symptoms observed, infer where the bug lies – possibly within 2 or 3 methods 2.Place breakpoints at the entry and exit of suspected methods 3.Run the program. When the program stops examine the values of the methods’ arguments and subsequently any return value. Identify the appropriate method. 4.Re-run the program stopping at the erroneous method and then single step through it until the cause has been identified.

15 15 Example E.g.Suppose we wish to monitor the determination of an average in the studentAverage project. Create a student object “Alan” and submit a mark 50 Open Editor on class Student and place breakpoint next to: i.e. click in margin alongside line. Call submitMark and enter 60. submissions += 1;

16 16 Debugger example 1.Start

17 17 Example – 2 2.Click to move on to next statement.

18 18 Example – 3 3.Click to move on to next statement.

19 19 Example – 4 4.When the method has been completed, execution will be stopped to await the next method call. call submitMark, again, and enter 100. then continue step by step with or continue to the end with finally click

20 20 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Review Errors are a fact of life in programs. Good software engineering techniques can reduce their occurrence. Testing and debugging skills are essential. Make testing a habit. Automate testing where possible. Practice a range of debugging skills.


Download ppt "Well-behaved objects Debugging. 2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Prevention vs Detection."

Similar presentations


Ads by Google