Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Objects Creating the MVP Storing Guesses

Why did he return isHit, at 8.21?

I know it returns boolean so we have to return something but why and where should we return, what is its purpose?

1 Answer

Brad Wagner
Brad Wagner
9,379 Points

I can see why this is confusing. At this point in time it appears the only purpose of the method 'applyGuess()' is to determine if we store the guess input from the user in the 'hits' String or the 'misses' String.

Later however he also uses it along with another method for the purpose of validation: to also determine whether or not what the user entered was a valid String and not a symbol, space, etc. by including a check (through another method also in the 'Game' class called 'normalizeGuess()' which will be included in 'applyGuess()'s body). With that in mind, until we receive a TRUE or FALSE from 'applyGuess()', an Exception will be thrown. Until an exception is not thrown anymore and 'applyGuess()' finally returns TRUE or FALSE then we know we have valid input from the user. Therefore, true OR false returned from 'applyGuess()' is used to determine whether the user's input was acceptable. Basically, later in the lessons if we get a return from this method, it means everything checked out OK.

Maybe it would have been more clear to have returned 'true' if it was a valid guess and 'false' if it wasn't instead of 'true' if we got a hit? I'm not sure without reworking the code a little and at this point I don't know what the best approach is frankly. I think you found a good reason to start names of booleans with 'is' ;).