Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 434
Dmitry Avatar asked Jul 22 '13 19:07

Dmitry


4 Answers

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.

like image 136
Kenneth Cochran Avatar answered Sep 21 '22 14:09

Kenneth Cochran


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.

like image 30
norgepaul Avatar answered Sep 24 '22 14:09

norgepaul


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

like image 4
House of Dexter Avatar answered Sep 23 '22 14:09

House of Dexter


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.

like image 4
Nicholas Ring Avatar answered Sep 22 '22 14:09

Nicholas Ring