Which functions I think should and shouldn't use const(#5 will surprise you!)

I know that the request was to just suggest which functions should be made to be const, but I decided to just go down the list of our functions and write down why I think they should be made const or not.

  1. void Reset(): I believe that this shouldn’t be const. I haven’t gone any further, but I am pretty sure that this is gonna reset current tries at the least when we write it and will probably also generate a new word or change the length of the word depending on the features we implement.
  • int GetMaxTries(): We made this const already because it is one of our getters and it doesn’t need to alter any of our private variables.
  • int GetCurrentTry(): This is the class’s sole other getter and as such is the function that we were tasked with making it const.
  • bool IsGameWon(): This function should only need to access private variables and has no reason to change, which is why I think that this function is a good candidate to be made const.
  • bool CheckGuessValidity(std::string): This function is going to check if the player’s input is a valid guess so it is another good candidate to be made const.

Now I originally believed that bool CheckGuessValidity(std::string) would not be const because I thought that’s naturally where we are going to alter MyCurrentTry. After thinking about it during this exercise I realized that would create a side effect and that @ben said we should trying to avoid side effects.In the future, we will create a setter and the logic for incrementing the MyCurrentTry will be handled in a side effect free way.

1 Like

You could write for Buzzfeed.

1 Like

Personally #5 did surprise me. Before I went through and thought about each I had figured that we’d be incrementing MyCurrentTry in CheckGuessValidity and I almost blindly wrote that but as I got to it I realized that would be a side-effect and we should try and avoid that. Although I suspect we may just do that anyways given how small our program will end up being, easier to just have a small side-effect that’s pretty harmless and not have to screw around with a setter.

Privacy & Terms