Is this proper practice?

I used a different way to separate the code. Is it still proper practice or is the way he separated it more proper?

string GetGuess() {

string Guess = “”;
// get a guess from the player
cout << "Enter your guess: ";
getline(cin, Guess);

return Guess;
}

void PrintGuess() {
// repeat the guess back to them
cout << "Your guess was: " << GetGuess() << endl;
return;
}

void PlayGame() {
// loops the guesses
for (int i = 1; i <= TOTAL_GUESSES; i++) {
PrintGuess();
cout << endl;
}

return;
}

A better practice would be that each method does a single job or at least what their name suggests. This is called cohesion. In your case it would be better to have it e.g. like so:

void PrintGuess(string theGuess) {
  // repeat the guess back to them
  cout << "Your guess was: " << theGuess << endl;
  return;
}


void PlayGame() {
  // loops the guesses
  for (int i = 1; i <= TOTAL_GUESSES; i++) {
	PrintGuess(GetGuess());
	cout << endl;
  }
  return;
}

Privacy & Terms