A Story About Pluralization In Code (2 Items vs 2 Boxes)
Source: Dev.to
Introduction
I was building a simple console application – a tiny shopping cart. It wasn’t fancy, just a chance to experiment with input, calculations, and formatted output. Here’s the first version I wrote:
import java.util.Scanner;
public class Cart {
static void cart() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter item name: ");
String item = scanner.nextLine();
System.out.print("Enter price for each: ");
double price = scanner.nextDouble();
if (price 1
&& !"aeiou".contains("" + word.charAt(word.length() - 2))) {
return word.substring(0, word.length() - 1) + "ies";
}
return word + "s";
}
Running a few tests
System.out.println(pluralize("box", 2)); // boxes
System.out.println(pluralize("city", 2)); // cities
System.out.println(pluralize("class", 2)); // classes
System.out.println(pluralize("apple", 2)); // apples
System.out.println(pluralize("mouse", 2)); // mouses
The results were mostly correct (boxes, cities, classes, apples), but mouse → mouses stood out. Irregular nouns weren’t going to be solved by simple rules, and every new edge case made the code longer and more fragile.
“Even numbers are hard” – a meme that felt all too appropriate.
At this point I realized my “make it perfect” curiosity was bumping into the limitations of language itself.
Discovering ICU4J
I searched for solutions and found ICU4J, a Java library used in production for internationalization and pluralization. However, after reading the docs more carefully, I realized ICU4J doesn’t actually change the spelling of words. It decides when to use a singular versus a plural form, e.g.:
You have 1 item
You have 2 items
It does not automatically turn box into boxes or child into children. It solves the category problem, not the word‑inflection problem.
The Tempting Idea
Disappointed, I considered another approach:
What if I just ask an AI to do this?
Modern AI models are excellent at language. They know that:
box→boxesclass→classesmouse→micechild→children
In theory, I could make an API call, pass the noun, and get back the correct plural. No grammar rules, no irregular‑noun lists – just let the model handle the complexity.
Why the AI Idea Actually Makes Sense (At First)
This approach isn’t foolish; it has real strengths because an AI model:
- Understands irregular nouns
- Handles borrowed words and edge cases
- Adapts to language naturally
- Requires very little code on my end
For a small personal project, the idea is genuinely attractive. I wouldn’t need to maintain pluralization logic at all – I’d just delegate the problem to the AI via an API.
But then I paused and started thinking like a system designer, not just a coder.
The First Red Flag: Cost
Pluralization is a low‑value, high‑frequency operation. If every time a user adds an item to a cart I make a network request to an AI service, the cumulative cost (both monetary and latency‑wise) can quickly outweigh the convenience.
Cart the app needs to:
- Make a network request
- Pay for tokens
- Wait for a response
That cost adds up quickly. Paying for AI inference to decide whether to print boxes or items is hard to justify, especially when the same operation could be done locally with zero cost.
For small scripts or experiments, this might be fine. At scale, it becomes expensive very fast.
The Second Red Flag: Latency
Pluralization sits on the critical path of user feedback. It happens:
- While rendering UI
- While printing output
- During fast interactions
An AI call introduces:
- Network latency
- Timeout risk
- Retry logic
- Failure modes you don’t control (e.g., 500 errors)
Something as simple as printing a sentence suddenly depends on the availability of an external service. That outright felt wrong.
The Third Red Flag: Non‑Determinism
This was by far the biggest concern. Language is flexible, and AI reflects that. For example:
cactus→cacticactus→cactuses
Both are correct, but if the application prints:
You bought 2 cacti
and later:
You bought 2 cactuses
you’ve introduced inconsistency. An answer that is “sometimes right” is often worse than a simpler one that is always consistent.
The Fourth Red Flag: Control and Safety
To pluralize a word using AI, I have to send user input to an external service. This raises questions:
- What if the item name contains sensitive information?
- What if this data must stay on‑device?
- What happens if the API changes behavior?
Suddenly, a simple console program is tied to:
- Network access
- API keys
- Privacy considerations
At this point the picture became clear: using AI for pluralization solves a hard linguistic problem but introduces cost, latency, inconsistency, dependency, and still does not guarantee perfect results.
The Practical Trade‑off
I had to step back and ask myself what the real goal of my program is:
- Showing the number of items bought
- Showing the total cost
The exact spelling of the product name in plural wasn’t critical.
That’s when it clicked—almost every app I use does the same thing. Shopping sites, notification systems, and dashboards don’t attempt to pluralize arbitrary nouns. They simply use a safe, controlled noun:
You bought 2 items
You have 5 notifications
Your cart contains 3 products
It is predictable, safe, and scales well since there are no edge cases to handle, and it also works perfectly if you later translate your application into other languages.
Final Code Snippet
Here’s how my original shopping‑cart code evolved after this reflection:
double total = price * quantity;
String message = quantity == 1 ? "item" : "items";
System.out.printf(
"You bought %d %s. Your grand total is KES %.2f.%n",
quantity, message, total
);
Output
You bought 1 item. Your grand total is KES 100.00.
You bought 2 items. Your grand total is KES 200.00.
No edge cases, no dictionary of irregular nouns, no fragile rules. This approach is exactly what large production systems do.
Lessons Learned
- Curiosity is important. Trying to improve the code teaches you a lot about programming, language, and trade‑offs.
- Language is messy; even simple pluralization can have dozens of edge cases.
- Production code values simplicity, correctness, and consistency.
- Using ICU4J helps in the right context and should be used for plural‑aware messages, not for spelling every noun correctly.
- Opting for a controlled vocabulary wins: it is safe, predictable, and maintainable.
Thought
The journey from apple → apples to understanding why 2 boxes is rarely shown in apps is more than a lesson in pluralization. It is a lesson in designing software systems that work reliably, even when human language does not cooperate.
Sometimes, the simplest solution—like displaying 2 items—is also the most elegant.