• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can I ignore this notion in this case: The value of the local variable nextDate is not used

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As a studyproject I'm currently writing a class the allows me to get al fun dates (like when eastern is in a given year, what day a given date has, calculate the date of tomorrow).

While working on the following method:



I get a notion in my lovely IDE (eclipse) reminding me I'm not using nextDate ("The value of the local variable nextDate is not used")
But I feel I really do use nextDay here. So either I'm making a coding(style) mistake giving me this notion or I should just ignore this notion.

Any elaboration on this is really appreciated. Any other feedback (compliments/ critism) is also always welcome ;)

Stijn
 
Ranch Hand
Posts: 159
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

At line 3 you declare your String nextDate.
At line 9 you assign it with a value (which is the return value of the method).
Other than this, you don't use it. You don't use nextDate for comparison to another String, you don't use it by printing its value, and so on...

If you rewrite your method like this, than Eclipse will be happy.
 
Stijn Rensen
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah cheers! I didn't know it is allowed to write a return like that. Is this also good coding practice? Or is this adjustment purely to positively influence the mood of eclipse?
 
Stijn Rensen
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh wait, found the answer myself in the book. Guess I shouldn't read to fast in order to get to the next excersize more quickly.
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stijn Rensen wrote:Is this also good coding practice?

I'd suggest you to choose variable names more carefully: day, nextDay, nextDate it's rather confusing and error prone. You can easily misplace variable and it would take hours to find out what is wrong.

Stijn Rensen wrote:Or is this adjustment purely to positively influence the mood of eclipse?


Nothing is wrong with Ecplise, actually it says what it is. In any expression you never used this nextDate variable.
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, my solution would have been :



But then I like stepping through code with a debugger, and this format lets you stop and inspect the return value of the method very easily, whereas your first example doesn't directly.

 
Stijn Rensen
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:

Stijn Rensen wrote:Is this also good coding practice?

I'd suggest you to choose variable names more carefully: day, nextDay, nextDate it's rather confusing and error prone. You can easily misplace variable and it would take hours to find out what is wrong.



What would you suggest in this case? The goal of the method is to construct a string that will tell you the the next date of a given date.

Liutauras Vilda wrote:

Stijn Rensen wrote:Or is this adjustment purely to positively influence the mood of eclipse?


Nothing is wrong with Ecplise, actually it says what it is. In any expression you never used this nextDate variable.



I wasn't saying eclipse is wrong. I guess I was naive in thinking I could be humoristic... :/ ;)
 
Stijn Rensen
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stefan Evans wrote:Actually, my solution would have been :



But then I like stepping through code with a debugger, and this format lets you stop and inspect the return value of the method very easily, whereas your first example doesn't directly.



That has some merrit to it too, Especially for a noob like me.
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stijn Rensen wrote:What would you suggest in this case? The goal of the method is to construct a string that will tell you the the next date of a given date.


Well, when you see variables: nextDay, nextDate you should agree, that is rather confusing, no matter what the goal of the method is. If you saying that the goal of the method is to construct a string.. why not to rename "nextDate" > "nextDateString" or even "goal" (it would be better in this case), so it would be easier to distinct them from each other. The main idea from me was, do not choose variable names which looks very similar as it is very easy to use wrong one.
Well, you need some own experience of it by trying to find mistake, you'd know what I mean.
 
Ranch Hand
Posts: 62
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When it comes to returning values, putting it in a variable improves readability if the return statement is too long or unwieldy.
 
Let's get him boys! We'll make him read this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic