2020年11月18日 星期三

[Java] Collapsible "if" statements should be merged (java:S1066)

 Merging collapsible if statements increases the code's readability.

Noncompliant Code Example

if (file != null) {
  if (file.isFile() || file.isDirectory()) {
    /* ... */
  }
}

Compliant Solution

if (file != null && isFileOrDirectory(file)) {
  /* ... */
}

private static boolean isFileOrDirectory(File file) {
  return file.isFile() || file.isDirectory();
}
以上內容的意思是~
當if條件句中, 又只有一個if條件句
這兩個if條件句就可以合併成一個
中間用&&隔開~真是有時候並不會發現的小毛病呢~

沒有留言:

張貼留言

AI 時代的軟體工程

  AI 時代的軟體工程:從「代碼寫手」到「系統指揮官」的轉型之路 2026 年,軟體工程正經歷自編譯器發明以來最大的範式轉移。AI 不再只是 IDE 側邊欄的輔助工具,而是進化為具備自主性的 Agent(代理人) ,這場變革正重新定義「工程師」的核心價值。 一、 現狀:AI 普...