delphiidedelphi-xe2

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.


Solution

  • There is no way to fully automate this.

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

    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.