Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dozens of getter methods vs a single get method [closed]

Tags:

java

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.

like image 847
david Avatar asked Nov 22 '10 06:11

david


1 Answers

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.

like image 77
Matthew Flaschen Avatar answered Oct 12 '22 10:10

Matthew Flaschen