Develofer
Develofer

Reputation: 41

Return 0 or return null

I have this code and I want to check if the code is not existing in the map.. Is it better to return primitive type and check if its not equal to zero? or its better to return a Long wrapper and return null then check if its null.. Is there any advantage with each other?

code = getCurrencyCode(parameterMap);
if (code != 0) {
    //do something with the code
}

private static long getCurrencyCode(Map<String, String> parameterMap) {
    for (Map.Entry<String, String> entry : MapUtils.emptyIfNull(parameterMap).entrySet()) {
        if (StringUtils.startsWith(entry.getKey(), CONFIG_CURRENCY)) {
            return  NumberUtils.createLong(entry.getValue());
        }
    }
    return 0;
}

OR

code = getCurrencyCode(parameterMap);
if (code != null) {
    //do something with the code
}

private static Long getCurrencyCode(Map<String, String> parameterMap) {
    for (Map.Entry<String, String> entry : MapUtils.emptyIfNull(parameterMap).entrySet()) {
        if (StringUtils.startsWith(entry.getKey(), CONFIG_CURRENCY)) {
            return  NumberUtils.createLong(entry.getValue());
        }
    }
    return null;
}

Upvotes: 0

Views: 1628

Answers (1)

Hikash
Hikash

Reputation: 429

Here's a rundown of the current suggested solutions, and my thoughts on them. Note that all of these are valid solutions, so it really comes down to preference, as well as who the consumer is.

  1. return a Long and check for null
    • In general, I prefer not to use null as much as possible. If there is a long value that could not be reasonably represented in the set of possible answers, then I would opt to use that over null.
  2. return a long and check for 0
    • This works fine as long as 0 is an obviously incomplete solution -- this is akin to having indexOf return -1 if not found. Note that I'd prefer using -1 over 0 in this case, as 0 tends to be a valid value, while -1 tends to be an invalid one, generally. I.e. there's less cognitive load with -1 over 0 generally.
  3. Throw an exception if not found
    • This might make sense in certain situations -- specifically, it being very unlikely to not find a code. In general, exceptions and exception handling is expensive. Likewise, try/catch is verbose, and so if it can be avoided, it should be.
  4. Return an Optional generic
    • This is a much more modern solution. I like this quite a bit, but, since you're already using Longs...
  5. Return an OptionalLong
    • Might as well use this. My personal opinion is that this is probably the best solution so far. It has a defined interface, is standard in java 8+, and is easily google-able.
  6. Return a specific value/type to represent Unknown.
    • To me, this is classic Java, or object oriented programming in general. So, there's that to consider. I might opt for this if I was building a library for others to consume this data. But, this could also be overkill for your project.

As noted above, in most cases, I'd opt for the OptionalLong solution, as it strikes a balance between being too object-orienty, and being too custom. I think there's arguments to move to a Currency object, which might make sense in the right project.

Also, possibly Code Review is a better fit for this question?

Upvotes: 1

Related Questions