Dmitry
Dmitry

Reputation: 14622

How to remove unused units from all source files on Delphi XE2?

How to automatically remove unused units from uses section on all source files of the project on Delphi XE2?

P.S. IDE will work much faster after that.

Upvotes: 17

Views: 10575

Answers (5)

norgepaul
norgepaul

Reputation: 6053

We have a utility called the Delphi Unit Dependency Scanner (DUDS). It scans your entire project and builds a fully searchable tree of the units and dependencies. It can be very useful in finding unused units.

The application is Freeware and you can find it here.

Disclaimer-I am the author.

Upvotes: 13

Miguel Guzmán
Miguel Guzmán

Reputation: 39

Use reFind.exe utility provided since Delphi XE, use this command

reFind *.pas /X:unuse.txt

And unuse.txt is a text file with something like this:

#unuse Unit1
#unuse Unit2
#unuse Unit3

And that's it. It will remove the units in the uses clause taking care if the unit is the last one used and there is a ; after the unit.

Upvotes: 0

Kenneth Cochran
Kenneth Cochran

Reputation: 12084

There is no way to fully automate this.

There are a few tools I'm aware of that take a wizard approach:

  • CnPack Uses Units Cleaner
  • Peganza's Pascal Analyzer (and it's sidekick icarus).
  • The Lazarus IDE has a "Unused Units" dialog in it's CodeTools package.

Peganza's tools simply show a report. CnPack will prompt to remove the unused units for you but you must confirm. Lazarus presents you with a list of unit's it thinks are unused and gives you the choice of removing some or all of them.

Why isn't it automated?

Because this is static analysis. Even the most sophisticated tools cannot determine with 100% certainty whether a specific line of code will be used at runtime let alone an entire unit. These tools have implemented their own parsers to accomplish this feat but they aren't fool proof.

In any case the main benefit of cleaning up the uses clause is removing visual clutter from both the source itself and the code completion feature. Yes there is some performance gained during compiling and certain IDE background operations will speed up slightly but I think you'll be disappointed if you think the IDE will miraculously speed up.

You'll see better IDE and compiler performance by:

  1. Breaking your projects into smaller pieces that can be worked on independently.
  2. Eliminating duplicate code.
  3. Disabling unneeded IDE packages.

I'm certainly not trying to dissuade you from removing unused unit references. As I said it will help unclutter your source. Just make sure you're doing it for the right reasons.

Upvotes: 17

Nicholas Ring
Nicholas Ring

Reputation: 1016

CnPack has "Use Cleaner..." option that I have used unit by unit basis without a problem. It also has the ability to do the entire project - which I haven't tried due to the size of the project.

Upvotes: 4

House of Dexter
House of Dexter

Reputation: 386

Don't think I would want a tool that would automatically rip out unnecessary units in the Uses section...

but there are tools out there to identify them...look at Icarus...freeware that you can get at http://www.peganza.com/downloads.htm

Upvotes: 4

Related Questions