循ç°çè¤é度ã¯ã©ã®ãããã«è¨å®ããã¹ãã
ã½ã¼ã¹ã³ã¼ãã®è¤éæ§ã表ãåºæºã¨ãã¦ã循ç°çè¤é度ã¨ããææ¨ãããã¾ãã
ãã®å¾ªç°çè¤é度ã¯ã©ããããã®è¨å®å¤ã«ããã®ãé©åãªã®ããã¾ã¨ãã¦ãããã¨æãã¾ãã
循ç°çè¤é度ã¨ã¯
循ç°çè¤é度ã¯Thomas J. McCabeæ°ã«ãã£ã¦1976å¹´ã«èæ¡ãããææ¨ã§ãã
ãã®å¤ã¯ã½ã¼ã¹ã³ã¼ãã®è¤éæ§ã示ããã®ã§ããããã®å¤ã測å®ããä½ãå¤ãä¿ã¤ãã¨ã§ãã½ã¼ã¹ã³ã¼ãã®ä¿å®æ§ãé«ãç¶æ
ã§ç¶æãããã¨ãã§ãã¾ãã
循ç°çè¤é度ã®ç®å®
循ç°çè¤é度ã®åºæºã¯ã対象ã¨ãã¦ããã½ããã¦ã§ã¢ã®è¤éæ§ãããã¡ã¤ã³ã®è¤éæ§ãªã©ã«å·¦å³ããã¾ãã
ãã®ããã絶対ã®æ°å¤ã¨ãããã®ã¯ããã¾ããããæ¸ç±ãªã©ã§ç´¹ä»ããã¦ãããã®ã®ä¸é¨ãè¦ã¦ãããã¨æãã¾ãã
MathWorks
MathWorksãå
¬éãã¦ããåºæºã§ãã
循ç°çè¤é度ã®ææ¨ã¨ãã¦ã¯ä¸çªå¼ç¨ãããåºæºã ã¨æãã¾ãã
循ç°çè¤é度 | è¤éãã®ç¶æ | ãã°æ··å ¥ç¢ºç |
10ä»¥ä¸ | é常ã«è¯ãæ§é | 25% |
30ä»¥ä¸ | æ§é çãªãªã¹ã¯ãã | 40% |
50ä»¥ä¸ | ãã¹ãä¸å¯è½ | 70% |
75ä»¥ä¸ | ãããªãå¤æ´ã誤修æ£ãçã | 98% |
Software Architecture Metrics
ã½ã¼ã¹ã³ã¼ãã®ã¢ã¼ããã¯ãã£ãªã©ãè©ä¾¡ããæ§ã
ãªã¡ããªã¯ã¹ãç´¹ä»ãã¦ããæ¸ç±ã§ãã
æ¥æ¬èªè¨³ã¯ç¾æç¹ã§åºçããã¦ãã¾ããã
This metric is well researched, and we know that error rates increase quickly for all values above 24. I recommend a threshold of 15, to stay on the safe side.
 â訳
ãã®ææ¨ã¯ããç 究ããã¦ããã24ãè¶ ãããã¹ã¦ã®å¤ã«å¯¾ãã¦ã¨ã©ã¼çãæ¥éã«å¢å ãããã¨ãããã£ã¦ãã¾ããå®å ¨ã確ä¿ããããã«ãç§ã¯15ã®é¾å¤ãæ¨å¥¨ãã¾ãã
Software Architecture Metrics: Case Studies to Improve the Quality of Your Architecture
ã½ããã¦ã§ã¢ã¢ã¼ããã¯ãã£ã®åºç¤
è²ã
ãªã½ããã¦ã§ã¢ã¢ã¼ããã¯ãã£ãªã©ãç´¹ä»ãã¦ããæ¸ç±ã§ãã
åæã¯è±èªã§ãããæ¥æ¬èªè¨³çãåºçããã¦ãã¾ãã
In general, the industry thresholds for CC suggest that a value under 10 is acceptable, barring other considerations such as complex domains. We consider that threshold very high and would prefer code to fall under five, indicating cohesive, well-factored code.Â
â訳ï¼æ¥æ¬èªè¨³çãæã£ã¦ããªãã£ãããåæããå¼ç¨ãã¾ãï¼
ä¸è¬çã«ãæ¥çã§ã®CCï¼å¾ªç°çè¤é度ï¼ã®é¾å¤ã¯ãè¤éãªé åãªã©ã®ä»ã®èæ ®äºé ãé¤ãã°ã10æªæºã§ããã°åãå ¥ããããã¨ããã¦ãã¾ããç§ãã¡ã¯ãã®é¾å¤ãé常ã«é«ãèãã¦ããã5æªæºã§ããã°ãå縮æ§ã®ãããé©åã«ãã¡ã¯ã¿ãªã³ã°ãããã³ã¼ãã示ãã¦ããã¨èãã好ã¾ããã¨èãã¦ãã¾ãã
å®éã®ã½ã¼ã¹ã³ã¼ã
ã§ã¯ãå®æ½ã«Javaã®ã½ã¼ã¹ã³ã¼ãã§ã循ç°çè¤é度ãã5ãã®ã³ã¼ããã10ãã®ã³ã¼ããã15ãã®ã³ã¼ãã®ãµã³ãã«ãè¦ã¦ã¿ããã¨æãã¾ãã
ã½ã¼ã¹ã³ã¼ãã®ä½æã¯ChatGPTã«ãé¡ããã¦ãã¾ãããCheckStyleã§ã確èªãã¦ãã¾ãã
循ç°çè¤é度ã5ã®ã³ã¼ã
public class CyclomaticComplexityExample5 {
  public static void exampleMethod(int a, int b, int c, int d) {
    if (a > b) {
      // ãã¹1
      System.out.println("a is greater than b");
    } else {
      // ãã¹2
      System.out.println("b is greater than or equal to a");
    }
    for (int i = 0; i < c; i++) {
      // ãã¹3
      System.out.println("i: " + i);
    }
    if (c > 10) {
      // ãã¹4
      System.out.println("c is greater than 10");
    }
    if (d % 2 == 0) {
      // ãã¹5
      System.out.println("d is even");
    }
  }
}
循ç°çè¤é度ã10ã®ã³ã¼ã
public class CyclomaticComplexityExample10 {
  public static void exampleMethod(int a, int b, int c, int d, int e) {
    if (a > 0) { // 1
      System.out.println("a is positive");
    }
    if (b > 0) { // 2
      System.out.println("b is positive");
    }
    if (c > 0) { // 3
      System.out.println("c is positive");
    }
    if (d > 0) { // 4
      System.out.println("d is positive");
    }
    if (a < 0) { // 5
      System.out.println("a is negative");
    }
    if (b < 0) { // 6
      System.out.println("b is negative");
    }
    if (c < 0) { // 7
      System.out.println("c is negative");
    }
    if (d < 0) { // 8
      System.out.println("d is negative");
    }
    if (e != 0) { // 9
      System.out.println("e is non-zero");
    }
    // ããã©ã«ããã¹ï¼10ï¼
    System.out.println("Check complete");
  }
}
循ç°çè¤é度ã15ã®ã³ã¼ã
public class CyclomaticComplexityExample15 {
  public static void exampleMethod(int a, int b, int c, int d, int e) {
    if (a > 0) { // 1
      System.out.println("a is positive");
    }
    if (b > 0) { // 2
      System.out.println("b is positive");
    }
    if (c > 0) { // 3
      System.out.println("c is positive");
    }
    if (d > 0) { // 4
      System.out.println("d is positive");
    }
    if (e > 0) { // 5
      System.out.println("e is positive");
    }
    if (a < 0) { // 6
      System.out.println("a is negative");
    }
    if (b < 0) { // 7
      System.out.println("b is negative");
    }
    if (c < 0) { // 8
      System.out.println("c is negative");
    }
    if (d < 0) { // 9
      System.out.println("d is negative");
    }
    if (e < 0) { // 10
      System.out.println("e is negative");
    }
    if (a == 0) { // 11
      System.out.println("a is zero");
    }
    if (b == 0) { // 12
      System.out.println("b is zero");
    }
    if (c == 0) { // 13
      System.out.println("c is zero");
    }
    if (d == 0) { // 14
      System.out.println("d is zero");
    }
    // ããã©ã«ããã¹ï¼15ï¼
    System.out.println("Evaluation complete");
  }
}
ãã®ãµã³ãã«ã³ã¼ãã¯å¦çããããç°¡åãªãã®ã§ããã循ç°çè¤é度ãã15ãã®ã³ã¼ãã¯æ¡ä»¶åå²ã®éããããªãã«å¤ãããã£ããå¦çãããå ´åã«ã¯ãããªãã®è¤éãªã³ã¼ãã«ãªãããã§ãã
ã¾ã¨ã
循ç°çè¤é度ã®ç®å®ã«ã¤ãã¦è¦ã¦ã¿ã¾ããã
åºæ¬çã«ã¯ã10ãããããã¯ã15ã以ä¸ã«è¨å®ããã®ãè¯ãããã§ãã
ãããããæ´ã«å³ããã«è¨å®ãããå ´åã¯ãã5ããã5ã¨10ã®ä¸éã®ã8ããªã©ã«è¨å®ããã®ãè¯ãã®ã§ã¯ãªãã§ããããï¼
ãã ãããããã®è¨å®å¤ã¯ã½ããã¦ã§ã¢ã®è¤éæ§ãªã©ã®ããä¸æ¦ã«ã¯æ±ºãããã¾ããã®ã§ãæ°è¦éçºã®éã¯æåã¯å³ãç®ã®å¤ã«ãã¦ããã¦ããã®åºæºãå®ãã®ãã³ã¹ããä¿å®æ§ã«è¦åããªãã¨å¤æããå ´åã«åºæºå¤ãç·©ããã¦ããã®ãè¯ãã¨æãã¾ãã