Friday, August 06, 2004

The uses clause considered harmful

(The headline plays with Edsger Dijkstra's famous article "Go To Statement Considered Harmful") Because the way unit references in the uses clause work in Delphi, you could occasionally get weird and even buggy effects because of changes made in one of the units you use. Note that in general, Delphi handles modularity and code imports much cleaner than most other programming languages. Still there is room for some improvement.

My suggestion is that the Delphi compiler be improved to emit a new Warning for potential name conflicts when the same identifier is imported from two or more units. The goal is to catch unintended changes in semantics after changes have been made to used units.

If I have two units, A & B, both of which export the identifier foo and then in another unit I write:

uses A, B; 
// ...
begin
foo;
end.

Here the reference to foo is without a unit prefix. Currently I would get no warning. My suggestion is to generate a warning, something like this:


"Warning: Potential name conflict: foo declared in unit A and unit B."

To resolve the warning, the programmer would prefix foo with the intended unit name:

begin   
A.foo;
end.

This would then generate no warning. Technically, the reference to 'foo' isn't really ambiguous. That's why you shouldn't get a compile error. But like most warnings, this one should be there to catch potential human error or confusion. You have two possible sceniarios:



  1. The user meant to use A.foo, but because of the unit-ordering he is actually using B.foo.

  2. Last week, foo was only defined in unit A and the program was compiling working as intended. This week a new foo has been added to B. The program still compiles, but stops working correctly at run-time. This is something that can be really hard to debug and track down - it has happened to me...

#2 is the most dangerous and where the warning would be most useful.

7 comments:

Anonymous said...

I love the blog article title, and I love the idea even more. I don't think this affects me all that much, but it would be nice to know that for sure. :)

Dan Miser

Anonymous said...

Nice idea... I really hate when the compiler halts with a error in a "myBitmap := TBitmap.Create" because I have Windows after Graphics in my uses clause.

Paulo França said...

Well mentioned the TBitmap issue. In my earlier days in Delphi, I spent hours until I found the source of the conflict between the units Graphics and Windows.

The idea of getting compiler warnings is so good that should be posted to Delphi QC.

Paulo França said...

Sorry for posting again, this turn for another subject...

It's just to say it's very difficult to read your blog's articles because of the dark blue background. I always got to select the whole page (CTRL-A) in order to make the text readable. Ok, I am indeed miope, but I believe that chosing another color combination would make your (excellent) blog more miope-friendly. \\:^)

I should have sent you an email for this, but I couldn't find it anywhere. After reading this, please feel free to remove this post from your blog.

Jim McKeeth said...

A third scenario is that the developer meant to use B.foo, and didn't realize there was an A.foo. This warning would help developers to better understand their code and the libraries they are using.

I really think this is a great idea. If you add this to QC then post a link here and we can all vote it up.

Hallvard Vassbotn said...

OK, I've QC'ed it now: #8761

As for the colors of the site; sorry about that. I recommend using a decent blog reader that understand RSS and/or Atom. Then you should be able to see it with out my classic Delphi colors. I have already updated the colors once to try and make it more readable...

Anonymous said...

Hi Hallvard,

goo d of you to re-raise this issue, but we already QC'ed it last year, remember? QC1615, with your very own comments added in the main body of the report!
:-)

Kristofer Skaug



Copyright © 2004-2007 by Hallvard Vassbotn