lundi 25 juillet 2016

Check list size with a magic number (1) or global constant?

I'm in an argument with a co-worker about the following code:

private static final byte ONE_ELEMENT = 1;
private boolean isListSizeEqualsOne(List<MyClass> myList) {
    return myList.size() == ONE_ELEMENT;
}

I'm arguing that this kind of code admittedly reduces a warning about a magic number but unnecessarily increases clutter at the same time. I'm suggesting to inline the global variable instead:

private boolean isListSizeEqualsOne(List<MyClass> myList) {
    return myList.size() == 1;
}

Is there any literature for / against this example?

Aucun commentaire:

Enregistrer un commentaire