32
Programming & Debugging

Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging

Embed Size (px)

DESCRIPTION

Modularity Why? –Smaller pieces are easier to understand –Easier to isolate proper piece for debugging, modification –Good modules are easier to reuse How? –Abstraction –Obiect-oriented Design

Citation preview

Page 1: Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging

Programming & Debugging

Page 2: Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging

Key Programming Issues

• Modularity• Modifiability• Ease of Use • Fail-safe programming• Style• Debugging

Page 3: Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging

Modularity

• Why?– Smaller pieces are easier to understand– Easier to isolate proper piece for

debugging, modification– Good modules are easier to reuse

• How?– Abstraction– Obiect-oriented Design

Page 4: Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging

Modifiability

• Why?– Programs are never static; problems change– Avoid reinventing the wheel as much as possible

• How?– Good structure (classes & functions)– Named constants– Typedef (e.g. typedef float RealType;)– Good documentation!

Page 5: Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging

Ease of Use

• Why?– So it will be used

• How?– Clear prompts for input– Echo the input– Clear output– Adapt program to user, not vice versa– See CPSC 222 for more!

Page 6: Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging

Fail-Safe Programming

• Why?– Input isn’t always what it should be (from

users or otherwise)• How?

– Validate input data (ensure preconditions satisfied)

– Use assert to check preconditions and invariants (assertions)

Page 7: Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging

Style

• Why?– Good style addresses all the other issues!– Programs are also read by people

• How?– Style guides vary– Pick one (the textbook guidelines are good) and

be consistent!– Many of the “style” guidelines in the book would

come under good modularity in my mind.

Page 8: Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging

Issues of Style

• Reasonable-sized methods• Private data members

(inspectors/accessors & mutators as necessary)

• Avoid global variables• Use void methods for side effects• Readability & Documentation

Page 9: Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging

Readability & Documentation• Good (consistent!) use of indentation• Liberal use of blank space• Useful identifier names• Identify author/date on each file (use JavaDoc template)• Comment per class (what it provides)• Comment for any non-obvious data item (class member

or local variable)• Comment per method (pre and post conditions, what it

does, explain parameters)• Comment at important / tricky steps in function (e.g.

invariants)

Page 10: Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging

Debugging

• Why?– Because there are bugs

• How?– Compiler’s help (for syntax errors)– Output statements– IDE Debugger

• Break• Watch• Step

Page 11: Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging

Program Defects and “Bugs”

• A program may be efficient, but is worthless if it produces a wrong answer

• Defects often appear in software after it is delivered

• Testing can never demonstrate the complete absence of defects

• In some situations it is very difficult to test a software product completely in the environment in which it is used

• Debugging: removing defects

Page 12: Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging

Types of Errors

• Syntax Error• Runtime Error (or Exception)• Logic Error

Page 13: Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging

Syntax Errors

• Mistakes in grammar of the language• Detected by compiler; prevent successful

compilation• Examples:

– Omitting or misplacing braces or semicolons– Performing an incorrect type of operation on a

primitive type value– Invoking an instance method not defined – Not declaring a variable before using it– Providing multiple declarations of a variable

Page 14: Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging

Run-time Errors or Exceptions

• Run-time errors– Occur during program execution– Occur when the JVM detects an operation that it

knows to be incorrect– Cause the JVM to throw an exception

• Examples of run-time errors include– Integer diivision by zero– Array index out of bounds– Number format and Input mismatch error– Null pointer exceptions

Page 15: Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging

Java Exceptions

Page 16: Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging

Logic Errors

• The program runs without error, but doesn’t do what was expected (in at least one case)

• Examples:– Calculates the wrong answer– Does not halt– Ignores valid inputs or data

Page 17: Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging

Logic Errors

• A logic error occurs when the programmer or analyst– Made a mistake in the design of a class or method– Implemented an algorithm incorrectly

• Most logic errors do not cause syntax or run-time errors and are thus difficult to find

• Sometimes found through testing• Sometimes found during real-world operation

of the program

Page 18: Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging

The Exception Class Hierarchy

• When an exception is thrown, one of the Java exception classes is instantiated

• Exceptions are defined within a class hierarchy that has the class Throwable as its superclass

• Classes Error and Exception are subclasses of Throwable

• RuntimeException is a subclass of Exception

Page 19: Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging

Throwable Exception Hierarchy

Page 20: Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging

Methods of Throwable

(Inherited by subclasses)

Page 21: Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging

Checked and Unchecked Exceptions

• Checked exception– Beyond control of programmer– Subclass of Exception (but not RuntimeException)

• Unchecked exception may result from– Programmer error– Serious external conditions that are unrecoverable– Subclasses of RuntimeException

Page 22: Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging

Example: Checked Exceptions

Page 23: Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging

Exception Hierarchy

Unchecked exceptions

Page 24: Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging

Handling Exceptions

• Unchecked exceptions (including errors)– These are considered unrecoverable– Programmers not “expected” to handle

them (but expected not to cause them)• Checked exceptions

– Due to external conditions, often recoverable

Page 25: Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging

Catching and Handling Exceptions

• When an exception is thrown, the normal sequence of execution is interrupted

• Default behavior– Program stops– JVM displays an error message and stack trace

• The programmer may override the default behavior by – Enclosing statements in a try block– Processing the exception in a catch block

Page 26: Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging

The try-catch-finally Sequence

• Avoid uncaught exceptions– Write a try-catch sequence to catch an exception– Handle it rather than relying on the JVM

• Catch block is skipped if all statements within the try block execute without error

• Finally (if provided) is executed after try block or catch block– Use this to “clean up” (e.g. close open files)

Page 27: Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging

Try-catch-finallytry { //statements that might cause exception}catch (EOFException ex){ //code for EOF Exception}catch(IOException ex){ //code for IO Exception}finally{ //code executed after try or catch code}

Page 28: Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging

Handling Exceptions to Recover from Errors

• Exceptions provide the opportunity to– Recover from errors (preferable, if possible)– Report errors

• First matching Catch block (only) is executed– Match according to the type of exception

• Compiler displays an error message if it encounters an unreachable catch clause– Example: IOException with no IO in try clause

Page 29: Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging

Throwing Exceptions

• If method doesn’t catch exception, it can throw it (to its caller)– Add a throws clause to the method header– Explicitly throw the exception, using a

throw statement• The throws clause is useful if a higher-

level module already contains a catch clause for this exception type

Page 30: Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging

Throw statement

• Can use a throw statement in a lower-level method to indicate that an error condition has been detected

• Once the throw statement executes, the lower-level method stops executing immediately

• Throw statement useful for user-defined exceptions (e.g. item not found)

Page 31: Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging

Catching Exceptions Example

Page 32: Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging

Guidelines for Exceptions

• If an exception is recoverable in the current method, handle the exception in the current method

• If a checked exception is likely to be caught in a higher-level method, declare that it can occur using a throws clause

• It is not necessary to use a throws clause with unchecked exceptions