Week 10Tracing, Testing,Debugging, APIDesign
© 2004 Pearson Addison-Wesley. All rights reserved 5-2Copyright WarningCOMMONWEALTH OF AUSTRALIACopyright Regulations 1969...
© 2004 Pearson Addison-Wesley. All rights reserved 5-3Robust Programs• We have seen that our programs can be fragile• Prog...
© 2004 Pearson Addison-Wesley. All rights reserved 5-4Why Do Programs Have Bugs?• Programs have bugs for several reasons:–...
© 2004 Pearson Addison-Wesley. All rights reserved 5-5Tracing Programs• We dont think like a computer, but to write goodpr...
© 2004 Pearson Addison-Wesley. All rights reserved 5-6Tracing Programs• Trace the following code when it is given the inpu...
© 2004 Pearson Addison-Wesley. All rights reserved 5-7Tracing Programs• Why tracing?• It forces us to read each line of co...
© 2004 Pearson Addison-Wesley. All rights reserved 5-8Debugging Programs• Debugging is like tracing but the computer looks...
© 2004 Pearson Addison-Wesley. All rights reserved 5-9Debugging Programs• Find the bug(s) in this method:// Return the pos...
© 2004 Pearson Addison-Wesley. All rights reserved 5-10Methods and Inputs• Every method has a declaration, which is itscon...
© 2004 Pearson Addison-Wesley. All rights reserved 5-11Unit Testing• All large programs have bugs: usual estimate is 1bug ...
© 2004 Pearson Addison-Wesley. All rights reserved 5-12Unit Testing• You should design the method interface first• THEN, c...
© 2004 Pearson Addison-Wesley. All rights reserved 5-13Unit Test Example• Design the declaration of a method isATriangle()...
© 2004 Pearson Addison-Wesley. All rights reserved 5-14API Design• A method provides an interface: name, inputs,output• A ...
© 2004 Pearson Addison-Wesley. All rights reserved 5-15API Design• Method arguments: keep consistent order acrossthe metho...
© 2004 Pearson Addison-Wesley. All rights reserved 5-16API Design• A method provides an interface: name, inputs,output• Cl...
Upcoming SlideShare
Loading in …5
×

Week10style

100 views
72 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
100
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Week10style

  1. 1. Week 10Tracing, Testing,Debugging, APIDesign
  2. 2. © 2004 Pearson Addison-Wesley. All rights reserved 5-2Copyright WarningCOMMONWEALTH OF AUSTRALIACopyright Regulations 1969WARNINGThis material has been copied and communicated to you by oron behalf of Bond University pursuant to Part VB of theCopyright Act 1968 (the Act).The material in this communication may be subject to copyrightunder the Act. Any further copying or communication of thismaterial by you may be the subject of copyright protectionunder the Act.
  3. 3. © 2004 Pearson Addison-Wesley. All rights reserved 5-3Robust Programs• We have seen that our programs can be fragile• Programs also need to be read by humans so theycan be fixed and extended• This week we are looking at techniques to makeour programs more robust, and more easilyunderstood by other programmers• We will be looking at:– Tracing, debugging and unit testing– Good API design
  4. 4. © 2004 Pearson Addison-Wesley. All rights reserved 5-4Why Do Programs Have Bugs?• Programs have bugs for several reasons:– Computers DWIS not DWIM– Inputs can have any or all possible values– We dont understand the language subtleties– Our understanding of the interfaces between themodules in our program, and with externalmodules, is wrong• Lets look at some ways of combatting theseproblems
  5. 5. © 2004 Pearson Addison-Wesley. All rights reserved 5-5Tracing Programs• We dont think like a computer, but to write goodprograms we need to.• Tracing a program means to step through somecode, by hand, and track all the variable valuesand decisions made, to ensure that the code isdoing exactly what it should be doing.• If we dont understand our own code, we can fallinto the trap of shotgun debugging: makingrandom changes in the hope that any problemswill disappear.
  6. 6. © 2004 Pearson Addison-Wesley. All rights reserved 5-6Tracing Programs• Trace the following code when it is given the input “Paul McCartney”public static String changeString(String s){String x = "";for (int i=0; i < s.length(); i += 2){if (s.charAt(i) < M)x = x + ".";x = x + Character.toUpperCase( s.charAt(i) );}return(x);}
  7. 7. © 2004 Pearson Addison-Wesley. All rights reserved 5-7Tracing Programs• Why tracing?• It forces us to read each line of code, think aboutwhat it is doing, why it is there, and what sideeffects each line has• It also makes us do things slowly• We will focus more deeply on the code• Sometimes, we also reflect on questions such asthe quality of the code, could it be done better etc.
  8. 8. © 2004 Pearson Addison-Wesley. All rights reserved 5-8Debugging Programs• Debugging is like tracing but the computer looksafter the display of variables and the steppingthrough the code• Dont start at main(). Try to identify where the bugis, and debug from just before the bug• Learn how to:– Set up breakpoints (stop signs)– Step over lines of code– Visualise the variables values– Step over, and into, methods
  9. 9. © 2004 Pearson Addison-Wesley. All rights reserved 5-9Debugging Programs• Find the bug(s) in this method:// Return the position of the first ch in the String,// or return -1 if ch is not in the String.public static int findLetter(String str, char ch){for (int i=0; i <= str.length(); i++)if (str.charAt(i) == ch)return(i);return(-1);}
  10. 10. © 2004 Pearson Addison-Wesley. All rights reserved 5-10Methods and Inputs• Every method has a declaration, which is itscontract: if you give me this input, I promise toreturn this value• Inputs, regardless of where they come from, areproblematic• If an input is of type X, any valid X value may comein as input: int: zero, negative values, large values floats: 0.0, INFINITY, very small values Strings: empty String, null char: non-printable chars, non-English chars
  11. 11. © 2004 Pearson Addison-Wesley. All rights reserved 5-11Unit Testing• All large programs have bugs: usual estimate is 1bug per 100 lines of code• Most bugs are logic bugs: the programmer thinksit works, but the code does something different• Its nearly impossible to tell if a program is 100%bug-free• Implication: programs should be tested thorouglyto give assurance there are no bugs• Unit testing: test a method with a large set ofinputs, to ensure that it produces the correctoutput• The test set is designed to stress the method asmuch as possible
  12. 12. © 2004 Pearson Addison-Wesley. All rights reserved 5-12Unit Testing• You should design the method interface first• THEN, construct a suitable test set for it Include good and bad inputs• ONLY THEN, write the actual method code• Once you think you have written the method, youcan then test the method with the unit tests• “Coding isnt complete until all the unit tests pass”• ALWAYS generate the tests by hand. Dont use aprogram to generate the test set. Why not?• Bluej provides a framework to construct unit tests.• You will see it in the labs, and you will beassessed on unit testing
  13. 13. © 2004 Pearson Addison-Wesley. All rights reserved 5-13Unit Test Example• Design the declaration of a method isATriangle()• Given 3 side lengths, return true if they represent atriangle, false otherwise• Create a set of unit tests (inputs, output) that youcan use to test the method.• In BlueJ, write the method to always return false So we can write the unit tests, which will fail• Now go and write the code.• Once all the unit tests pass, you now haveconfidence that the code works
  14. 14. © 2004 Pearson Addison-Wesley. All rights reserved 5-14API Design• A method provides an interface: name, inputs,output• A Java class provides a set of interfaces, knownas an API: application program interface• APIs can be designed well, or extremely bad• Here are some tips on good API design• Names mean everything. Choose wisely• Methods perform actions. Use action names, e.g.getUserName() not UserName()• Make it obvious if a method is a getter or a setter• Boolean methods: use “Is”, e.g. isEnrolled()
  15. 15. © 2004 Pearson Addison-Wesley. All rights reserved 5-15API Design• Method arguments: keep consistent order acrossthe methods in the API, e.g. (hroom, wroom) inAssignment 2.• Make it hard for user to get wrong, easy to getright e.g if method takes 2 ints and a char, make itmethod(int, char, int), so hard to get the ints thewrong way around• Encapsulation: private data, public methods,public constants, private helper methods
  16. 16. © 2004 Pearson Addison-Wesley. All rights reserved 5-16API Design• A method provides an interface: name, inputs,output• Clear and concise documentation at the top ofeach method tells the user what it does, and how itdeals with input errors.• Design a clear description of the isATriangle()method

×