Delphi Handbooks Collection


Delphi XE Handbook


Delphi 2010 Handbook


June 26, 2007

If With is Considered Harmful, What About Double With?

I use the with statement, sparingly, but using a with statement with multiple data structure is not something I like, but the VCL does it.

The With statement is considered harmful by many Delphi developers. I agree only partially. The with TMyClass.Create do construct can come handy at times. Truly, this code looks simple but is often harder to read, as you don't know if a member has to be applied to the object within the with statement or the current object. This is because we don't tend to write code with the Java-style of prefixing local fields with this (or self).

Anyway you feel about the with statement, using a double one is really a little too much. In theory, you can write:

      with Object1, Object2 do 
    

but at this point if the code is within a method the field name you type can belong the current object or one of the two in the with statement. THis is why I wasn't happy to read in the VCL source code (of Delphi 2007) the following:

      with LMargins, GlassFrame do
      begin
        if Enabled then
        begin
          if not SheetOfGlass then
          begin
            cxLeftWidth := Left;          
            cxRightWidth := Right;

The assignments int he last two lines I've quoted are not too bad, but what about Enabled? Which object does it belong to? And what about SheetOfGlass? They are both part of TGlassFrame, but this is not very readable. To make matters worse, the LMargins data structure referenced here is declared as:

LMargins: UxTheme.TMargins;

The unit name is there because there is another (totally different) TMargins data type, a new class of the VCL used for the Margins property of the form (which the current class of the method displayed above). Oh, my!





 

8 Comments

If With is Considered Harmful, What About Double With? 

Weirder than the double with is your code formatting!
:) Almost have to copy/paste it into an editor and
format it properly so as to understand it! (Almost!)

If you can't or don't want to use Windows Live Writer,
how about trying out Steve Trefethen's Code Syntax
Highlighter? You can find it here:
http://www.stevetrefethen.com/highlighter/
Comment by Fernando Madruga [http://memyselfanddelphi.blogspot.com] on June 26, 12:47

If With is Considered Harmful, What About Double With? 

Fernando,
  you are right, the formatting was terrible... should
be a little better now. Using Windows Live writer
means I should support its API on the server, which is
a little too much work for now. I do have an
highlighter... should not post that much in a hurry.
Comment by Marco Cantù [http://www.marcocantu.com] on June 26, 13:03

If With is Considered Harmful, What About Double With? 

"The unit name is there because there is another
(totally different) TMargins data type, a new class of
the VCL used for the Margins property of the form
(which the current class of the method displayed
above). Oh, my!"

...though not unprecedented of course
(Graphics.TBitmap <> Windows.TBitmap).
Comment by CR on June 26, 16:17

If With is Considered Harmful, What About Double With? 

According to Help (D7) multiple (more than double)
With is possible.

And finally something I just discovered reading the
post is this from the help:

"In this case, each variable reference or method name
in statement is interpreted, if possible, as a member
of objn; otherwise it is interpreted, if possible, as
a member of objn-1; and so forth. The same rule
applies to interpreting the objs themselves, so that,
for instance, if objn is a member of both obj1 and
obj2, it is interpreted as obj2.objn."

Comment by Daniel Luyo Murata on June 26, 17:10

If With is Considered Harmful, What About Double With? 

The double with is an abomination.  The minute amount
of time that you save by not having to type the object
name to prefix the variable is more than used up by
the time you'll need to read that code 6 months after
writing it.  

The bigger danger is when you add a new property to
one of the objects that has the same name as a local
property referenced in the with block.  Hilarity will
not ensue.

I used to use "with TMyClass.Create do..." blocks, but
I tend to avoid that construct now.  It's hard to view
the contents of an unnamed object in the debugger. 
Couple that with the other abomination FreeAndNil (why
didn't they just call that one NilAndFree?) and trying
inspect variables at runtime becomes unnecessarily
difficult.
Comment by Chris Miller [http://anotherlab.rajapet.net] on June 26, 17:45

If With is Considered Harmful, What About Double With? 

Please forgive my naivety, I have been a loyal Delphi
programmer since version 1 and Turbo Pascal since it
cost less than $40.  But I had to write VB6 code for
eight years recently.  VB6 only allows a single WITH
and insists that you prefix each of the members of the
WITH object with a ".".  That made it plenty clear
what referred to what.  I am sure that this solution
was offered before.  I don't understand why we need to
give up the convenience of WITH when there is a fix
that works so well.
Comment by jr on June 26, 18:21

If With is Considered Harmful, What About Double With? 

I have to confess that I use the with statement from
time to time, always with a pang of guilt, but I just
can't help myself.  It's that "I know this is wrong
but I'm going to do it anyway" feeling.  Maybe it's a
desire to live dangerously - some sort of James Dean
syndrome.

I further have to confess in my early days of coding I
had used the double with on occasion - but I now find
this to be living far to dangerously, I must be
getting conservative in my old age.
Comment by Alister Christie [http://codegearguru.com] on June 27, 03:16

If With is Considered Harmful, What About Double With? 

Are there not performance advantages of using 
WITH...? I remember in Delphi 7 we had code that 
looped through a couple thousands of records and 
using a WITH whilst setting 30+ properties within 
that loop sped things up quite a bit. Infact most of 
that app was "WITH"ed...

The VB idea is cool and would've liked it in Delphi. 
Altho merely for the debugger feedback.
Comment by Roy on June 27, 11:57


Post Your Comment

Click here for posting your feedback to this blog.

There are currently 0 pending (unapproved) messages.