260 likes | 368 Views
Web Based Programming Section 6 . James King 12 August 2003. Key Skills. This section will teach how to: make your programs readable document you programs debug your programs. Making code Readable.
E N D
Web Based Programming Section 6 James King 12 August 2003
Key Skills • This section will teach how to: • make your programs readable • document you programs • debug your programs
Making code Readable • The key to readability is indentation it is difficult to see which bit of code is inside which if or loop if your presentation is messy • If you miss out a { or } it can be difficult to find the correct place to add one
How not to Indent There is a } missing somewhere public void nestit() { for (int i=0; i<3; i=i+1){ System.out.println("top of outer loop i "+i); for (int j=0; j<3; j=j+1) { System.out.println("inside inner loop i "+i+" j "+j); } System.out.println("bottom of outer loop i "+i); }
One Way to indent (Pascal Method) • Each time you type a { all lines following are indented more • Each time you type a } all lines following are indented less • both { and } are on separate lines by themselves • it is easy to match up { and } pairs like this...
One way to Indent (Pascal Method) public void nestit() { for (int i=0; i<3; i=i+1) { System.out.println("top of outer loop i "+i); for (int j=0; j<3; j=j+1) { System.out.println("inside inner loop i "+i+" j "+j); } System.out.println("bottom of outer loop i "+i); } There is a } missing !!
Indentation Style • We don’t mind which way you indent as long as • you are consistent • it is easy to see nesting • it is easy to see where the blocks start and end
Comments and automatic documentation Generation Key skills section
Documentation - Comments • The simplest form of documentation for you code are comments. This allows you to add English descriptions to your code • These can be placed almost anywhere in your code • Comments can extend over multiple lines and are enclosed inside /* comment */ • Single line comments can be made with // comment
Comments Example /* simple demonstration of the how the continue statement works inside a loop */ public void loopit() // takes no parameters { System.out.println("before loop"); for (int i=0; i<5; i=i+1) { System.out.println(" before if"+i); if (i==3) continue; System.out.println(" after if"+i); // bypassed if i is 3 // } oops I commented out the end of block marker by accident... System.out.println("after loop"); }
Automatic Documentation Generation • Java compiler can generate HTML web pages from your comments if you place them in certain places in a certain style • comments go before the declarations /** * about this procedure **/ public void proc() • comments in the body of blocks are ignored
Documentation Tags • In addition to describing the class inside the comment you can add a version and authors name using tags • @version number • @author name • You can document the parameters and return valves of a method • @param variable description • @returndescription • There are more tags...
Documentation Example /** * description of the class * @author (your name) * @version (a version number or a date) */ public class doc { // description of the attribute private int x; /** * description of the constructor */ public doc() {} /** * description of the method * @param y description of the parameter * @return description of the return value */ public int sampleMethod(int y) {return x + y; } }
Debugging Key skills section
Debugging Code • Most compilers come with a debugger which usually allows you to • Step though your program line by line • Examine the value of each attribute and variable in scope • Run the code at full speed and stop at certain points (breakpoint) • In addition BlueJ allows you to • Call a method and optionally provide values for the parameters it requires • Create a new instance of a class
Debuggers are useful in several situations • You want to understand how someone else's code works and it is too complex to run in your head (reverse engineer) • You want to find out why your program is not doing what you expect it to and make it work correctly (debug) • You want to see how Java runs your programs and how the ifs and loops work
Debugging • Debugging is a practical activity so you will learn how to do this in the practical sessions.
Catching and Generating Runtime Errors Advanced Java Facilities
Dealing with problems in a running program • The compiler tries to catch errors during byte code creation. However some errors only occur when the program is running int i=1; i=i-1; int b=3/i; 3 divided by 0 is infinity. Infinity can not be stored in a int The Java interpreter generates a ArithmeticException and stops running the program
Catching Exceptions • To avoid the program stopping we can catch the exception try { int i=1; i=i-1; int b=3/i; } catch (Exceptione) { System.err.println(“CRASH”); } Any exception generated anywhere in this block of code will cause execution to jump to the catch block, skipping any remaining code. If there is no exception the catch block is skipped In either case execution eventually gets here
Information about the Exception • The e in catch (Exception e) acts just like a parameter and is actually an object. It contains information about the exception e.printStackTrace(); displays on the console screen the class and line number the exception was generated at e.getMessage(); returns a string with details of why the exception occurred
Catching different types of exceptions try { } Catch (ArithmeticException f) { } catch (Exception e) { } This will catch only ArithmeticException and any subclasses Since all Exceptions are subclasses of Exception this will pick up all other exceptions
Cleaning up the mess • Sometimes regardless of if an exception is generated or not you want some code to be executed try { } catch (Exception e) { } finally { }
Exceptions • Java built in methods and classes may generate exceptions if used incorrectly • The Java compiler expects you to either catch these possible exceptions in the method they could be created in or leave it to the method that called that method to handle them…
Throws • Java expects each method to catch any exceptions it could generate • If you don’t want to do this you must add the uncaught exceptions to the method definition in a throws list void problem2() throws Exception { throw new Exception(); }