Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects Delivering the MVP Determining If the Game is Won

christianwinnen
christianwinnen
5,394 Points

"game.isWon() == false" instead of "!game.isWon()"

In the video Craig uses "!game.isWon()" in the while loop of the Hangman class.

I was wondering if using "game.isWon() == false" would also be correct? This code is obviously just a little bit longer but is there another reason why Craig's version should be used?

1 Answer

alastair cooper
alastair cooper
28,204 Points

In my experience they are functionally equivalent. The following stack overflow post sums up the arguments, but basically it is brevity vs readability https://stackoverflow.com/questions/11831881/if-boolean-false-vs-if-boolean

Personally i think x == false is easier to read, but most experienced programmers I know prefer the ! and see the former as a beginners habit. If the variables are conventionally named, ie isEmpty, isLastRound, etc, then they are equally readable either way

christianwinnen
christianwinnen
5,394 Points

Thanks for your answer!