DEV Community

Cover image for clean code : add meaningful context
DevByJESUS
DevByJESUS

Posted on

1 2

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 😍

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs