考虑这一行:

if (object.getAttribute("someAttr").equals("true")) { // ....

显然,这条线是一个潜在的错误,属性可能会null,我们会得到一个NullPointerException。因此,我们需要把它重构为两个选择:

<强>第一种选择:

if ("true".equals(object.getAttribute("someAttr"))) { // ....

<强>第二个选项:

String attr = object.getAttribute("someAttr");
if (attr != null) {
    if (attr.equals("true")) { // ....

第一个选项是笨拙读但更简洁,而第二个是在意图明确,但冗长。

你在可读性方面更喜欢哪一种方式?

有帮助吗?

解决方案

我一直使用

if ("true".equals(object.getAttribute("someAttr"))) { // ....

因为它虽然是一个小更难以读懂它的详细得多,我认为这是足够的可阅读,所以你习惯它很容易

其他提示

在第二个选项,则可以利用短路&&的:

String attr = object.getAttribute("someAttr");
if (attr != null && attr.equals("true")) { // ....

有在简洁的做法感到不对某些情况下下手,但有效的变成的成语。这是其中之一;另一种是这样的:

String line;
while ((line = bufferedReader.readLine()) != null) {
  // Use line
}
在的条件下

副作用?不可想象的!除了它基本上比其它更好,当你认识到特定的模式。

此模式类似 - 这是在Java中很常见的,我期望的任何合理经验的开发人员承认它。其结果是相当简洁。 (有趣的是,有时会看到它使用相同的成语,不必要的C#代码 - 相等运算正常工作与在C#字符串)

底线:使用第一个版本,并且熟悉它

我喜欢的选项1和我认为,它是足够的可读性。

选项3顺便说一句会引入getAttribute方法采用一个默认值作为参数。

始终向往较短的码,因为这两个在功能上等同。尤其是在情况是这样,其中可读性不牺牲。

Util.isEmpty(string) - 返回string == null || string.trim().isEmpty() Util.notNull(string)返回“”如果string == null,串否则。 Util.isNotEmpty(string)回报! Util.isEmpty(string)

和我们有一个约定的字符串,Util.isEmpty(string)语义意味着真Util.isNotEmpty(string)语义意味着假。

这是一个非常好的问题。 我通常使用的不优美:

if (object.getAttribute("someAttr") != null && object.getAttribute("someAttr").equals("true")) { // ....

(我不会使用它了)

我有另一个答案;

List<Map<String, Object>> group = jjDatabase.separateRow(db.Select("SELECT * FROM access_user_group  WHERE user_id=1 ;"));

没有“group_c80”,如在我的数据库“access_user_group”列,所以在获取(0)获得(“group_c80”)空指针异常协定。但我处理它通过以下代码:

for (int j = 1; j < 100; j++) {
                    String rulId="0";//defult value,to privent null pointer exeption in group_c
                    try {
                        rulId = group.get(0).get("group_c" + j)).toString();
                    } catch (Exception ex) {
                        ServerLog.Print( "Handeled error in database for " + "group_c" + (j < 10 ? "0" + j : j) +"This error handeled and mot efect in program");
                        rulId = "0";
                    }}

下面是我的做法,但需要一个PropertyUtil类,但它只能写一次:

/**
 * Generic method to encapsulate type casting and preventing nullPointers.
 * 
 * @param <T>          The Type expected from the result value.
 * @param o            The object to cast.
 * @param typedDefault The default value, should be of Type T.
 * 
 * @return Type casted o, of default.
 */
public static <T> T getOrDefault (Object o, T typedDefault) {
    if (null == o) {
        return typedDefault;
    }
    return (T) o;
}

客户端代码可以做到这一点:

PropertyUtil.getOrDefault(obj.getAttribute("someAttr"), "").equals("true");

,或对于一个列表:

PropertyUtil.getOrDefault(
    genericObjectMap.get(MY_LIST_KEY), Collections.EMPTY_LIST
).contains(element);

或者以列表的消费者,这将拒绝对象:

consumeOnlyList(
    PropertyUtil.getOrDefault(
        enericObjectMap.get(MY_LIST_KEY), Collections.EMPTY_LIST
    )
)

默认可能是空对象图案的IMPL https://en.wikipedia.org/wiki / Null_Object_pattern

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top