{"id":154377,"date":"2021-01-14T16:00:52","date_gmt":"2021-01-14T13:00:52","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/what-is-cyclomatic-complexity-measuring-code-quality-cloudsavvy-it\/"},"modified":"2021-01-14T16:00:52","modified_gmt":"2021-01-14T13:00:52","slug":"what-is-cyclomatic-complexity-measuring-code-quality-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/what-is-cyclomatic-complexity-measuring-code-quality-cloudsavvy-it\/","title":{"rendered":"#What Is Cyclomatic Complexity? Measuring Code Quality \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a3574958086c\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a3574958086c\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-cyclomatic-complexity-measuring-code-quality-cloudsavvy-it\/#What_Is_Cyclomatic_Complexity\" >What Is Cyclomatic Complexity?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-cyclomatic-complexity-measuring-code-quality-cloudsavvy-it\/#Is_Cyclomatic_Complexity_Useful\" >Is Cyclomatic Complexity Useful?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/what-is-cyclomatic-complexity-measuring-code-quality-cloudsavvy-it\/#Looking_For_Bad_Code\" >Looking For Bad Code<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#What Is Cyclomatic Complexity? Measuring Code Quality \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<figure id=\"attachment_9006\" style=\"width: 700px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-9006 size-full\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/563e5fc0745a8bdd51f1a1711439326c\/p\/uploads\/2021\/01\/6dbdfbd8.png\" alt=\"\" width=\"700\" height=\"300\" data-crediturl=\"https:\/\/www.shutterstock.com\/image-photo\/man-think-how-solve-mathematical-problem-766012597\" data-credittext=\"Shutterstock\/fran_kie\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><figcaption class=\"wp-caption-text\"><span class=\"imagecredit\"><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.shutterstock.com\/image-photo\/man-think-how-solve-mathematical-problem-766012597\">Shutterstock\/fran_kie<\/a><\/span><\/figcaption><\/figure>\n<p>Cyclomatic Complexity is a code metric that you can view in many IDEs like Visual Studio. While it\u2019s not an exact <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/sciencee\/\" data-internallinksmanager029f6b8e52c=\"5\" title=\"Science\" target=\"_blank\" rel=\"noopener\">science<\/a>, it allows you to get a <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/general\/\" data-internallinksmanager029f6b8e52c=\"3\" title=\"General\" target=\"_blank\" rel=\"noopener\">general<\/a> idea of the complexity of functions, classes, and namespaces, which can be useful when looking for code to refactor.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"What_Is_Cyclomatic_Complexity\"><\/span>What Is Cyclomatic Complexity?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Cyclomatic complexity basically measures how much your code branches. Every time there\u2019s an <code>if<\/code>\u00a0statement or other control block like a loop, cyclomatic complexity goes up, since the graph will look more and more like a tree.<\/p>\n<p>If you imagine your code as a <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/watch-movies-tv-seriess\/\" data-internallinksmanager029f6b8e52c=\"8\" title=\"Watch Movies &amp; TV Series\" target=\"_blank\" rel=\"noopener\">series<\/a> of actions (functions, method calls, variable assignments) connected via control flow, you\u2019ll get an abstract graph that you can use to better understand the complexity. For common control flows like <code>if<\/code>\u00a0statements and <code>for<\/code>\u00a0loops, the graphs look like this:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8997\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/fd742beb7b468270078f57ac519c3522\/p\/uploads\/2021\/01\/4bd4fcfd.png\" alt=\"\" width=\"700\" height=\"212\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The formula for it is simple; take the number of edges in the graph (the arrows connecting everything) and subtract the number of nodes in the graph (the actions themselves).<\/p>\n<pre>Complexity = Edges -- Nodes + 2<\/pre>\n<p>For example, this code has a cyclomatic complexity of one, since there aren\u2019t any branches, and it just calls WriteLine over and over. Since it\u2019s just perfectly linear code, the number of nodes will cancel out the number of edges, giving a cyclomatic complexity of one.<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"csharp\">\n<pre class=\"de1\"><span class=\"kw1\">public<\/span> <span class=\"kw4\">void<\/span> TestFunction<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span>\n<span class=\"br0\">{<\/span>\n    Console<span class=\"sy0\">.<\/span><span class=\"me1\">WriteLine<\/span><span class=\"br0\">(<\/span><span class=\"st0\">\"Test\"<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n    Console<span class=\"sy0\">.<\/span><span class=\"me1\">WriteLine<\/span><span class=\"br0\">(<\/span><span class=\"st0\">\"Test\"<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n    Console<span class=\"sy0\">.<\/span><span class=\"me1\">WriteLine<\/span><span class=\"br0\">(<\/span><span class=\"st0\">\"Test\"<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n    Console<span class=\"sy0\">.<\/span><span class=\"me1\">WriteLine<\/span><span class=\"br0\">(<\/span><span class=\"st0\">\"Test\"<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n    Console<span class=\"sy0\">.<\/span><span class=\"me1\">WriteLine<\/span><span class=\"br0\">(<\/span><span class=\"st0\">\"Test\"<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>For more complicated code with branches, the complexity will be higher. This code, which contains a <code>switch<\/code>\u00a0statement, has a complexity of 6, because there are many different paths the code can take. Each <code>case<\/code>\u00a0in the switch statement adds complexity, as it can lead to different outputs with differing inputs.<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"csharp\">\n<pre class=\"de1\"><span class=\"kw1\">public<\/span> <span class=\"kw4\">void<\/span> TestFunction<span class=\"br0\">(<\/span><span class=\"kw4\">string<\/span> arg<span class=\"br0\">)<\/span>\n<span class=\"br0\">{<\/span>\n    <span class=\"kw1\">switch<\/span><span class=\"br0\">(<\/span>arg<span class=\"br0\">)<\/span>\n    <span class=\"br0\">{<\/span>\n        <span class=\"kw1\">case<\/span> <span class=\"st0\">\"one\"<\/span><span class=\"sy0\">:<\/span>\n            Console<span class=\"sy0\">.<\/span><span class=\"me1\">WriteLine<\/span><span class=\"br0\">(<\/span><span class=\"st0\">\"Test\"<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n            <span class=\"kw1\">break<\/span><span class=\"sy0\">;<\/span>\n        <span class=\"kw1\">case<\/span> <span class=\"st0\">\"two\"<\/span><span class=\"sy0\">:<\/span>\n            Console<span class=\"sy0\">.<\/span><span class=\"me1\">WriteLine<\/span><span class=\"br0\">(<\/span><span class=\"st0\">\"Test\"<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n            <span class=\"kw1\">break<\/span><span class=\"sy0\">;<\/span>\n        <span class=\"kw1\">case<\/span> <span class=\"st0\">\"three\"<\/span><span class=\"sy0\">:<\/span>\n            Console<span class=\"sy0\">.<\/span><span class=\"me1\">WriteLine<\/span><span class=\"br0\">(<\/span><span class=\"st0\">\"Test\"<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n            <span class=\"kw1\">break<\/span><span class=\"sy0\">;<\/span>\n        <span class=\"kw1\">case<\/span> <span class=\"st0\">\"four\"<\/span><span class=\"sy0\">:<\/span>\n            Console<span class=\"sy0\">.<\/span><span class=\"me1\">WriteLine<\/span><span class=\"br0\">(<\/span><span class=\"st0\">\"Test\"<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n            <span class=\"kw1\">break<\/span><span class=\"sy0\">;<\/span>\n        <span class=\"kw1\">case<\/span> <span class=\"st0\">\"five\"<\/span><span class=\"sy0\">:<\/span>\n            Console<span class=\"sy0\">.<\/span><span class=\"me1\">WriteLine<\/span><span class=\"br0\">(<\/span><span class=\"st0\">\"Test\"<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n            <span class=\"kw1\">break<\/span><span class=\"sy0\">;<\/span>\n    <span class=\"br0\">}<\/span>\n\u00a0\n    Console<span class=\"sy0\">.<\/span><span class=\"me1\">WriteLine<\/span><span class=\"br0\">(<\/span><span class=\"st0\">\"Test\"<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Cyclomatic complexity is only calculated within the scope of the function. If a function calls another function that has a high cyclomatic complexity, it\u2019s only counted as a single node, and doesn\u2019t add anything to the caller, despite technically adding complexity to the program in a general sense.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Is_Cyclomatic_Complexity_Useful\"><\/span>Is Cyclomatic Complexity Useful?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Cyclomatic complexity isn\u2019t a perfect metric. It\u2019s a very basic metric and looks over the nuance of the code itself. Of course, you can still have terrible code with low complexity, or decent code with high complexity. But, in general, it\u2019s still quite useful for getting a roundabout sense of how complex a program is.<\/p>\n<p>For the most part, complexity under 6 to 8 is probably fine as long as the code itself is well formatted. Anything from 8-15 is questionable, and anything over 15 is probably not great. Anything over 25 is almost certainly a problem unless proven otherwise.<\/p>\n<p>While having a high cyclomatic complexity in any given function isn\u2019t the end of the world, it can be indicative of a larger problem. High complexity functions are harder to maintain and prone to more bugs, since there are more things to go wrong. And higher complexity functions directly lead to higher complexity unit tests, which can make the code hard to maintain in the long run due to the difficulty of testing.<\/p>\n<p>Visual Studio and other IDEs will calculate aggregate complexities of entire classes and namespaces, which can be useful for tracking down your most complex classes. You can sort by highest complexity and drill down into individual functions.<\/p>\n<p>Often, code review can take cyclomatic complexity into account, even flagging problematic functions that may need manual review. This can make it a very useful tool for maintaining a clean and orderly codebase.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Looking_For_Bad_Code\"><\/span>Looking For Bad Code<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Many IDEs, like Visual Studio, will have built in tools for calculating cyclomatic complexity and other code metrics for your entire codebase.<\/p>\n<p>To do so from Visual Studio, click Analyze &gt; Calculate Code Metrics &gt; For Solution.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9002\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/a2dafa0d41fd43f785d4f4c0fc8fec28\/p\/uploads\/2021\/01\/5ceea790.png\" alt=\"\" width=\"499\" height=\"194\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>This will bring up the \u201cCode Metrics\u201d panel that will show a breakdown for your solution. You can sort by complexity in descending order to view the most problematic namespaces.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9003\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/9bd8a9b9f4bea5e997e2cec3435fe672\/p\/uploads\/2021\/01\/bb28c42b.png\" alt=\"\" width=\"700\" height=\"220\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Alongside complexity, Visual Studio also presents a \u201cMaintainability Index\u201d which scores the method from 0-100 on high easily it can be maintained, as well as \u201cClass Coupling,\u201d which lists how many classes are referenced from that function or class.<\/p>\n<p>Here, Visual Studio pointed out a 400 line method of mine that scored a whopping 72 on the complexity scale as well as a 14\/100 on the maintainability index (presented as a yellow triangle danger sign), and references 65 different classes.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9004\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/2bbe173ed96e5dfb40e1079f1b4de1a0\/p\/uploads\/2021\/01\/5b72820c.png\" alt=\"\" width=\"700\" height=\"85\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>You may be sent directly into the five stages of grief at the result. \u201cBut, this is a really long coroutine that has a bunch of tasks to do!\u201d I tell myself, while trying to deny that the code I wrote is mathematically bad, to the point where Visual Studio throws a warning. A complexity of 72 is certainly in need of cleaning up.<\/p>\n<p>In this case, the fix was simple \u2014 the coroutine has a bunch of tasks to do, so I break those tasks up into smaller coroutines, and replace the main method with calls to subroutines. The overall code didn\u2019t change, and neither did the total complexity of the class itself, but now the main function isn\u2019t a 400 line monstrosity.\n<\/p><\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/8981\/what-is-cyclomatic-complexity-measuring-code-quality\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#What Is Cyclomatic Complexity? Measuring Code Quality \u2013 CloudSavvy IT&#8221; Shutterstock\/fran_kie Cyclomatic Complexity is a code metric that you can view in many IDEs like Visual Studio. While it\u2019s not an exact science, it allows you to get a general idea of the complexity of functions, classes, and namespaces, which can be useful when looking&#8230;<\/p>\n","protected":false},"author":1,"featured_media":154378,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/01\/6dbdfbd8.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-154377","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/154377","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=154377"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/154377\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/154378"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=154377"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=154377"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=154377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}