views:

145

answers:

7

Hi,

I'm wondering what / if any consequences there are in having unused functions in code?

If you hunt down and remove all unused functions and variables would there be any percievable improvement in performance?

Or is it just good practice to remove unused functions and variables would?

+1  A: 

Just a good practice. Nearly every compiler/linker will skip non-used code when compiling with optimizations turned on.

BarsMonster
+10  A: 

Unused functions can't harm performance. They are making the job harder for guys who are maintaining the code. Modern IDE's keep track of unused functions/methods and variables. If it's not a case with the technology that you are speaking about maintainers will have to deal with unused code thinking it's necessary.

Boris Pavlović
+1 code maintainability should be programmer job #1.
kenny
Although I agree on the maintaining part, the "can't harm performance" is in general not true. It is valid statement *most* of the time, but can easily be wrong for lots of constellations.
Eiko
+4  A: 

Depending on your compiler / linker, it may have no cost at all (and even be removed automatically), or give a small penalty because the code is bigger and gives cache misses. But I'd expect it too be very minor difference.

Edit: Removal cannot be done automatically when there are chances that other code will call it, i.e. library code or other binary that can later be reused. It is also language dependent - if you write JavaScript, everything will get loaded and probably parsed, so this will make a much bigger penalty than in compiled languages.

Eiko
+3  A: 

In most languages unused functions will not have any measurable performance impact on execution. Unused functions will affect the code/binary size. In Javascript this affects the download time and some parsing time.

Unused variables might affect performance a little bit, since they do some memory allocations. But the overhead of some unused variable here and there is probably not measurable either.

The big benefit of removing unused code is better control during development. If you do a change you don't need to go through lots of unused code to check if it might be effected.

Albin Sunnanbo
Cannot agree with you - bigger binary = more L1 cache misses = lower performance. But the idea is that most of compillers skip dead code automatically.
BarsMonster
A: 

They will increase the compile time, but the final binary (or library) will not increase, because all unused symbols should be striped.

As already mentioned, there are no run-time penalty.

VJo
+2  A: 

there is a security issue: if an attacker can control execution of your application (buffer overflow, crosssite scripting etc.), code fragments in memory will make it easier for him to achieve something significant (especially true if code fragments access privileged resources such as registry keys and files).

Jimmy
+1 For mentioning possible security risk as opposed to performance hit
El Ronnoco
A: 

OCaml and Haskell warn you of unused functions/variables on the assumption that if you defined them it must have been for a reason, and not using them may indicate a typo somewhere else in the code (e.g. calling a similarly named function instead). For the benefit of this additional help, I try to avoid or at least comment out things that I don't use.

A good compiler will simply optimize away unused code, so there is no penalty at runtime.

Gaius