ITWales.com

Section 6: Running and debugging programs in the editor


Delphi's IDE contains many facilities to help you with the task of finding the problems in your code. Clearly syntactic errors will be caught by the compiler, and when they are, they are reported in the list that is displayed below the code editing window. Delphi will automatically jump to the first error in this list. If you decide you want to have a go at finding the next one, Delphi will jump directly to it if you double click on the error message itself.

Whilst your program will run without the need to remove all of the hints and warnings from your code, it is strongly advised that you do whatever it takes to prevent Delphi from reporting any hints or warnings. This is for two reasons:

1) Delphi doesn't do this for the sake of it, and your code will almost certainly be more robust, efficient and bug-free if you do so.
2) If you get in to the habit of ignoring hints and warning you consider unimportant, you will probably not notice the number of hints and warnings increasing, and will ultimately miss an important one.

Once you have a clean compile of your code, with no hints and no warnings, you are ready to run it. Because Delphi is a true compiler, there are two ways in which this can be achieved.

1) By running the code inside the Delphi IDE
2) By running it as a standalone executable

There are advantages and disadvantages to both, and it is important as a developer that you experiment with both.

Advantages of running the code in the IDE:

1) Useful for debugging
2) Places extra load on machine - Useful for ironing out problems caused by low resources

Disadvantages of running the code in the IDE

1) Arguably more likely to hang
2) Can be slower (although in practice hard to detect)
3) Large projects can run into resource problems that simply don't exist when running standalone



To run a compiled program (and Delphi will re-compile automatically if it considers it necessary), you can simply press the F9 key, or click on the green Run/Play button on the main button bar.




If at any time whilst the program is running, you want to pause the program, and have a look at which part of the code is current running, then you can press the pause button (to the right of the play/run button) to temporarily pause the program. When you do this, Delphi shows a highlight bar in the code window, to indicate the line that will be executed in the next step.


Once the program is "paused" like this (Delphi considers such a program to be "Stopped" rather than paused, which is a little misleading), you can continue the program by pressing F9 or by clicking on the run button.

More useful, however, is the facility to "trace" into the program, and watch its progress line by line. Delphi provides this functionality both in the form of buttons and function keys. Delphi differentiates between tracing "into" and tracing "over". If you reach a point in your code where a call is made to another procedure or function, you can elected to trace "into" if you want to see Delphi execute the called procedure, or you can select trace "over" if you are happy to just view that particular call as an atomic action.

Tracing into can be performed by pressing the left of the two highlighted buttons, or pressing the F7 key. Tracing over can be performed by pressing the right of the two highlighted buttons, or by pressing the F8 key.






























As you trace through the code, Delphi will show a blue highlight bar and a green arrow to the left of the line that is about to be executed. Commands take a significantly longer time to process when the IDE is running them line by line, so be patient. Experience dictates that pressing the F7 or F8 keys too quickly (or worse still, holding them down and getting auto-repeat to do the work for you) can cause the debugging tool to crash, hanging Delphi.

If you want to step through a larger number of lines, then one final useful tracing key is provided. By pressing the F4 key, Delphi will run the program, until it reaches the line the cursor was on at the time you pressed the F4 key. So, to skip a batch of lines, simply place the cursor at the point you want to regain control, and press the F4 key. There is a menu command equivalent to this function key, but by default, no button on the button bar.

















Being able to stop the program at any point, and trace it through line by line is fairly useful, but it is only part of the story. To make this facility really useful, we need to be able to see what is happening to the variables in our program, as they are manipulated by the code. In the next section, we look in greater detail at the facilities Delphi provides to allow this.
From a personal perspective I strongly recommend the use of line by line running of each new function and procedure you write (within reason). In fact I have invented my own coding methodology (modestly called the Mikey Method - well that's what I call it!) - it's slow but it leads to solid code and it works like this:

1) Assume any function or procedure that you write contains an error.
2) Attempt to prove this is the case by running it line by line
3) Before each line runs, work out in your mind, what you EXPECT to happen
4) Compare this to what DOES happen and if the two are the same, consider any input under which the required result might diverge from the actual result
5) Change the code immediately to correct any observed faults
6) Use the reset function on the run menu to exit the code
7) Place a breakpoint at the point where you made your correction
8) Re-run the program and continue from where you left off



Home, Services, Events, Features, Interviews, Profiles, Reviews, News, Resources, Press