Something that I strongly discourage Delphi developers from using is the with statement. I never used to dislike the with statement until I got burned pretty badly by a bug caused by the use of one.
You can’t debug the with statement.
You can’t watch a property if it’s in a with block (check out my example), there is no tool tip evaluation for these properties either.
Small example showing the hazards of debugging a with statement.
1. Create a new win32 application (steps for D2005 – should be the same for all Delphi versions)
2. Add a button to the form
3. Create an OnCreate event
4. Add the following code:
procedure TForm1.FormCreate(Sender: TObject);
begin
with Button1 do
Caption := ‘Hello’;
end;
5. Put a breakpoint on the with line and add Caption to the watch list.
6. Run the application and press the button.
Although it’s probably better that the debugger doesn’t cope with this as it might encourage more widespread use of with.
Perhaps the compiler should be enhanced to fulfil the request of this report in QualityCentral?
Defect No: 2667 Status: ReportedAdd ambiguous warnings to WITH statements
http://qc.borland.com/wc/wc.exe/details?ReportID=2667
One final thing before I end this post. The VCL is starting to get a large number of with statements added to it and it needs to stop. Especially in message handlers where you often see the following:
with Message do
begin
// 30 lines of code here
end;
The string “with Message do” was found 101 times in my D2005 VCL source. Think the 30 lines is an overstatement? Open up Forms.pas and check out the TCustomForm.WndProc method (it’s over 100 lines).
This makes debugging a nightmare. If you debug the VCL often (as I do) and find this annoying, you may want to vote for the following QualityCentral report:
Defect No: 13515 Status: Reported
Remove all occurrences of the with statement from the VCL
http://qc.borland.com/wc/wc.exe/details?ReportID=13515
Looking for a QualityCentral win32 client?
Check out my QC Client – tentatively called JED, QC from http://www.jed-software.com/.
Want to beta test the soon to be released Version 1 (mega changes from the website version)? Send me an email requesting the download information (email on website).