Comment

Comment by morgante

I can't believe this "refactoring" is actually presented as good—creating unnecessary abstractions for a simple function.

If anything, I would go in the opposite direction. There's no need to use parameterized variables at all. Just write out the message 3 times:

  private String generateGuessSentence(char candidate, int count) {
      if (count === 0) {
          return "There are no " + candidate + "s";
      } else if (count === 1) {
          return "There is 1 " + candidate;
      } else {
          return "There are " + count + " " + candidate + "s";
      }
  }
Maybe this isn't "clean" (I'd claim it is) but it's certainly the easiest to understand.
parent morgante 2 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton