mardi 14 août 2018

If statement - Variable order for null safety

Assume I want to check if the first element from a list is equal to "YES" or "NO".

dummy_method(List<String> myList) {
    if(myList.isEmpty()) {
        return null; 
    }
    String firstListValue = myList.get(0).getStringValue(); 
    // Should I do this: 
    if ("YES".equalsIgnoreCase(firstListValue)) {
        return firstListValue; 
    }
    // OR this: 
    if (firstListValue.equalsIgnoreCase("YES")) {
        return firstListValue; 
    }
    // Do something else
}

In other words: Does the order of if A equals B versus if B equals A matter when I already have a null-check?

Aucun commentaire:

Enregistrer un commentaire