• Post Reply Bookmark Topic Watch Topic
  • New Topic

unused variables/imports - should I care?  RSS feed

 
Juergen Unfried
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I just came across a project where Eclipse gives me 1000+ warnings that local variables and/or imports are never used.

It's probably no big issue since not all objects are in use at the same time but I was wondering if this causes extra memory and should be removed or if it is neglectable

Juergen
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36455
460
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Juergen,
Welcome to JavaRanch!

It doesn't create any performance/imact. You should care from a maintainability point of view though. Unused variables are misleading and make the code harder to understand/change/maintain.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

Unused imports have really no impact at all; still, as Jeanne says, nicely organized imports can make code easier to understand.

My statement about unused local variables is a little stronger, however. Quite often, an unused local variable warning is pointing out a bug: you've spelled something two different ways, or inadvertently changed the name of something half-way through a method. It's really worth checking those all out and cleaning them up -- you're almost guaranteed to improve the quality of your software.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Eclipse you can select a whole folder full of files and use Source/Organize Imports (Ctrl-Shift-O) and make all those import warnings go away in an instant. Makes me smile every time.
 
Juergen Unfried
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for all the information. For the imports I was pretty sure it doens't matter very much. Regarding the variables I was thinking it doesn't reduce the performance very much but somewhere the jvm has to store the information.

Personally I don't like any warnings in my programs, but since I am new in this company I thought it's better to get more information first

thank you all for the nice welcome AND the help

cya
Juergen
 
Tim LeMaster
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No one here can tell you the political impact of trying to clean up the code. Its pretty easy to do from a technology stand point. However my last job we used a framework developed by another in-house team. It had hundreds probably thousands of warnings and it generated code with lots of warnings too. I was filtering some many warnings in eclipse the warnings list became completely useless - but no one seemed to care. Fixing this is a political problem as you can't just go rewrite other people's code in most corporate environments.

I left because of that framework and various other reasons and I'm glad I did.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best way to fix errors is to simply have the tool hide them. One of the compilation options is to have unused imports as a warning, or to ignore them altogether. I always ignore them altogether.

I find in IBM's Rational Application Developer (IRAD), much of the computer generated code for EJBs comes in with unused imports. It's funny, watching IBM's tool generate code it doesn't like. lol

-Cameron McKenzie
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kameron McKenzie:
The best way to fix errors is to simply have the tool hide them.


Only that that doesn't actually fix the errors, it just hides them...
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36455
460
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kameron McKenzie:
The best way to fix errors is to simply have the tool hide them.

Until it bites you of course. My "favorite" was when our JSPs didn't work because the package referenced in an unused import didn't exist. And of course this only happens in certain environments...
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Completely unused local variables - those which are never written or read - should be removed.

But what about the case when a variable is written, but never read? Some compilers (I'm thinking of all languages here) call that an unused variable, but in fact it can be useful when using a debugger.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!