Why are we introducing variables (like bWantsToPlayAgain)?

Why aren’t we just using
while(AskToPlayAgain());

Is there some deeper meaning behind this or is it just a preference?

No deeper meaning, I don’t see the need for the variable either but whatever makes it more readable for you.

variables store a state to be used later on , functions/methods usually return a current state.

Also if retrieving a state can be performance intensive it wont be wise to call the method more than once unless you have to (ie you have the reason to suspect that the state has changed and that affects the outcome of your code)

Another reason can be in cases where function cannot or should not be called directly, for example when you do not want a dialog to appear

So its a good idea to retain state, live state (= current data) after all is a extremely important part of OOP.

Thanks for your reply Dimitris, it makes sense and all but I see I should paste whole code when asking question in such specific context (exactly this section of tutorial, only this variable so far).

Indeed if you want a specific answer a specific case study , meaning example code, helps a lot.

The important thing is that you start early on to exercise critical thinking. You should see in a lot of cases coders doing it the same way, some claiming a way as a golden rule, others arguing that you should not do it in a specific way. People tend to develop bad habits and find lame excuses for them.

The best way to code is through trail and error. Many of the decisions may seem more ideological/philosophical and you may be tempted to follow the popular way but later on you will see that trusting you own critical thinking and developing it at the same time can greatly benefit you and is great you ask these questions. Finding also your personal style may make coding much more enjoyable too.

Privacy & Terms