JetBrains製ã®C# IDEã§ããRiderã«ã¯ãæ§ã ãªãã©ã°ã¤ã³ãæä¾ããã¦ãã¾ãã ãã®ä¸ãããã³ã¼ãã®è¤é度ãè¨æ¸¬ãããã©ã°ã¤ã³ã2ã¤ç´¹ä»ãã¾ãã
ãµã¤ã¯ãããã£ãã¯è¤é度
ãµã¤ã¯ãããã£ãã¯è¤é度ï¼cyclomatic complexityï¼ã¯å¾ªç°çè¤é度ã¨ãå¼ã°ããã³ã¼ãã®è¤éãã測ãææ¨ã¨ãã¦ãã使ããã¾ãã ã¡ã½ããåä½ã«åå²ã«ãããã¹ã®æ大æ°ãç®åºããæ°å¤ã§ãä¸è¬ã«ã32以ä¸ã§ãã°æ··å ¥çãé«ãã50以ä¸ã§ãã¹ãå°é£ã¨è¨ããã¦ãã¾ãã
ãããRiderã®ã¤ã³ã¹ãã¯ã·ã§ã³ã«çµã¿è¾¼ããã©ã°ã¤ã³ãCyclomaticComplexityã§ãã
CyclomaticComplexityãã©ã°ã¤ã³ã¯ãä¸è¬ã®ãã©ã°ã¤ã³ã¨åæ§ãPreferences... > Plugins ããã¤ã³ã¹ãã¼ã«ã»æå¹åã§ãã¾ãã æå¹åããã¨ãã¨ãã£ã¿ã®ã¡ã½ããå®ç¾©ã®ä¸ã«ãµã¤ã¯ãããã£ãã¯è¤é度ã表示ãããããã«ãªãã¾ãã
ä¸å³ã§ã¯ããµã¤ã¯ãããã£ãã¯è¤é度ã29ãWariningã¨ãªããããå¤20ï¼ããã©ã«ãå¤ï¼ã«å¯¾ãã¦145%ã§ããã¨è¡¨ç¤ºããã¦ãã¾ãã
ãããå¤ã¯ãPreferences > Languages & Frameworks > Cyclomatic Complexity ã§å¤æ´ã§ãã¾ãã æ¢åããã¸ã§ã¯ãã§æ°å¤ã®é«ãã¡ã½ãããå¤ãå ´åã¯ããããå¤ãç·©ãã®å¤ã«ãã¦ç®ç«ã¤ãã®ããå°ãã¥ã¤ãªãã¡ã¯ã¿ãªã³ã°ãé²ãã¦ãããã¨ããããããã¾ãã
ã³ã°ããã£ãè¤é度
ã³ã°ããã£ãè¤é度ï¼cognitive complexityï¼ã¨ã¯ã2016å¹´ããSonarSource社*1ãæå±ãã¦ããæ¯è¼çæ°ããè¤é度ã®ææ¨ã§ãã循ç°çè¤é度ã«ãªãã£ã¦æ¥æ¬èªåããã®ã§ããã°ãèªç¥çè¤é度ãã§ããããã
Cognitive Complexity - A new way of measuring understandability
SonarSource社ã¯ããµã¤ã¯ãããã£ãã¯è¤é度ã§ã¯ä¿å®æ§ãæ£ããæ°å¤åã§ãã¦ãããããã®ç¹ãæ¹åãããã®ãã³ã°ããã£ãè¤é度ã§ããã¨æ¸ãã¦ãã¾ãã
ä¾ãã°ããµã¤ã¯ãããã£ãã¯è¤é度ã§ã¯ãã¹ãããã«ã¼ãã¨switch-caseã¯åãã¹ã³ã¢ã«ãªãã¾ãããå¾è ã®ã»ããç解ããããã³ã¼ãã®ã¯ãã§ãã ã¾ããç°å¸¸ç³»ã®å¦çãifæã§æ·±ããã¹ãããä¸ã«æ¸ãããããã¡ã½ããã®å é ã§ã¨ã©ã¼ãã§ãã¯ãã¦returnãã¦ãã¾ãã»ããèªã¿ããããªãã¾ãã
ãã®ãããåå²ã§ã¯ãªãå¦çã®æµããé»å®³ããåæãå ç¹ãããã¨ã§ä¿å®æ§ã»å¯èªæ§ã®è©ä¾¡ã«ã¤ãªãããã¨ãããã®ãã³ã°ããã£ãè¤é度ã§ãã
ãã®ã³ã°ããã£ãè¤é度ãRiderã®ã¤ã³ã¹ãã¯ã·ã§ã³ã«çµã¿è¾¼ããã©ã°ã¤ã³ãCognitiveComplexityã§ãã
CognitiveComplexityãã©ã°ã¤ã³ã¯ãä¸è¬ã®ãã©ã°ã¤ã³ã¨åæ§ãPreferences... > Plugins ããã¤ã³ã¹ãã¼ã«ã»æå¹åã§ãã¾ãã æå¹åããã¨ãã¨ãã£ã¿ã®ã¡ã½ããå®ç¾©ã®ä¸ã«ã³ã°ããã£ãè¤é度ã表示ãããããã«ãªãã¾ãã
ä¸å³ã§ã¯ãã³ã°ããã£ãè¤é度ã23ãWariningã¨ãªããããå¤10ï¼ããã©ã«ãå¤ï¼ã«å¯¾ãã¦230%ã§ããã¨è¡¨ç¤ºããã¦ãã¾ããã¾ãã¯ãªãã¯ãããã¨ã§ã³ã¼ãå ã®å ç¹ãããåæï¼ä¸å³ã§ã¯ifæã®æ¨ªã«ãã"+1"ï¼ã表示ã§ãã¾ãã
ãããå¤ã¯ãPreferences > Languages & Frameworks > Cognitive Complexity ã§å¤æ´ã§ãã¾ã*2ã æ¢åããã¸ã§ã¯ãã§æ°å¤ã®é«ãã¡ã½ãããå¤ãå ´åã¯ããããå¤ãç·©ãã®å¤ã«ãã¦å°ãã¥ã¤ãªãã¡ã¯ã¿ãªã³ã°ãé²ãã¦ãããã¨ããããããã¾ãã
ããã¸ã§ã¯ãåä½ã®ã¤ã³ã¹ãã¯ã·ã§ã³å®è¡
ãããã®ãã©ã°ã¤ã³ãRiderã®ã¤ã³ã¹ãã¯ã·ã§ã³æ©è½ã«çµã¿è¾¼ã¾ãã¦ãã¾ãã®ã§ãCode > Inspect Code... ã§ã¹ã³ã¼ããæå®ãã¦å®è¡ã§ãã¾ãã
å®è¡çµæã¯"Inspection Results"ã¦ã£ã³ãã¦ã«è¡¨ç¤ºããã¾ãã
ä¸å³ã®ããã«ãWARNING > Potential Code Quality Issues ã®ä¸ã«ããµã¤ã¯ãããã£ãã¯è¤é度ã¨ã³ã°ããã£ãè¤é度ããããå¥ã«è¡¨ç¤ºããã¾ãã
- Element exceeds cyclomatic complexity
- Element exceeds Cognitive Complexity threshold
ãªãã両æ¹ã®ãããå¤ãè¶ ãã¦ããã¡ã½ããã¯ãã³ã°ããã£ãè¤é度ã«ã®ã¿è¡¨ç¤ºããã¾ãã
ã¾ã¨ã
åãç®çãæã¤2ã¤ã®ãã©ã°ã¤ã³ãç´¹ä»ãã¾ããããããã¤ãã®ããã¸ã§ã¯ãã§è¦æ¯ã¹ãæããã³ã°ããã£ãè¤é度ã®ã¿é©ç¨ãã¦ããã°ããããã§ãã
è¤é度ã®é«ãã¡ã½ããã¯ãæ¡ä»¶ã®çµã¿åãããå¤ããªããã¹ããããã¨ãé£ããããã°ãä½ãè¾¼ã¾ãããããªãã¾ãã ãã©ã°ã¤ã³ãå ¥ãã¦æ°å¤ãæ°ã«ãããã¨ã§ãé©åã«ã¡ã½ãããåå²ãã¦ä¿å®æ§ã®é«ãç¶æ ããã¼ãã§ããããã«ãªãã§ãããã
ãªããCyclomaticComplexityãã©ã°ã¤ã³ã®åå¨ã¯Unityã²ã¼ã éçºè ã®ã«ãã®Slackã§ç¥ãã¾ãããè²ã ã¨å½¹ç«ã¤æ å ±ã交æãã§ããã³ãã¥ããã£ã§ãã®ã§ãUnityã§ã²ã¼ã ãéçºãã¦ããæ¹ã¯å ¥ã£ã¦ã¿ã¦ã¯ãããã§ããããã
é¢é£è¨äº
é¢é£æ¸ç±
*1:SonarQubeã®éçºã»éå¶ä¼ç¤¾
*2:Riderããã³ãã©ã°ã¤ã³ã®ãã¼ã¸ã§ã³ã«ãã£ã¦å¤ãããã¨ãããã¾ããcognitiveã§æ¤ç´¢ãã¦è¦ã¤ãã¦ãã ãã