Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

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 themeaningful 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 modeExit 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 modeExit fullscreen mode

this is absolutely 😍

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I want to change the world with code by having fun with it . #keeplearning #cleancode #tdd #atdd #bdd #ddd
  • Location
    Dakar , Senegal , Africa
  • Joined

More fromDevByJESUS

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp