DEV Community

Cover image for clean code : add meaningful context
DevByJESUS
DevByJESUS

Posted on

clean code : add meaningful context

I am not gonna talk to much just look at the meaningful context differences between these two examples.

Example 1

private void printGuessStatistics(char candidate, int count){

 String number;
 String verb;
 String pluralModifier;

if (count == 0) {
 number = "no";
 verb = "are";
 pluralModifier = "s";
} else if (count == 1) {
 number = "1";
 verb = "is";
 pluralModifier = "";
} else {
 number = Integer.toString(count);
 verb = "are";
 pluralModifier = "s";
}
  String guessMessage = String.format(
   "There %s %s %s%s", verb, number, candidate, 
   pluralModifier
  );
  print(guessMessage);

}

Enter fullscreen mode Exit fullscreen mode

Example 2

public class GuessStatisticsMessage {

private String number;
private String verb;
private String pluralModifier;

public String make(char candidate, int count) {
   createPluralDependentMessageParts(count);
  return String.format(
   "There %s %s %s%s",
   verb, number, candidate, pluralModifier );
}

private void createPluralDependentMessageParts(int count) {
  if (count == 0) {
   thereAreNoLetters();
  } else if (count == 1) {
   thereIsOneLetter();
  } else {
    thereAreManyLetters(count);
  }
}

private void thereAreManyLetters(int count) {
  number = Integer.toString(count);
  verb = "are";
  pluralModifier = "s";
}

private void thereIsOneLetter() {
  number = "1";
  verb = "is";
  pluralModifier = "";
}

private void thereAreNoLetters() {
  number = "no";
  verb = "are";
  pluralModifier = "s";
 }

}

Enter fullscreen mode Exit fullscreen mode

this is absolutely 😍

Top comments (0)