I saw a pull request come in the other day that had a string as a parameter in a function where the string wasn't modified. Instead, the string was used to toggle between what the function does.
What are the pros and cons of doing it this way vs creating separate functions like getNewTableVersion
/ getOldTableVersion
to omit the tableType
parameter?
public static String getTableVersion(String prefix, String tableVersion, String tableType) {
String[] tableVersions = tableVersion.split(prefix);
Integer version;
switch (tableType) {
case "new":
// set version to newer one
case "old":
// set version to older one
default:
// default functionality
}
return prefix + version.toString();
}
For the record, I believe this is a bad practice. I think with a list of pros and cons I may be able to convince the developer not to do this.
I'd say it is bad practice, because you cannot infer the functionality of the function from its prototype. It reeks of magical constants.
If you want to do something like this, an option is to use an enum
to limit the possibilities of the tableType
parameter.