Avoid if-else statements using collections

For transformations, there are two options. Either write an implementation, complete with conditional logic and documentation, or write the table out then lookup in it.

Using HashMaps for look-ups can make code much more readable as well as efficient. Here are the reasons:


There is a code quality metric called Cyclomatic Complexity. This metric basically counts the number of different paths through the code.

For every possible execution path a method becomes harder and harder to both understand AND fully test for correctness.


It boils down to the fact that “controlling keywords” like: ifs, elses, whiles, etc… leverage boolean tests that can be wrong. Repeated use of “controlling keywords” produces fragile code.


There are two speeds in software: the time it takes to write/read/debug the code; and the time it takes to execute the code.

For large amounts of values, the map will perform better. Using a hashtable, one can look up the answer in constant time. The multiple-if construct has to compare the input to each of the possibilities until it finds the right answer.

In other words, the map lookup is O(1) while the ifs are O(n) where n is the number of possible inputs.

Map creation is O(n), but is only done once if it is static constant state. For a lookup performed frequently the map will outperform the if statements in the long run, at the cost of slightly more time when the program is starting up (or the class is loaded, depending on language).

That being said, the map is not always the right tool for this job. It is nice when there are a lot of values, or the values must be configurable through a text file, user input, or database (in which case the map acts as a cache).


Data is better than code. Not least because it’s too tempting to add yet another branch to code, yet adding a row to a table is tough to get wrong.

A table of data is always a better representation of some data than code is, modulo optimisation passes.


import java.util.Map;

class Scratch {

    private static final Map<String, String[]> map;
    static{
        // immutable
        map = Map.of("USA", new String[]{"usa-address-line-1", "usa-city", "usa-state", "usa-zip-code"},
                "Canada", new String[]{"canada-address-line-1", "canada-city", "canada-state", "canada-zip-code"});
    }

    private static MyAddress transformation_using_if_else(String countryName){

        MyAddress myAddress = new MyAddress();

        if(countryName.equals("USA")) {
            myAddress.addressLine1 = "usa-address-line-1";
            myAddress.city = "usa-city";
            myAddress.state = "usa-state";
            myAddress.zipCode = "usa-zip-code";
            return myAddress;
        }
        else if(countryName.equals("Canada")) {
            myAddress.addressLine1 = "canada-address-line-1";
            myAddress.city = "canada-city";
            myAddress.state = "canada-state";
            myAddress.zipCode = "canada-zip-code";
            return myAddress;
        }
        else throw new RuntimeException("Invalid input to function foo");
    }

    private static MyAddress transformation_using_map(String countryName){

        String[] stringArray = map.get(countryName);

        MyAddress myAddress = new MyAddress();
        myAddress.addressLine1 = stringArray[0];
        myAddress.city = stringArray[1];
        myAddress.state = stringArray[2];
        myAddress.zipCode = stringArray[3];

        return myAddress;
    }

    public static void main(String[] args) {
        System.out.println(transformation_using_if_else("USA"));
        System.out.println(transformation_using_map("Canada"));
    }

    static class MyAddress {
        private String addressLine1;
        private String city;
        private String state;
        private String zipCode;

        @Override
        public String toString() {
            return "MyAddress{" +
                    "addressLine1='" + addressLine1 + '\'' +
                    ", city='" + city + '\'' +
                    ", state='" + state + '\'' +
                    ", zipCode='" + zipCode + '\'' +
                    '}';
        }
    }
}