Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what are the consequences of having unused functions

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?

like image 825
dubbeat Avatar asked Oct 18 '10 11:10

dubbeat


People also ask

Why are unused variables bad?

Unused variables are a waste of space in the source; a decent compiler won't create them in the object file. Unused parameters when the functions have to meet an externally imposed interface are a different problem; they can't be avoided as easily because to remove them would be to change the interface.

Why should you remove unused code?

We come across examples of unused code, often not realizing it. Such unused code convolutes the codebase - especially true to developers reading the code for the first time. They have to exert unnecessary cognitive load when trying to understand the code's functionality.

Do unused variables affect performance Javascript?

Unused local variables make code hard to read and understand. Any computation used to initialize an unused variable is wasted, which may lead to performance problems.

Does compiler remove unused functions?

Feedback from the linker to the compiler can remove unused function code, minimizing code size.


2 Answers

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.

like image 180
Boris Pavlović Avatar answered Sep 21 '22 09:09

Boris Pavlović


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.

like image 24
Eiko Avatar answered Sep 23 '22 09:09

Eiko