• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

Question on if I should use a static method

 
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:Also, probably it is time to think, what happens and how you handle situations if user is not interested in entering Y, y, N or n, but instead, he is interested just hit "return" or anything else, for instance "4".


I am only going to use this method when I want to force the user to answer y or n. If they enter anything else I will keep asking them to enter y or n. For example if I want them to tell me if it is the correct player. It either is or isn't. There is no other acceptable answer.
 
Marshal
Posts: 7867
547
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kendall Ponder wrote:Is this the simplification you were referring to?

Yes exactly what I meant, this part looks the way simpler now.
 
Liutauras Vilda
Marshal
Posts: 7867
547
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This code above now could be simplified even more. Second method now looks redundant also, since you could use one of the loops: "ask user for input (at least once), until you get what you expect" ? What do you think?
 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:
This code above now could be simplified even more. Second method now looks redundant also, since you could use one of the loops: "ask user for input (at least once), until you get what you expect" ? What do you think?


I don't follow you on this one. I have to check for correct input and my understanding of clean code is the check should be in a separate method. Using a While loop instead of a do-While loop allows me to use recursion. I don't think it would be any simpler to use a do-while loop but if you show me what you mean I may change my mind. Thanks!
 
Liutauras Vilda
Marshal
Posts: 7867
547
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kendall Ponder wrote:Using a While loop instead of a do-While loop allows me to use recursion. I don't think it would be any simpler to use a do-while loop


Well, you're not using while loop in there, just "if" statement and recursion call if input not the one you expected - the latter is dangerous. Recursion idea suppose to be "divide and conquer". There is no base case, where recursion call would end up at some point, so it indicates me a wrong usage of it. Actually in practice recursion not really used as it can lead to a memory overflow problems.

Think what would happen in your case if user would enter lets say "4" instead of "Y or N" 4 billion times, maybe more?

I think the way simpler and appropriate way would be to use do-while loop over here. And personally for myself looks the way simpler and clearer approach.
 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:

Kendall Ponder wrote:Using a While loop instead of a do-While loop allows me to use recursion. I don't think it would be any simpler to use a do-while loop


Well, you're not using while loop in there, just "if" statement and recursion call if input not the one you expected - the latter is dangerous. Recursion idea suppose to be "divide and conquer". There is no base case, where recursion call would end up at some point, so it indicates me a wrong usage of it. Actually in practice recursion not really used as it can lead to a memory overflow problems.

Think what would happen in your case if user would enter lets say "4" instead of "Y or N" 4 billion times, maybe more?

I think the way simpler and appropriate way would be to use do-while loop over here. And personally for myself looks the way simpler and clearer approach.


I can change it if recursion is considered bad practice, but my understanding of clean code would still require the userEnteredInvalidChoice() method to control the loop. I do have trouble envisioning someone on their PC holding 4 down long enough to get a billion 4's. It would probably take several years.
 
Marshal
Posts: 26132
77
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's more likely to occur with recursion if you don't control it properly is a stack overflow, not memory overflow. Each time you call a recursive method another entry is added to the stack, and that entry isn't removed from the stack until you return from the recursive method.

And the size of the stack isn't measured in billions. It's more like hundreds. So your user with their thumb on the "4" key could cause a stack overflow with no problem.
 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to both of you. I will restrict recursion to "divide and conquer" as Liutauras called it. I will rewrite the method as just a loop.
 
Liutauras Vilda
Marshal
Posts: 7867
547
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:is a stack overflow, not memory overflow

With all respect Paul, agree and disagree. But still, stack tended to be a fixed in terms of allocated memory for the stack, so, it does run out of this allocated memory. So, I just called memory overflow.
Agree, likely my post wasn't clear and fully correct, I did mean stack overflow, tried to be more imaginative, it seems it lead to misleading post.
 
Marshal
Posts: 71084
292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know it is a very minor point, but I think Paul C is correct.
 
Liutauras Vilda
Marshal
Posts: 7867
547
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I know it is a very minor point, but I think Paul C is correct.

After I weighted again, what Paul said and myself - indeed, Paul is correct.

Kendall Ponder wrote:Thanks to both of you. I will restrict recursion to "divide and conquer" as Liutauras called it. I will rewrite the method as just a loop.

You're welcome. A bit more details, how to think about right use of recursion.

A recursive function normally has two parts:
Stopping condition: processing the case where the recursive function does not call itself.
Recursive step: processing the case where the recursive function calls itself.
The recursive call must have different parameters than those of the calling function because otherwise the program gets into an infinite loop.

And that bolded part probably is the most important, so it is exactly what would happen in your case, it would be an infinite loop if user would refuse to enter what you expect, and later, it would happen exactly what Paul correctly and in details explained about stack overflow.

And in addition: recursion is called "divide and conquer" technique, because each time you step down into recursion, you suppose to solve smaller problem than you had before you stepped into recursive call. And once you divide problem enough to a smaller sub problems, you reach the "base case" (stopping condition).
 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the refactored code per your advice. I would like some feedback on how I implemented a menu I had to pass data to, but I'm going to put it on a new post so if someone else is searching for information on the same question they will find it. I have received lots of good advice on this thread which people won't find based on the original question "Question on if I should use a static method".

As always I am open to suggestions.
 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I posted my new question on this code at How to pass a parameter to an object created in an enum.. As always any feedback will be appreciated.
 
Liutauras Vilda
Marshal
Posts: 7867
547
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kendall Ponder wrote:Here is the refactored code


If were my code, what I'd do (I know it is a tinny things, but):
1. I'd invert logic of "userEnteredInvalidChoice()" method and would change the name to "enteredValidChoise()" (for me easier to read, but it is just for me maybe)
2. I'd use do-while loop and the code would look something like:
And i'm not sure if these methods suppose to be static as they are in your code now.
 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1. I'd invert logic of "userEnteredInvalidChoice()" method and would change the name to "enteredValidChoise()" (for me easier to read, but it is just for me maybe)


I think you may be right. I wasn't sure if it was better to use the ! or not. I often wish Java had a do-until so I didn't have to use the ! operator.

Liutauras Vilda wrote:
2. I'd use do-while loop and the code would look something like:

The reason I didn't do it this way was if the user enters invalid input no message is printed out telling them why they have to re-enter their choice. Using the while loop allows me to give them an error message inside the while loop. Using the While loop I have to have the scan.next and promptMessage print in two places which I don't like but I don't know how to get around it and still have the error message. That is why I used recursion the first time, but as you pointed out that has stability problems in this case.


And i'm not sure if these methods suppose to be static as they are in your code now.


I made them static because they are part of a utility class which I can use for many different programs. Thanks for the input!
 
Doe, a deer, a female deer. Ray, a pockeful of sun. Me, a name, I call my tiny ad ...
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic