I got into a discussion with a colleague regarding the following issue:
Our project reads in a config file and stores dozens of parameters. He's stored this data in named variables together with dozens of getter methods for each variable. In my opinion, this has made the class overly long with getter methods and difficult to maintain.
private var;
public String getVar() {
return var;
}
// This appears dozens of times in the class
......
My solution is to store key values pairs in a map, and have a single getValue(String key) method which takes as argument a key which represents each variable. The keys will be stored as a list of constants in a Config class which will also handle reading the data from file.
Config c = new Config();
c.readConfig(someFile);
...
c.getValue(Config.SOME_VAR);
His argument against my design is that if any key needs to change or be made obsolete, all instances of the key will have to be hunted down and changed at many places in the source code, whereas in his design everything is managed from 1 file. Also, type safety poses a problem, as Integer.parseInt() from a String returned from getValue() might crash, whereas in his method, the return type is fixed.
Any comments regarding the above? Thanks.
What is so difficult to maintain about his solution? Unless the config format changes, I don't foresee any maintenance (though surprises are always possible). If maintenance is necessary, I think this solution will make it easier, as all the relevant parsing (including string->int, etc.) is in one place.
And his has far better compile-time checking. If you remove a method, the compiler will tell you. If you assign the return value to the wrong type, the compiler will tell you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With