This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Yes and no.
Assuming there are appropriate methods in your reservation object and its checkIn field, the latter taking a String as a parameter, and both returning booleans, then that code will work. But it won't return false.
There are two ways to improve it.
Replace the & operator by &&. It is usual to use && for boolean AND-ing. Remember the right-hand operation will only be executed if the left-hand operation returns true.
Get rid of the if. That improves the style.
You would then writeRemember that a "return" terminates your method, so you cannot put code after a "return" statement. Note you have no need to write "return false;" anywhere.
Sebastian Janisch wrote:
Other than that it's fine, you could even get rid of the if statement by simply doing this:
Be careful here. This is not a direct substitution. In the first example, only true is returned, the next line (following the if) is executed if the expression is false. In the second example, the value of the condition is returned, regardless of whether it is true or false.
Perhaps I should have included more code...I try not to write too long of posts.
My real issue is that this method returns true in all cases or false in all cases, but never a combination. I think I have a logic error. Any suggestions would be appreciated. Thanks so much.
Joined: Feb 23, 2009
You want to use && instead of &.
Think about it. If you use AND it means both ends of the expression need to be true, if the first one is already false, there is no need to check the second condition, hence && makes more sense.
& on the other hand will check the second expression no matter if the first was already false. This is only useful if your method call causes any side effects that you wish to cause no matter of the expression is true or false.
Joined: Sep 03, 2009
Thanks Sebastian for clarifying that. Anyone know why this method returns either all true or all false?
Joined: Oct 13, 2005
You are only ever checking the first true. You start off with a list of reservations, go through them, find something out about them, return from the method . . .
The first true will return, and that terminates the method. If you hunt for our JavaRanch style guide, which can be found from our title page by clicking "more" it says that using return in the middle of a method can be confusing. That is why. You could reduce your boolean expression to a single statementIf you can actually read that statement!
Can you make a boolean or List<Boolean> which you can then return with all the reservations' date-checking in?
I did say earlier that you can't have anything after a "return", which is what Henry meant about not being able to test anything else afterwards. Maybe not a good idea to have side-effects in a boolean expression.
Why don't you have a method in the Reservation class to check whether a date is within the requisite range?
What will happen if you create a Reservation for 14th September to 16th September and pass check-in date 13th September to it? Check-in is before the termination date of your Reservation . . .
String check out >= String check in. OR: not earlier than check in.
String check in <= date check out, OR: not later that check out date.
String check out >= date check in, OR: not earlier than check in date.
Then maybe you can reduce it to three tests.
Joined: Sep 03, 2009
Hey thanks. I'm still working on it, but I'm going to chew an each of your suggestions. I appreciate you helping me think this through.