Hasan Al-Baghdadi
Hasan Al-Baghdadi

Reputation: 442

What are the drawbacks of having unused classes left in a c++ project?

Let's say I had a class called foo which performed some functionality that I found was better done by library bar.

Assuming I deleted all references and includes of foo, could it still cause inefficiency in run-time/compile-time and would I have to remove the code from the project, is what I did enough or is this compiler dependent.

Upvotes: 3

Views: 526

Answers (3)

Jesper Juhl
Jesper Juhl

Reputation: 31459

If you still compile the file containing the unused code then yes, it will impact your compile time.

If you are building a library then the type will also be included, even if unused, so it will impact your library size.

Run time impact will be minimal, but not zero - the dynamic linker will still need to spend a few nanoseconds on your type when loading your library.

If you are building an executable, any decent compiler/linker should be able to eliminate the unused code, so the only impact is on build time.

Remove dead code. Let your version control system (git, cvs, whatever) remember it for posterity, but get it out of your project.

Upvotes: 3

curiouscupcake
curiouscupcake

Reputation: 1277

It depends on the linker.

If programs in the build chain (compiler, linker, etc) can detect that any symbol is not being used, it can safely remove them.

Moreover, if the class is unused, then there is no way to tell it has been removed - so any proper linker will remove it anyway.

Upvotes: 1

SergeyA
SergeyA

Reputation: 62603

Yes, you should remove unused code from the project. The reason is not so much efficiency or code size (with any modern linker there will be no impact), but a phenomenon called 'Software Rot'.

If you have unused but available code inside your project, sooner or later someone will try to use it again, but since the code was not available and not maintained, the code will likely be outdated and might cause serious issues in production. Further reading: https://en.wikipedia.org/wiki/Software_rot

And it is not an empty threat - a similar thing once eradicated the whole trading firm as they lost all their money due to a software bug related to software rot: https://dougseven.com/2014/04/17/knightmare-a-devops-cautionary-tale/

Upvotes: 2

Related Questions