Search code examples
javanaming-conventionsnaming

How to name two variables that have the same meaning but different types


I have a method that parses a String and converts it to a boolean. The legal values are "true" and "false".

boolean convertStringToBoolean(String text) {
  if (text.equals("true") {
    return true;
  } else if (text.equals("false")) {
    return false;
  } else {
    throw new IllegalArgumentException(text);
  }
}

When I use this variable, I get a naming problem.

void doSomething(String isSpecificReadString) {
  boolean isSpecificRead = convertStringToBoolean(isSpecificReadString);
  ...
}

The problem is that the parameter carries a meaning that I want to keep in its name. The meaning doesn't change just because the type is changed. So far, my solution has been to just suffix the type to the parameter. But I don't like this solution.

What should I call the variables to solve this problem?


Solution

  • As inspired by Andy Turner, I can make two doSomething methods. A method that takes a String and another that takes a boolean. The String method can then call the boolean method. As follows:

    void doSomething(String isSpecificRead) {
      doSomething(convertStringToBoolean(isSpecificReadString));
    }
    
    void doSomething(boolean isSpecificRead) {
      ...
    }