HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Naming Vegetable Factories

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
factoriesvegetablenaming

Problem

Given the code below, I'm curious to know if you'd have better naming suggestions for types:

  • VegetablePresentersFactory



  • VegetablePresenterFactory



It bothers me a bit that their names only differ by a single character ('s'), and that this character is located in the middle of the name.

class VegetablesApp {
    private final VegetablePresentersFactory vegetablePresentersFactory; // gets injected

    public void displayVegetablePresenters(AllVegetables all) {
        List presenters = vegatablePresentersFactory.create(all);

        displayPresenters(presenters);
    }
}

class VegetablePresentersFactory {
    private final VegetablePresenterFactory vegetablePresenterFactory;

    public List create(AllVegetables all) {
        List presenters = new ArrayList<>();

        list.add(vegetablePresenterFactory.create(all.getTomato()));
        list.add(vegetablePresenterFactory.create(all.getLettuce()));
        list.add(vegetablePresenterFactory.create(all.getBacon()));

        return presenters;
    }
}

interface VegetablePresenterFactory {
    VegetablePresenter create(Vegetable vegetable);
}

Solution

How about replacing "s" of plural with the word "List":

interface VegetablePresenterListFactory {    
    List create(AllVegetables allVegetables);
}


Like you said, the s in the middle is indeed a problem.
The difference is too small: it can lead to errors, and it hurts readability.

The same is true for name vs names in this code snippet:

void printNames(List names) {
    for (String name : names) {
        // ...
    }
}


Although errors may sound unlikely because name and names are completely different types,
it can still happen when there are methods with the same name that happen to work on both objects,
for example isEmpty.

In such cases I use the suffix List to disambiguate, so nameList.
It's true that the term "List" is a bit redundant.
But it's better to be redundant than ambiguous,
so the little redundancy is justified by the improved readability.

UPDATE

You mentioned in the comment that "List" is a bit unfortunate if in the future you change the interface to use a Set instead of a List.
I think that's a moot point.
Changing the interface is a big deal,
and must be done carefully.
If you change the method to return a Set instead of a List,
I think you should be careful enough to notice that the interface name should change as well.
In terms of migrating users of the API,
this should not be a significant additional burden.

That said, I do have an alternative offering, using the word "Items" instead of "List":

interface VegetablePresenterItemsFactory {    
    List create(AllVegetables allVegetables);
}


The good thing about "Items" is that it's less technical.
It's vague enough to be flexible, but not too vague to be meaningless.
Perhaps there are even better synonyms, you get the idea.

Code Snippets

interface VegetablePresenterListFactory {    
    List<VegetablePresenter> create(AllVegetables allVegetables);
}
void printNames(List<String> names) {
    for (String name : names) {
        // ...
    }
}
interface VegetablePresenterItemsFactory {    
    List<VegetablePresenter> create(AllVegetables allVegetables);
}

Context

StackExchange Code Review Q#69189, answer score: 3

Revisions (0)

No revisions yet.