Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Recursively calling a method

 
Ranch Hand
Posts: 176
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it a good approach to recursively call a method from its body ??

 
Saloon Keeper
Posts: 14327
321
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Java, it's a bad idea to write recursive methods that don't reduce the problem. In your application, the recursive steps don't guarantee that the state of the program is changed, meaning that the program may throw a StackOverflowError before it terminates.

Now, there is such a thing as tail call optimization, which avoids the creation of a stack frame when the recursive call is performed right before the method returns, but Java doesn't guarantee that this actually happens.

So no, don't use recursion in this way.

Moving to Java in General.
 
Bartender
Posts: 2908
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any approach that simplifies your problem or makes a good readable code is a good approach.
There are folks who think more naturally in recursion as opposed to loops. Having said that, its just a matter of style of coding.
The basic rule about recursion is that there is some sort of "terminating" condition that should be present else your code would run indefinitely. The code you posted has a simple condition to stop over a few random checks. I feel that a while loop would suffice your case as:

 
Marshal
Posts: 76491
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure you want tryAgain to default to false?
 
Stephan van Hulst
Saloon Keeper
Posts: 14327
321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tend to avoid the do-while loop. I prefer something like this:
 
salvin francis
Bartender
Posts: 2908
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Are you sure you want tryAgain to default to false?


Yup Programmer has to explicitly write code to set it to true for another loop. by default, it wont loop.
 
Shubham Semwal
Ranch Hand
Posts: 176
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay got it. Thanks for the tips

"A method recursion is appropriate when it makes some changes in the method body each time it runs."
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shubham Semwal wrote:Is it a good approach to recursively call a method from its body ??


I totally agree with salvin's opening remark ("Any approach that simplifies your problem or makes a good readable code is a good approach."), but I'd actually go slightly further than Stephan:

    In Java, it's generally a bad idea to write recursive methods that don't reduce the problem exponentially.

It's sometimes called the "divide and conquer" rule because many elegant sort algorithms, for example, use recursion to "halve" the problem with each call.

And the reason is because of the StackOverflowError he mentioned. Depending on resources, this can happen with as few as a few dozen recursive calls, so if your program doesn't reduce the problem exponentially, it could throw that error very quickly. On the other hand, a divide-and-conquer sort can handle a billion records with a "depth" limit as low as 30, which is way lower than you're ever likely to run into in real life.

Unfortunately it's measured in size, not depth; so it can be tough to work out exactly when your program will break without testing it; but in general you will be good for many hundreds of calls.

But that can happen very quickly if your program only reduces the problem linearly.

HIH

Winston
 
Bartender
Posts: 5078
189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not like this formulation

Winston Gutkowski wrote:In Java, it's generally a bad idea to write recursive methods that don't reduce the problem exponentially.


First of all, what is 'reducing a problem'? In general, the problem remains
the same, it is just that we get closer, in some definite sense, to a base case.

But apart from that: there are many cases in which recursion does not
'reduce the problem', let alone exponentially, in any way. In such cases
some external stopping conditions must be set (like maximum depth,
time constraints, perhaps memory usage).

A simple example would be a chess playing program,
that uses recursion to go one ply deeper. The problem does, however,
not get reduced (unless you get into a forced mate situation).
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:I do not like this formulation...


Yes, I agree it's incomplete; but I'm not sure that a chess game is a particularly good example of recursion. What does it buy you?

But you're absolutely right. There certainly are applications that lend themselves to recursion where "reduction" - exponential or otherwise - simply doesn't apply. An 'Undo' function for an editor springs to mind.

But I hope we can agree that a recursive solution (at least in Java) needs to have a strict - and predictable - upper bound to the depth of calls it makes; otherwise Sod's Law dictates that it'll crash at some point. And exponential reduction is one way to make sure of that.

Winston
 
Piet Souris
Bartender
Posts: 5078
189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:
But I hope we can agree that a recursive solution (at least in Java) needs to have a strict - and predictable - upper bound to the depth of calls it makes; otherwise Sod's Law dictates that it'll crash at some point. And exponential reduction is one way to make sure of that.

Winston


I agree!
 
Shubham Semwal
Ranch Hand
Posts: 176
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very good discussion here. I want to make a chess game after I learn java.
 
Campbell Ritchie
Marshal
Posts: 76491
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shubham Semwal wrote:Very good discussion here. . . .

Thank you We try our hardest, and occasionally succeed.

You may find chess difficult. It will require a lot of learning about games before you can implement it.
 
Shubham Semwal
Ranch Hand
Posts: 176
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Shubham Semwal wrote:Very good discussion here. . . .

Thank you We try our hardest, and occasionally succeed.

You may find chess difficult. It will require a lot of learning about games before you can implement it.



Then I'll ask for easier games later somewhere in forum and eventually move to chess.
I want to be a good programmer within 6 months to get a job.
 
The only cure for that is hours of television radiation. And this tiny ad:
The trailboss has a kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic