klefevre
klefevre

Reputation: 8735

Why are unused variables bad?

I would like to know why an unused variable is bad.

Is it because the compiler would create a bigger binary? If yes, is there a tool/script which can add an unused keyword or something like that?

Upvotes: 9

Views: 5372

Answers (4)

Himanshu Chhikara
Himanshu Chhikara

Reputation: 51

at somepoint memory is limited. Memory stack get full and overloaded when there are unused variables in the code. Compiler will take more time to run the code. So as for the sake of memory consumption and memory optimisation unused variables are bad.

Upvotes: 0

kyrylo
kyrylo

Reputation: 1738

In my humble opinion, unused variables complicate readability of your code. No matter what language you are using.

Upvotes: 10

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215457

Because...

Perfection is reached not when there’s nothing left to add, but when there’s nothing left to remove.

Upvotes: 12

Joey
Joey

Reputation: 354734

The compiler gives you warnings to hint on things that could potentially be a problem or unintentional.

Unused variables will be optimized away most likely. But maybe you intended to do something with them – and in that case the compiler helpfully notes that you may have done something you didn't want.

What's the use in a variable you declare but neither read from nor write to?

Upvotes: 21

Related Questions