With or Using

David C. Ullrich ullrich at math.okstate.edu
Fri Apr 20 12:00:26 EDT 2001


On Fri, 20 Apr 2001 01:44:04 GMT, "Gary Walker" <borealis3 at home.com>
wrote:

>I don't necessarily think I'm one of the five experts, but I have used
>Delphi since 1.0.
>
>The with keyword never, ever struck me one which was designed to save
>keystrokes. Rather, it seemed to add clarity and succinctness to the code.
>I'm a team leader of a 6 person software development effort. If members of
>my team asked me my preference, I'd say use it.

I've seen many people say that "with" is a bad thing in Delphi. Their
point is that it makes it _harder_ to understand the code. The problem
being that we're dealing with these huge components with many
properties; together with the fact that AMethod is the same as
Self.AMethod (inside an object method) it can cause confusion. Like

procedure TForm1.Something;
begin
with Button1 do
  begin
    Canvas.Whatever;
  end;
end;

People write that and think that it's going to draw Whatever
on the button's Canvas, which it would if the button had a
Canvas. Instead it draws something on the form's Canvas.
If you say Button1.Canvas.Whatever instead the compiler
knows what you meant and gives you an error.

That's the argument. I used to think it was silly - you just have to
know what has what methods and there's no problem. I don't
think it's so silly as I used to.

>Consider this:
>
>Recipe for Lemonade
>
>Add 8 cups water to a large pitcher
>Add 1/2 cup lemon juice to a large pitcher
>Add 2 cups sugar to a large pitcher
>Stir the ingredients that are in the large pitcher
>
>Now consider:
>
>To a large pitcher add:
>    8 cups water
>    1/2 cup lemon juice
>    2 cups sugar
>    and stir.
>
>Did the second one confuse you? Did you forget the large pitcher?? Of course
>not. It's much easier to understand. So it is with with.

Well actually the two snippets are not equivalent, but never mind 
that. No, it didn't confuse me. But this has no bearing on the
objection to "with" in Delphi because it's such a simple situation
(and also the problem that inside a "with" inside an object
method an unqualified reference can refer either to the
thing we're using with with or to Self doesn't arise here.)

>I believe Python allows a similar idea with the FROM keyword. If you specify
>the object that you're importing from a specific module, aren't you allowed
>to reference it without qualifying the module?
>
>As in:
>
>from SimpleHTTPServer import SimpleHTTPRequestHandler
>
>Doesn't this allow you to now reference SimpleHTTPRequestHandler without
>specifying SimpleHTTPServer?? Or am I missing something...??

The difference is this: Suppose that SimpleHTTPServer does not
_contain_ a SimpleHTTPRequestHandler class. Probably you'd
agree that the import statement should then give a compiler error,
right? Or do you feel that if SimpleHTTPServer does not contain
a SimpleHTTPRequestHandler class but there is already such a
class in the enclosing scope then the import statement should
"succeed"?

>>It's in Delphi all right. Now find five Delphi experts and ask them
>>whether they think it's a good idea.
>>
>>>> It saves a small amount of typing (and not very much) at the expense of
>>>> a lack of clarity and the addition of ambiguity.
>>>
>>>I thought with Python's block indenting it would be pretty clear what
>>>was being referred to.
>>>
>
>
>




More information about the Python-list mailing list