• Post Reply Bookmark Topic Watch Topic
  • New Topic

programming best practices for warnings  RSS feed

 
Will Klem
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Being a rank newbie to programming, I want to get started on the right foot. Forming good habits now will be much easier than breaking bad habits later. Several of my programs have had "variable not used" warnings; however, they have both compiled properly and provided the desired (and correct) results. I am quite certain that ignoring the warnings because the program works is unprofessional. For example: if I initialize variables in main as placeholders for use in a method, I receive the warnings. Should I make an effort to resolve these warnings, or are there some warnings which should be ignored?

This code, which I posted in another thread, works properly and has "the value of local variable ... is not used" warnings in lines 22-26.


Your thoughts on this subject will be appreciated.

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The warnings are a sign that your code is flawed. You are wise to realize that just because something "works" doesn't mean that it is good. you should strive to find out what the warnings mean, how they point out the flaws in the code, and resolve them.

It's also quite likely that the conditions causing the warnings are "bugs waiting to happen" that can reveal themselves with future changes in the code. (Be aware that code has "lifetime". The problems you create today can haunt you for years to come.)

It's sort of like if a car were designed with flaws in steering such that the steering works now, but if a different set of tires were put on, well, maybe it'd stop working.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you declare a variable in one method it is not possible to use it in another method because its scope will be local to the method you declared it in.

Take for example your variables x1, x2, y1, y2 and result in main. You say they're placeholders, but what are they placeholders for? You're not using them, so they shouldn't be there. They're not breaking anything but they will likely cause confusion to you and whoever reads the code.

And just to be clear, those variables in main have no relationship to and have no effect on the parameters to the distance() method. They happen to have the same name, but they are different.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And just to add about warnings in general. The compiler does sometimes warn you about things that aren't actually a problem. In these situations you can suppress the warning with an annotation.

However you need to fully understand why the compiler is warning you about something before you can know if it is a problem or not. If you don't know whether something is a problem or not then it needs investigating
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!