Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture Roger Sutton CO331 Visual programming 15: Debugging 1.

Similar presentations


Presentation on theme: "Lecture Roger Sutton CO331 Visual programming 15: Debugging 1."— Presentation transcript:

1 Lecture Roger Sutton c.r.sutton@kent.ac.uk CO331 Visual programming 15: Debugging 1

2 CO331 Visual Programming Introduction In constructing a program, the code produced by the programmer, usually referred to as source code, will progress through three stages: 1.Compilation – A programming language usually comprises  a set of reserved words, i.e. sequences of characters, that have a particular interpretation, and  a set of rules governing the acceptable input of other characters. This is referred to as the language’s syntax. Once a program has been written, it is submitted to a compiler. This is a program which examines the code character by character to ensure that it complies with the language’s syntax. If not, the compiler produces a report usually indicating the point at which the code started to deviate with a possible explanation. Such instances are called compilation or syntax errors. 2

3 CO331 Visual Programming Introduction – cont’d Working within an IDE, code is usually checked as it is typed in. In such cases the code is being interpreted as opposed to compiled – hence the term interpreter. Once the source code has compiled without error it is converted into a more machine readable form called object or binary code. In the process it will be noted whether any library methods or programmer-written classes are required. 2.Linking Here any necessary library methods or programmer-written classes are incorporated. If the code is being interpreted and these are not available, it will be detected early on, following the syntax check. 3.Running When the source code has been compiled and linked successfully, it is loaded to produce machine code and the program can then be run. 3

4 CO331 Visual Programming Errors ‘That’s not a bug it’s a feature !’ Syntax Compilation Errors - coding that does not conform to the language specification. Run-time Although syntax error-free, program may fail as the result of a situation developing that is inconsistent with program constraints or which cannot be handled by the program. Logic Errors in the program design that produce unacceptable results. 4

5 CO331 Visual Programming Error detection Understanding how your program works is essential in the search for errors. The effort required is often reduced substantially with the aid of a debugger. A debugger is a facility of the IDE and is a program that lets programmers execute their software one step at a time; allows them to stop and start at different points, and to examine the value of the variables. 5

6 CO331 Visual Programming Syntax Errors To ensure the greatest possible error checking is brought into action, it is recommended that the options Explicit and Strict are switched on. Explicit: ensures that the compiler checks that all variables have been declared. Strict: requires the compiler to check that all data conversions have been carried out explicitly. 6

7 CO331 Visual Programming Syntax Errors – cont’d The zig-zag underlining indicates a syntax error. Hovering over the error provides some indication of the nature of the error. E.g. Correcting the variable name ‘num’ and substituting ‘Text’ for ‘value’ produces: Requires: 7 n = CInt(txtNumber.Text)

8 CO331 Visual Programming Run-time errors As a program runs, variables usually take a number of different values - the program is said to be in different states. Sometimes the values of variables become inconsistent, i.e. they might not comply with previous declarations or it is not possible to perform required operations. E.g. A value assigned to a variable is not of the correct type An array subscript value is outside the declared range An attempt is made to divide by a variable whose value is zero An attempt is made to find the square root of a negative value These are run-time errors since they become apparent when the program is run in particular circumstances. A program might appear to run satisfactorily for one set of input but ‘crash’ (break, fall-over) for another; hence the need for methodical testing. 8

9 CO331 Visual Programming Run-time errors – cont’d E.g. Entering a value 9 in the textbox produces: Clicking Break and hovering over the array subscript produces : 9 The upper bound of the array is declared to be 8. So is the declaration wrong or is there an error in the program logic?

10 CO331 Visual Programming Debugging facilities The VB IDE debugger allows a program to be executed one step at a time and to examine the changing values of the variables. Breakpoints – allow a program to be halted at specified points. Breakpoints are introduced by clicking the margin, level with the line at which the program is to halt. Values of variables can then be examined by hovering over the relevant variable. Execution is restarted by clicking the Continue arrow. 10

11 CO331 Visual Programming Debugging facilities – cont’d Breakpoints are removed by re-clicking the margin or, alternatively, by selecting the Clear All Breakpoints in the Debug menu Watch Window - sometimes it is useful to examine several variables together. Once the program has halted, variables can be added to a watch window panel by selecting the variable, right-clicking and selecting Add Watch. The variables name, value and type are then displayed in the panel to allow continuous monitoring. 11

12 CO331 Visual Programming Debugging facilities – cont’d Single stepping Alternatively a program may be executed one step at a time by selecting Step Into from the Debug menu or using the function key: It is usual to use breakpoints and single stepping together to focus on particular sections of code. The yellow arrow and highlighting indicates the next line to be executed. Changes of value are shown in red in the watch window. 12

13 CO331 Visual Programming Debugging facilities – cont’d Single stepping – cont’d Two other stepping processes are available:  Step over: When a method call is reached, single-stepping is suspended for the called method to be executed and then continued with the step immediately following the call.  Step Out: Finishes executing the method and returns control to the step immediately following the call. 13

14 CO331 Visual Programming Debugging strategy The debugger is a 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. In this way the method containing the error may be identified. 4.Re-run the program stopping at the erroneous method and then single step through it until the cause has been pin-pointed. 14

15 CO331 Visual Programming 15 Summary Compilation process and error types  Compiling – syntax errors  Linking  Running – run-time, logic errors Explicit and Strict Debugger  Zig-zag line, hovering  Breakpoints  Stepping process: into, over, out  Watch window Debugging strategy


Download ppt "Lecture Roger Sutton CO331 Visual programming 15: Debugging 1."

Similar presentations


Ads by Google