-3

I am very much aware that 'goto' is a dirty word in the lexicon of most professional followers of structured programming. So I would ask how the following use of a 'goto' could be replaced.

I'm writing a bridge bidding app. I ask the user "Who dealt". If he did, no problem. If the player on his right dealt (East), then the user must be asked what that player bid. If the player opposite dealt (partner), the user must be asked what the player opposite bid and then what East. If the player on the user's left dealt (West), the user must be asked, in turn, what all those three players bid.

I have an activity "What bid" that displays a screen and is passed either "West", "your partner" or "East" depending on who bid. That activity is called using startActivityForResult and returns the bid that was made. Now here's the thing. I reckon this is the most efficient way to do this:

if (whodealt == "West") {continue}
else if (whodealt == "North") {goto northbid;}
else if (whodealt == "East") {goto eastbid;}
westbid:
 startActivityForResult() etc. passing "West";
 process result (e.g. store bid)
northbid:
  startActivityForResult() etc. passing "your partner";
  process result (e.g. store bid)
eastbid:
  startActivityForResult() etc. passing "East";
  process result (e.g. store bid)

Obviously, I've simplified everything. But isn't the alternative non-goto structure below a bit cumbersome, such as

if (whodealt == "West") {
  startActivityForResult() etc. passing "West";
  process result (e.g. store bid)
  startActivityForResult() etc. passing "your partner";
  process result (e.g. store bid)
  startActivityForResult() etc. passing "East";
  process result (e.g. store bid) }

else if (whodealt == "North") {
  startActivityForResult() etc. passing "your partner";
  process result (e.g. store bid)
  startActivityForResult() etc. passing "East";
  process result (e.g. store bid) }

else if (whodealt == "East") {
  startActivityForResult() etc. passing "East";
  process result (e.g. store bid) }

What do people think about this?

Boken
  • 4,825
  • 10
  • 32
  • 42
John of York
  • 93
  • 2
  • 12

3 Answers3

2

What you are describing is essentially a switch statement with fallthroughs:

switch(whodealt) {
    case WEST:
        // startActivityForResult() etc. passing WEST;
        // process result
        // fall through

    case NORTH:
        // startActivityForResult() etc. passing NORTH;
        // process result
        // fall through

    case EAST:
        // startActivityForResult() etc. passing EAST;
        // process result
        // fall through

    case SOUTH: // (the player dealt themselves)
        break;
    default:
        throw new IllegalArgumentException("Unrecognised Dealer");
}

Java has supported Strings in switch statements since Java 7, but I would recommend not using strings for this, and defining your own enum for the dealer: In this case there is only a finite domain of values that whodealt could inhabit, and it is much smaller than the domain of all Strings.

amnn
  • 3,657
  • 17
  • 23
  • Thank you, Amnn. So it's not that if "case WEST" is true, that it then jumps to the end of the switch statement, because I though that's what happened. It will execute the statements within case WEST then NORTH then EAST without you telling it to fall through? Is this correct? If not, how do you tell it to "fall through"? – John of York Nov 07 '15 at 16:04
  • That is correct, in a `switch` statement, fall through is what happens by default, and you have to explicitly put a `break` in (like I did in the `SOUTH` case) to get it to stop doing that. – amnn Nov 07 '15 at 16:15
  • Thanks you, Sir. Your answer now checked. – John of York Nov 07 '15 at 16:22
0

Java does not have a goto statement. goto is a reserved keyword in Java but it is not used by the language. See this question for a more complete answer.

Is there a goto statement in Java?

To answer your question "Is this a valid case for use of a 'goto' in Java" the answer has to be no since it is not possible in Java.

Community
  • 1
  • 1
bhspencer
  • 13,086
  • 5
  • 35
  • 44
0

If the player on his right dealt (East), then the user must be asked what that >player bid. If the player opposite dealt (partner), the user must be asked what >the player opposite bid and then what East. If the player on the user's left >dealt (West), the user must be asked, in turn, what all those three players bid.

Seems to be a good situation for a chain of responsibility especially if you like to implement more logical code. Otherwise I would suggest a switch case statement like @amnn.

YAUN
  • 33
  • 4
  • "A chain of responsibility". That sounds interesting. Can you point me to documentation on that because although I can guess what it means of course, I have no idea how to implement such a thing in Java. – John of York Nov 07 '15 at 16:21
  • See: [link](https://dzone.com/articles/design-patterns-uncovered-chain-of-responsibility) I don't know how far you'd like to get with your app but it should be possible to implement different handlers for offline and online gaming – YAUN Nov 07 '15 at 16:27
  • You have to think about if it's worth the effort. A good solution for you're problem is mentioned above by @amnn – YAUN Nov 07 '15 at 16:39
  • Thanks. Yaun. Followed the link A bit too advanced for my purposes and experience right now, but have logged your suggestion and may well return to it when I have gained confidence. – John of York Nov 07 '15 at 16:44