Return 0 或返回 null

huangapple go评论88阅读模式
英文:

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?

我有这段代码,我想检查代码是否存在于地图中。是返回基本数据类型并检查是否不等于零更好?还是返回Long包装器并返回null,然后检查是否为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;
}

答案1

得分: 1

以下是当前建议的解决方案概述以及我的想法。请注意,所有这些都是有效的解决方案,所以最终取决于个人偏好以及消费者是谁。

  1. 返回Long并检查是否为null

    • 一般来说,我尽量避免使用null。如果存在一个长整型值无法合理表示为可能答案集合中的一部分,那么我会选择使用该值而不是null。
  2. 返回long并检查是否为0

    • 只要0明显是不完整的解决方案,这就可以正常工作,这类似于如果未找到则返回-1的indexOf。请注意,在这种情况下,我更喜欢使用-1而不是0,因为0通常是有效的值,而-1通常是无效的,总体而言,-1比0更容易理解。
  3. 如果未找到,则抛出异常

    • 在某些情况下,这可能是有道理的,具体来说,很难找不到一个代码。一般来说,异常和异常处理是昂贵的。同样,try/catch很冗长,所以如果可以避免,就应该避免。
  4. 返回一个Optional泛型

    • 这是一个更现代的解决方案。我很喜欢这个,但是,由于你已经在使用Longs...
  5. 返回一个OptionalLong

    • 不妨使用这个。我的个人观点是,这可能是目前为止最好的解决方案。它具有定义的接口,在Java 8+中是标准的,并且容易通过谷歌搜索找到。
  6. 返回特定值/类型来表示未知

    • 对我来说,这是典型的Java,或者一般的面向对象编程。所以,需要考虑这一点。如果我正在构建一个供其他人使用的库来消耗这些数据,我可能会选择这个选项。但是,对于你的项目来说,这可能也是过度设计。

正如上面所提到的,在大多数情况下,我会选择OptionalLong解决方案,因为它在过于面向对象和过于定制之间取得了平衡。我认为可以有理由转向货币对象,这在合适的项目中可能是有意义的。

此外,也许Code Review更适合这个问题?

英文:

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?

huangapple
  • 本文由 发表于 2020年8月5日 00:03:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63250742.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定