Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

To use break statements or not to use break statements, that is the question

 
Lisa Beglaw
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The only part of this that isn't working the way I want is the '==null' part. If I try to rent a movie that ISN'T in my collection, I want the boolean value to return as false and I want a message displayed that says the movie isn't in the collecion.

With the break statements:
The null method doesn't ever get used, if I try to return a movie that doesn't exist I get the false return plus the message "Selected movie is not currently rented out, cannot return."

Without the break statments:
If I try to return a movie that isn't in the collection I get the false return plus both messages, "Selected movie is not currently rented out, cannot return." and "Selected movie is not in the collection."

How can I break this up so they both get the return of false, but each one has a differnt message?
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firstly, the comparison with null should be the first of the three, because if it is true (i.e. the object is null), either of the two other statements would bomb out with a NullPointerException. So currently, that comparison is never going to execute.

Secondly, with the code as shown you can never get both message for the same movie, because only one of the three branches ever gets executed. You can only get them in succession for two different movie titles if the first branch is taken (because that is the only one that does not terminate the iteration).

It is not totally clear to me what the code is supposed to do, so I can't comment on the iteration logic. E.g. how do you know which movie is to be returned given that the 'title' parameter is not used?
[ August 07, 2005: Message edited by: Ulf Dittmer ]
 
Dale Seng
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm guessing at what you want to do, but I think I have a better way to proceed. You might create a map object and use that, instead of looping to find what you need.

 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also notice that the method as written *always* will return false (even if you drop the break statements).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic