Yes, even the experienced Delphi programmers make mistakesIt happens to me all the time, never mind my 10+ years of Delphi programming experience. Compiler errors like "Missing operator or semicolon" or ":= expected but = found", after pressing the F9 key (run command in the Delphi IDE), constantly pop up!
Suppose you are writing a recursive procedure to calculate the factorial value of an integer. The procedure should also output each iteration value to the screen in a console mode Delphi application.
Below is a solution to the problem - to calculate 5! (=120).
The output should look like:
Here's the procedure:
[line 0] procedura ErrorTesting; [line 1] var [line 2] l, k : integer; j : string; [line 3] begin [line 4] j := 1 [line 5] l := 5; [line 6] for k:= 1 to l do [line 7] j = k * j; [line 8] writeln(j) ; [line 9] end;
How many possible compile (or logic) errors (not warnings) can you count?
I'm counting four. In less than 7 lines of code there are 4 errors - and on the first look the code looks ok!
Line 0: Error! Declaration expected but identifier 'procedura' found.
Line 4: Error! Incompatible types: 'String' and 'Integer'.
Line 5: Error! Missing operator or semicolon.
Line 7: Error! := expected but = found
There is even one logic error! If you correct all 4 errors in the source, the program will only output one number: 120.
Errors and How to Prevent ErrorsThere are four types of errors a Delphi programmer can experience while developing, even the simplest program.
Design and compile time errors are seen only by a developer, while run time and logic errors tend to crash your program right in front of the user's eyes!
The good thing about design and compile time errors is that Delphi will not let you build (compile) the project - therefore stopping you deploying a non working version of the application. The bad thing about the run time and logic errors is that Delphi will build the exe for you - but that exe might or might not work properly.
Read on to lean how to prevent and fix common coding errors...