• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

programming best practices for warnings

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Sheriff
Posts: 67663
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
The moustache of a titan! The ad of a flea:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic