views:

385

answers:

4

Every time I add a new form to my project, it drops a big glop of boilerplate in the uses clause.

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

Seriously, who uses the Variants unit on anything resembling a regular basis? I generally end up removing Windows, Messages, Variants, Graphics and Dialogs and never missing them.

That's gotta be coming out of a template file somewhere, but I can't seem to find it. Does anyone know where I can find the template and edit it? I'm using D2009, in case it's changed recently.

+9  A: 

The according resource is located in $(BDS)\bin\delphivclide*.bpl and named "VCLIDECMD"; you can extract, edit and update it with the resource editor of your choice (I recommend the one included in Pelles C).

For C++Builder users, the template file is "CPPVCLIDECMD" in bcbvclide*.bpl.

If you are using a localized version of RAD Studio, look at the appropriate language resource files (*.DE, *.FR or *.JA).

Moritz Beutel
+4  A: 

You are not saving anything by removing Windows and Messages. Graphics and Dialogs may get added back in based on what components you drop on the form, and they are fairly useful to reference anyway. Doubt you are saving much by removing them. Feel free to remove variants if you are not using them (which I agree is pretty common unless doing COM or DB development).

I guess it all depends on your objective in cleaning the uses clause. Variants is really the only one that may have an impact on your application.

As far as changing the default template, I believe it is in a package that says if you are descending from a TForm then you get those. You would most likely need to modify a .PAS file and rebuild the VCL packages. A lot of work for very little gain.

Jim McKeeth
I don't think Mason Wheeler wants to reduce executable size, he just wants cleaner autogenerated code.
The_Fox
Fox: Yes, exactly. Jim: If Moritz's method works, it's not actually all that much work, and it'll only have to be done once.
Mason Wheeler
+4  A: 

I usually use "Uses cleaner" which is coming with cnPack after finishing the project, it will give you a list for all unused units in your project, because usually when finishing project you may have larger unused units than you mentioned.

Mohammed Nasman
The is a risk with this method. Such a tool can never analyze if the initialization or finalization sections of a used unit need to be executed or not. Removing units from the uses could result in not running initialization and finalization sections and therefore result in bugs.
Lars Truijens
Interesting point, but IMO if you're relying on initialization of a unit that's never actually used anywhere, your code needs refactoring.
Mason Wheeler
@Mason: Not necessarily if it is a unit used to automatically install some utility like FastMM4 or a patch like Andreas’ VclFixPack unit. We used that trick to dynamically replace calls to InitializeCriticalSection by InitializeCriticalSectionEx when running on Vista/Windows2008.Granted, it's more likely to happen in the dpr than in a Form unit.
François
@Lars CnPack has an option to skip units with initialization
mjustin
A: 

Actually, that bothered me aswell.

But then i checked the executable size compiled with the default units and then checked it without them (Messages, Variants, Grahpics) and the size was not much smaller.

Dialogs.pas adds some kilobytes, but I frequently use delphi VCL dialogs over Win API ones.

So, I'd say not bother too much with them

Aldo