• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method calls  RSS feed

 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm interested to know the difference between these two styles of doing things:

Suppose there is a game, with the methods usersTurn() and computersTurn()

Then the best way (most likely) is to have a loop:

for, or while loop
{
usersTurn()
computersTurn()
}

Alternatively, we can do it recusively by getting usersTurn() to invoke computersTurn() at the end of its method body, and vice versa for computersTurn().
So doing this, say we have 10,000 methods called (5,000 for usersTurn() and 5,000 for computersTurn()), then none of these 10,000 methods will actually complete until the last method completes.

So my question is, will having 10,000 methods still 'open' hinder performance over the simple loop? I guess it would, but I'm not sure how it works with the java stack and everything, thanks for any input.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Definitely use the loop. A recursive approach will lead to an java.lang.StackOverflowError.
I would use something like this:

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apart from performance and stack overflow, there is no reason to think about recursive calls here. The process flow (computersTurn come after userTurns) should be kept outside userTurns. And the less things a method does, the better.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!