Delphi Dead Code Elimination

Delphi’s compiler tool chain may automatically eliminate some dead code, but with an older or “mature” product there are bound to be lots of units that are no longer used but are still present as part of a project or referenced indirectly by some other unit. Having a good gatekeeper on your team can help, as can these tools, but it can never replace a team that is concerned about keeping it lean.

For instance, I stumbled across code that was written to compensate for a database column not being a consistent length. That change management issue was addressed a long time ago, yet the code remained, running a very expensive query on Oracle for absolutely no benefit. Some people may not think that is a big deal, but multiply that by 1000 other such things in the main thread of an application and it can start to make Delphi programs look pretty sluggish.

If your team keeps units around because it ‘might’ need them, or older methods because not every call has been replaced, you might want to rethink that.  It’s easy to spend time maintaining code that may no longer be relevant.  Time means money, and there is no ROI if you are a code hoarder.  A VCS makes it easy to restore code in the event you ever need it, or disposed of it by accident.

3 Responses to “Delphi Dead Code Elimination”

  1. Kevin G. McCoy Says:

    I think you may have confused dead code with obsolete code.

    Dead code (the kind that can be eliminated by the Linker) are functions, procedures and methods that are no longer called by *anything*. If the linker sees no calls to that type of code, it leaves it out of the binaries / EXE.

    Obsolete code is still being called, but may no longer serve a useful purpose - and it contributes to bloat, potential slowdowns and bugs.

    You can let the linker do its job on the former, but you probably need a coverage analysis tool for the latter.

    Sometimes dead code can be accidentally added to the EXE, if a unit’s INITIALIZATION block calls some obsolete code. That can pull in all sorts of unwanted junk. The junk may or may not cause problems, but it surely contributes to bloat.

  2. Larry Hengen Says:

    @Kevin,

    I understand the difference, but I lumped the two together. The idea I was trying to put across is that you should not rely on the compiler toolchain to eliminate dead or obsolete code. The linker of course only removes the former, while both can be removed if you/your team focus on keeping the application as lean as possible and therefore your code as neat and organized as possible.

    Thanks for the comments. Always nice to know someone actually reads my posts ;-)

  3. Fouad Loucif Says:

    I always read your posts, and I enjoy them, thank you for your contributions to the Delphi Comunity. Pease from Algeria.

Leave a Reply