[{"data":1,"prerenderedAt":3262},["ShallowReactive",2],{"\u002Fblog\u002Fcyclomatic-complexity-vs-cognitive-complexity":3},{"id":4,"title":5,"body":6,"createdAt":3248,"description":3249,"extension":3250,"externalPost":3251,"githubLink":3251,"language":3252,"meta":3253,"navigation":561,"ogImage":3251,"path":3254,"robots":3251,"seo":3255,"stem":3256,"tags":3257,"__hash__":3261,"date":3248},"blog\u002Fblog\u002Fcyclomatic-complexity-vs-cognitive-complexity.md","Cyclomatic Complexity vs. Cognitive Complexity",{"type":7,"value":8,"toc":3228},"minimark",[9,14,23,26,45,48,59,62,73,84,88,98,101,104,107,112,115,118,121,128,134,140,144,149,172,176,198,208,223,232,236,248,251,254,304,307,322,326,333,337,369,375,379,382,401,405,409,475,478,484,587,590,597,688,694,701,798,801,810,934,940,944,1087,1090,1096,1204,1217,1221,1327,1330,1334,1456,1459,1463,1573,1576,1583,1722,1727,1731,1734,1737,1740,1744,1747,1750,1753,1756,1789,1800,1807,1810,1813,1817,1865,1868,1873,1948,1951,1955,2036,2042,2049,2154,2157,2163,2255,2261,2265,2388,2391,2398,2401,2405,2522,2525,2529,2617,2623,2627,2722,2725,2732,2909,2915,2920,3034,3043,3047,3050,3054,3109,3112,3116,3143,3154,3158,3161,3164,3168,3171,3175,3198,3202,3215,3218,3221,3224],[10,11,13],"h2",{"id":12},"what-does-complexity-mean-in-code","What does complexity mean in code?",[15,16,17,18,22],"p",{},"Contrary to what many people think, ",[19,20,21],"strong",{},"we don't write code for the machine, we write it for other developers",". At the end of the day, even if our code is complex, the machine will still understand and execute it.",[15,24,25],{},"For a product to be high quality, it isn't enough for the application to run. We also need to make sure that:",[27,28,29,33,36,39,42],"ul",{},[30,31,32],"li",{},"the logic is implemented correctly, which means testing the possible paths through the application",[30,34,35],{},"the code is easy to maintain and evolve, so other developers, or even we ourselves in the future, can understand what we wrote today",[30,37,38],{},"the application scales well",[30,40,41],{},"the application performs well enough for our users",[30,43,44],{},"and so on",[15,46,47],{},"There are many forms of complexity. If we don't keep them under control, they quietly turn into technical debt, which can have serious consequences:",[27,49,50,53,56],{},[30,51,52],{},"new features take longer to implement",[30,54,55],{},"onboarding new developers gets harder",[30,57,58],{},"bugs hide in the least obvious places",[15,60,61],{},"In practice, complexity has a direct impact on:",[27,63,64,67,70],{},[30,65,66],{},"development speed",[30,68,69],{},"bug count",[30,71,72],{},"the system's ability to evolve",[15,74,75,76,79,80,83],{},"There are several kinds of complexity, but this post focuses on two widely used metrics: ",[19,77,78],{},"cyclomatic complexity"," and ",[19,81,82],{},"cognitive complexity",".",[10,85,87],{"id":86},"cyclomatic-complexity","Cyclomatic complexity",[15,89,90,91,83],{},"The idea was introduced in 1976 by ",[92,93,97],"a",{"href":94,"rel":95},"https:\u002F\u002Fen.wikipedia.org\u002Fwiki\u002FCyclomatic_complexity",[96],"nofollow","Thomas J. McCabe, Sr.",[15,99,100],{},"Cyclomatic complexity measures the number of linearly independent execution paths through a function or method.",[15,102,103],{},"Think of a function as a road map. Cyclomatic complexity tells you how many distinct logical routes there are between the entry point and the exit point.",[15,105,106],{},"In basis-path testing, that value helps you estimate how many white-box tests are needed to exercise the main branches without having to enumerate every possible path.",[108,109,111],"h3",{"id":110},"how-do-you-measure-cyclomatic-complexity","How do you measure cyclomatic complexity?",[15,113,114],{},"There are several ways to measure it, from formal mathematical approaches to the more practical approach of counting control-flow decisions. For this post, I'll stick to the practical view, although the graph-based definitions are worth exploring separately.",[15,116,117],{},"Think about a method.",[15,119,120],{},"You always start with:",[27,122,123],{},[30,124,125],{},[19,126,127],{},"+1 (base value of the method)",[15,129,130,131,83],{},"Then add ",[19,132,133],{},"+1 for each decision that creates a new path",[135,136,137],"blockquote",{},[15,138,139],{},"Important note: depending on the tool (Sonar, linter, plugin, language), the exact count can vary slightly in specific cases.",[108,141,143],{"id":142},"what-usually-counts-as-1","What usually counts as +1",[145,146,148],"h4",{"id":147},"_1-control-flow-conditions","1. Control-flow conditions",[27,150,151,157,166],{},[30,152,153],{},[154,155,156],"code",{},"if",[30,158,159,162,163],{},[154,160,161],{},"else if"," \u002F ",[154,164,165],{},"elif",[30,167,168,171],{},[154,169,170],{},"else"," - doesn't count, because it doesn't create a new independent path",[145,173,175],{"id":174},"_2-loops","2. Loops",[27,177,178,183,188,193],{},[30,179,180],{},[154,181,182],{},"for",[30,184,185],{},[154,186,187],{},"while",[30,189,190],{},[154,191,192],{},"do while",[30,194,195],{},[154,196,197],{},"foreach",[145,199,201,202,162,205],{"id":200},"_3-switch-match","3. ",[154,203,204],{},"switch",[154,206,207],{},"match",[27,209,210,217],{},[30,211,212,213,216],{},"each ",[154,214,215],{},"case"," usually counts as +1",[30,218,219,222],{},[154,220,221],{},"default"," usually doesn't count",[15,224,225,226,228,229,231],{},"Some tools count the ",[154,227,204],{}," itself as 1 plus the number of cases. Others count only the ",[154,230,215],{}," branches.",[145,233,235],{"id":234},"_4-short-circuit-logical-operators","4. Short-circuit logical operators",[27,237,238,243],{},[30,239,240],{},[154,241,242],{},"&&",[30,244,245],{},[154,246,247],{},"||",[15,249,250],{},"Each logical operator inside a condition often counts as +1, though not all tools count them consistently.",[15,252,253],{},"Example:",[255,256,261],"pre",{"className":257,"code":258,"language":259,"meta":260,"style":260},"language-csharp shiki shiki-themes github-dark","if (a && b || c)\n{\n  \u002F\u002F ...\n}\n","csharp","",[154,262,263,285,291,298],{"__ignoreMap":260},[264,265,268,271,275,277,280,282],"span",{"class":266,"line":267},"line",1,[264,269,156],{"class":270},"snl16",[264,272,274],{"class":273},"s95oV"," (a ",[264,276,242],{"class":270},[264,278,279],{"class":273}," b ",[264,281,247],{"class":270},[264,283,284],{"class":273}," c)\n",[264,286,288],{"class":266,"line":287},2,[264,289,290],{"class":273},"{\n",[264,292,294],{"class":266,"line":293},3,[264,295,297],{"class":296},"sAwPA","  \u002F\u002F ...\n",[264,299,301],{"class":266,"line":300},4,[264,302,303],{"class":273},"}\n",[15,305,306],{},"Typical count:",[27,308,309,314,318],{},[30,310,311,313],{},[154,312,156],{}," - +1",[30,315,316,313],{},[154,317,242],{},[30,319,320,313],{},[154,321,247],{},[145,323,325],{"id":324},"_5-ternary-operator","5. Ternary operator",[27,327,328],{},[30,329,330,313],{},[154,331,332],{},"condition ? whenTrue : whenFalse",[145,334,336],{"id":335},"_6-exceptions-and-control-flow","6. Exceptions and control flow",[27,338,339,344,349],{},[30,340,212,341,313],{},[154,342,343],{},"catch",[30,345,346,222],{},[154,347,348],{},"finally",[30,350,351,354,355,354,358,361,362,365,366],{},[154,352,353],{},"return",", ",[154,356,357],{},"break",[154,359,360],{},"continue",", and ",[154,363,364],{},"goto"," usually ",[19,367,368],{},"don't count directly",[15,370,371,372,374],{},"Why? Because these constructs don't create a new independent logical path on their own. They either handle an existing path (",[154,373,343],{},") or simply terminate or redirect control flow.",[145,376,378],{"id":377},"what-usually-doesnt-count","What usually doesn't count",[15,380,381],{},"Besides the items already mentioned, this is where people often get tripped up:",[27,383,384,388,391,398],{},[30,385,386],{},[154,387,170],{},[30,389,390],{},"method calls",[30,392,393,394,397],{},"assignments (",[154,395,396],{},"=",")",[30,399,400],{},"LINQ\u002Fstreams, unless there is relevant internal conditional logic that affects the count",[108,402,404],{"id":403},"practical-examples","Practical examples",[145,406,408],{"id":407},"example-1-simple-function","Example 1: Simple function",[255,410,412],{"className":257,"code":411,"language":259,"meta":260,"style":260},"int Sum(int a, int b)\n{\n  \u002F\u002F +1 (base)\n  \u002F\u002F Total: 1\n  return a + b;\n}\n",[154,413,414,441,445,450,455,470],{"__ignoreMap":260},[264,415,416,419,423,426,428,431,433,435,438],{"class":266,"line":267},[264,417,418],{"class":270},"int",[264,420,422],{"class":421},"svObZ"," Sum",[264,424,425],{"class":273},"(",[264,427,418],{"class":270},[264,429,430],{"class":421}," a",[264,432,354],{"class":273},[264,434,418],{"class":270},[264,436,437],{"class":421}," b",[264,439,440],{"class":273},")\n",[264,442,443],{"class":266,"line":287},[264,444,290],{"class":273},[264,446,447],{"class":266,"line":293},[264,448,449],{"class":296},"  \u002F\u002F +1 (base)\n",[264,451,452],{"class":266,"line":300},[264,453,454],{"class":296},"  \u002F\u002F Total: 1\n",[264,456,458,461,464,467],{"class":266,"line":457},5,[264,459,460],{"class":270},"  return",[264,462,463],{"class":273}," a ",[264,465,466],{"class":270},"+",[264,468,469],{"class":273}," b;\n",[264,471,473],{"class":266,"line":472},6,[264,474,303],{"class":273},[15,476,477],{},"The flow is completely linear, with no decisions or branches, so cyclomatic complexity stays at the minimum value.",[145,479,481,482],{"id":480},"example-2-simple-if","Example 2: Simple ",[154,483,156],{},[255,485,487],{"className":257,"code":486,"language":259,"meta":260,"style":260},"int GetSign(int number)\n{\n  \u002F\u002F +1 (base)\n  if (number > 0) \u002F\u002F +1 (if)\n  {\n    return 1;\n  }\n\n  \u002F\u002F Total: 2\n  return -1;\n}\n",[154,488,489,505,509,513,534,539,550,556,563,569,582],{"__ignoreMap":260},[264,490,491,493,496,498,500,503],{"class":266,"line":267},[264,492,418],{"class":270},[264,494,495],{"class":421}," GetSign",[264,497,425],{"class":273},[264,499,418],{"class":270},[264,501,502],{"class":421}," number",[264,504,440],{"class":273},[264,506,507],{"class":266,"line":287},[264,508,290],{"class":273},[264,510,511],{"class":266,"line":293},[264,512,449],{"class":296},[264,514,515,518,521,524,528,531],{"class":266,"line":300},[264,516,517],{"class":270},"  if",[264,519,520],{"class":273}," (number ",[264,522,523],{"class":270},">",[264,525,527],{"class":526},"sDLfK"," 0",[264,529,530],{"class":273},") ",[264,532,533],{"class":296},"\u002F\u002F +1 (if)\n",[264,535,536],{"class":266,"line":457},[264,537,538],{"class":273},"  {\n",[264,540,541,544,547],{"class":266,"line":472},[264,542,543],{"class":270},"    return",[264,545,546],{"class":526}," 1",[264,548,549],{"class":273},";\n",[264,551,553],{"class":266,"line":552},7,[264,554,555],{"class":273},"  }\n",[264,557,559],{"class":266,"line":558},8,[264,560,562],{"emptyLinePlaceholder":561},true,"\n",[264,564,566],{"class":266,"line":565},9,[264,567,568],{"class":296},"  \u002F\u002F Total: 2\n",[264,570,572,574,577,580],{"class":266,"line":571},10,[264,573,460],{"class":270},[264,575,576],{"class":270}," -",[264,578,579],{"class":526},"1",[264,581,549],{"class":273},[264,583,585],{"class":266,"line":584},11,[264,586,303],{"class":273},[15,588,589],{},"This creates two possible execution paths: one when the condition is true and one when it is false, so cyclomatic complexity rises to 2.",[145,591,593,594],{"id":592},"example-3-ifelse","Example 3: ",[154,595,596],{},"if\u002Felse",[255,598,600],{"className":257,"code":599,"language":259,"meta":260,"style":260},"int GetSignWithElse(int number)\n{\n  \u002F\u002F +1 (base)\n  if (number > 0) \u002F\u002F +1 (if)\n  {\n    return 1;\n  }\n  else\n  {\n    return -1;\n  }\n  \u002F\u002F Total: 2\n}\n",[154,601,602,617,621,625,639,643,651,655,660,664,674,678,683],{"__ignoreMap":260},[264,603,604,606,609,611,613,615],{"class":266,"line":267},[264,605,418],{"class":270},[264,607,608],{"class":421}," GetSignWithElse",[264,610,425],{"class":273},[264,612,418],{"class":270},[264,614,502],{"class":421},[264,616,440],{"class":273},[264,618,619],{"class":266,"line":287},[264,620,290],{"class":273},[264,622,623],{"class":266,"line":293},[264,624,449],{"class":296},[264,626,627,629,631,633,635,637],{"class":266,"line":300},[264,628,517],{"class":270},[264,630,520],{"class":273},[264,632,523],{"class":270},[264,634,527],{"class":526},[264,636,530],{"class":273},[264,638,533],{"class":296},[264,640,641],{"class":266,"line":457},[264,642,538],{"class":273},[264,644,645,647,649],{"class":266,"line":472},[264,646,543],{"class":270},[264,648,546],{"class":526},[264,650,549],{"class":273},[264,652,653],{"class":266,"line":552},[264,654,555],{"class":273},[264,656,657],{"class":266,"line":558},[264,658,659],{"class":270},"  else\n",[264,661,662],{"class":266,"line":565},[264,663,538],{"class":273},[264,665,666,668,670,672],{"class":266,"line":571},[264,667,543],{"class":270},[264,669,576],{"class":270},[264,671,579],{"class":526},[264,673,549],{"class":273},[264,675,676],{"class":266,"line":584},[264,677,555],{"class":273},[264,679,681],{"class":266,"line":680},12,[264,682,568],{"class":296},[264,684,686],{"class":266,"line":685},13,[264,687,303],{"class":273},[15,689,690,691,693],{},"The ",[154,692,170],{}," doesn't add a new independent path. It simply makes the complementary branch explicit, so the complexity stays the same as in the previous example.",[145,695,697,698,700],{"id":696},"example-4-if-logical-operator","Example 4: ",[154,699,156],{}," + logical operator",[255,702,704],{"className":257,"code":703,"language":259,"meta":260,"style":260},"bool IsValid(int age, bool hasPermission)\n{\n  \u002F\u002F +1 (base)\n  if (age > 18 && hasPermission) \u002F\u002F +1 (if), +1 (&&)\n  {\n    return true;\n  }\n\n  \u002F\u002F Total: 3\n  return false;\n}\n",[154,705,706,730,734,738,759,763,772,776,780,785,794],{"__ignoreMap":260},[264,707,708,711,714,716,718,721,723,725,728],{"class":266,"line":267},[264,709,710],{"class":270},"bool",[264,712,713],{"class":421}," IsValid",[264,715,425],{"class":273},[264,717,418],{"class":270},[264,719,720],{"class":421}," age",[264,722,354],{"class":273},[264,724,710],{"class":270},[264,726,727],{"class":421}," hasPermission",[264,729,440],{"class":273},[264,731,732],{"class":266,"line":287},[264,733,290],{"class":273},[264,735,736],{"class":266,"line":293},[264,737,449],{"class":296},[264,739,740,742,745,747,750,753,756],{"class":266,"line":300},[264,741,517],{"class":270},[264,743,744],{"class":273}," (age ",[264,746,523],{"class":270},[264,748,749],{"class":526}," 18",[264,751,752],{"class":270}," &&",[264,754,755],{"class":273}," hasPermission) ",[264,757,758],{"class":296},"\u002F\u002F +1 (if), +1 (&&)\n",[264,760,761],{"class":266,"line":457},[264,762,538],{"class":273},[264,764,765,767,770],{"class":266,"line":472},[264,766,543],{"class":270},[264,768,769],{"class":526}," true",[264,771,549],{"class":273},[264,773,774],{"class":266,"line":552},[264,775,555],{"class":273},[264,777,778],{"class":266,"line":558},[264,779,562],{"emptyLinePlaceholder":561},[264,781,782],{"class":266,"line":565},[264,783,784],{"class":296},"  \u002F\u002F Total: 3\n",[264,786,787,789,792],{"class":266,"line":571},[264,788,460],{"class":270},[264,790,791],{"class":526}," false",[264,793,549],{"class":273},[264,795,796],{"class":266,"line":584},[264,797,303],{"class":273},[15,799,800],{},"The logical operator inside the condition introduces an extra decision, so this rises to 3 even though the structure still looks simple.",[145,802,804,805,807,808],{"id":803},"example-5-if-else-if","Example 5: ",[154,806,156],{}," + ",[154,809,161],{},[255,811,813],{"className":257,"code":812,"language":259,"meta":260,"style":260},"string GetCategory(int score)\n{\n  \u002F\u002F +1 (base)\n  if (score > 90) \u002F\u002F +1 (if)\n  {\n    return \"A\";\n  }\n  else if (score > 70) \u002F\u002F +1 (else if)\n  {\n    return \"B\";\n  }\n\n  \u002F\u002F Total: 3\n  return \"C\";\n}\n",[154,814,815,832,836,840,856,860,870,874,894,898,907,911,915,919,929],{"__ignoreMap":260},[264,816,817,820,823,825,827,830],{"class":266,"line":267},[264,818,819],{"class":270},"string",[264,821,822],{"class":421}," GetCategory",[264,824,425],{"class":273},[264,826,418],{"class":270},[264,828,829],{"class":421}," score",[264,831,440],{"class":273},[264,833,834],{"class":266,"line":287},[264,835,290],{"class":273},[264,837,838],{"class":266,"line":293},[264,839,449],{"class":296},[264,841,842,844,847,849,852,854],{"class":266,"line":300},[264,843,517],{"class":270},[264,845,846],{"class":273}," (score ",[264,848,523],{"class":270},[264,850,851],{"class":526}," 90",[264,853,530],{"class":273},[264,855,533],{"class":296},[264,857,858],{"class":266,"line":457},[264,859,538],{"class":273},[264,861,862,864,868],{"class":266,"line":472},[264,863,543],{"class":270},[264,865,867],{"class":866},"sU2Wk"," \"A\"",[264,869,549],{"class":273},[264,871,872],{"class":266,"line":552},[264,873,555],{"class":273},[264,875,876,879,882,884,886,889,891],{"class":266,"line":558},[264,877,878],{"class":270},"  else",[264,880,881],{"class":270}," if",[264,883,846],{"class":273},[264,885,523],{"class":270},[264,887,888],{"class":526}," 70",[264,890,530],{"class":273},[264,892,893],{"class":296},"\u002F\u002F +1 (else if)\n",[264,895,896],{"class":266,"line":565},[264,897,538],{"class":273},[264,899,900,902,905],{"class":266,"line":571},[264,901,543],{"class":270},[264,903,904],{"class":866}," \"B\"",[264,906,549],{"class":273},[264,908,909],{"class":266,"line":584},[264,910,555],{"class":273},[264,912,913],{"class":266,"line":680},[264,914,562],{"emptyLinePlaceholder":561},[264,916,917],{"class":266,"line":685},[264,918,784],{"class":296},[264,920,922,924,927],{"class":266,"line":921},14,[264,923,460],{"class":270},[264,925,926],{"class":866}," \"C\"",[264,928,549],{"class":273},[264,930,932],{"class":266,"line":931},15,[264,933,303],{"class":273},[15,935,936,937,939],{},"Each ",[154,938,161],{}," is its own decision and adds another independent path, so the complexity increases step by step.",[145,941,943],{"id":942},"example-6-loop-with-an-internal-condition","Example 6: Loop with an internal condition",[255,945,947],{"className":257,"code":946,"language":259,"meta":260,"style":260},"int CountAdults(List\u003Cint> ages)\n{\n  \u002F\u002F +1 (base)\n  var count = 0;\n\n  foreach (var age in ages) \u002F\u002F +1 (foreach)\n  {\n    if (age >= 18) \u002F\u002F +1 (if)\n    {\n      count++;\n    }\n  }\n\n  \u002F\u002F Total: 3\n  return count;\n}\n",[154,948,949,974,978,982,997,1001,1023,1027,1043,1048,1058,1063,1067,1071,1075,1082],{"__ignoreMap":260},[264,950,951,953,956,958,961,964,966,969,972],{"class":266,"line":267},[264,952,418],{"class":270},[264,954,955],{"class":421}," CountAdults",[264,957,425],{"class":273},[264,959,960],{"class":421},"List",[264,962,963],{"class":273},"\u003C",[264,965,418],{"class":270},[264,967,968],{"class":273},"> ",[264,970,971],{"class":421},"ages",[264,973,440],{"class":273},[264,975,976],{"class":266,"line":287},[264,977,290],{"class":273},[264,979,980],{"class":266,"line":293},[264,981,449],{"class":296},[264,983,984,987,990,993,995],{"class":266,"line":300},[264,985,986],{"class":270},"  var",[264,988,989],{"class":421}," count",[264,991,992],{"class":270}," =",[264,994,527],{"class":526},[264,996,549],{"class":273},[264,998,999],{"class":266,"line":457},[264,1000,562],{"emptyLinePlaceholder":561},[264,1002,1003,1006,1009,1012,1014,1017,1020],{"class":266,"line":472},[264,1004,1005],{"class":270},"  foreach",[264,1007,1008],{"class":273}," (",[264,1010,1011],{"class":270},"var",[264,1013,720],{"class":421},[264,1015,1016],{"class":270}," in",[264,1018,1019],{"class":273}," ages) ",[264,1021,1022],{"class":296},"\u002F\u002F +1 (foreach)\n",[264,1024,1025],{"class":266,"line":552},[264,1026,538],{"class":273},[264,1028,1029,1032,1034,1037,1039,1041],{"class":266,"line":558},[264,1030,1031],{"class":270},"    if",[264,1033,744],{"class":273},[264,1035,1036],{"class":270},">=",[264,1038,749],{"class":526},[264,1040,530],{"class":273},[264,1042,533],{"class":296},[264,1044,1045],{"class":266,"line":565},[264,1046,1047],{"class":273},"    {\n",[264,1049,1050,1053,1056],{"class":266,"line":571},[264,1051,1052],{"class":273},"      count",[264,1054,1055],{"class":270},"++",[264,1057,549],{"class":273},[264,1059,1060],{"class":266,"line":584},[264,1061,1062],{"class":273},"    }\n",[264,1064,1065],{"class":266,"line":680},[264,1066,555],{"class":273},[264,1068,1069],{"class":266,"line":685},[264,1070,562],{"emptyLinePlaceholder":561},[264,1072,1073],{"class":266,"line":921},[264,1074,784],{"class":296},[264,1076,1077,1079],{"class":266,"line":931},[264,1078,460],{"class":270},[264,1080,1081],{"class":273}," count;\n",[264,1083,1085],{"class":266,"line":1084},16,[264,1086,303],{"class":273},[15,1088,1089],{},"The loop introduces a decision point: keep iterating or exit. That adds another possible path and brings the complexity to 3.",[145,1091,1093,1094],{"id":1092},"example-7-switch","Example 7: ",[154,1095,204],{},[255,1097,1099],{"className":257,"code":1098,"language":259,"meta":260,"style":260},"string GetDayType(int day)\n{\n  \u002F\u002F +1 (base)\n  switch (day)\n  {\n    case 1: \u002F\u002F +1 (case)\n    case 7: \u002F\u002F +1 (case)\n      return \"Weekend\";\n    default:\n      return \"Weekday\";\n  }\n\n  \u002F\u002F Total: 3\n}\n",[154,1100,1101,1117,1121,1125,1133,1137,1150,1161,1171,1179,1188,1192,1196,1200],{"__ignoreMap":260},[264,1102,1103,1105,1108,1110,1112,1115],{"class":266,"line":267},[264,1104,819],{"class":270},[264,1106,1107],{"class":421}," GetDayType",[264,1109,425],{"class":273},[264,1111,418],{"class":270},[264,1113,1114],{"class":421}," day",[264,1116,440],{"class":273},[264,1118,1119],{"class":266,"line":287},[264,1120,290],{"class":273},[264,1122,1123],{"class":266,"line":293},[264,1124,449],{"class":296},[264,1126,1127,1130],{"class":266,"line":300},[264,1128,1129],{"class":270},"  switch",[264,1131,1132],{"class":273}," (day)\n",[264,1134,1135],{"class":266,"line":457},[264,1136,538],{"class":273},[264,1138,1139,1142,1144,1147],{"class":266,"line":472},[264,1140,1141],{"class":270},"    case",[264,1143,546],{"class":526},[264,1145,1146],{"class":273},": ",[264,1148,1149],{"class":296},"\u002F\u002F +1 (case)\n",[264,1151,1152,1154,1157,1159],{"class":266,"line":552},[264,1153,1141],{"class":270},[264,1155,1156],{"class":526}," 7",[264,1158,1146],{"class":273},[264,1160,1149],{"class":296},[264,1162,1163,1166,1169],{"class":266,"line":558},[264,1164,1165],{"class":270},"      return",[264,1167,1168],{"class":866}," \"Weekend\"",[264,1170,549],{"class":273},[264,1172,1173,1176],{"class":266,"line":565},[264,1174,1175],{"class":270},"    default",[264,1177,1178],{"class":273},":\n",[264,1180,1181,1183,1186],{"class":266,"line":571},[264,1182,1165],{"class":270},[264,1184,1185],{"class":866}," \"Weekday\"",[264,1187,549],{"class":273},[264,1189,1190],{"class":266,"line":584},[264,1191,555],{"class":273},[264,1193,1194],{"class":266,"line":680},[264,1195,562],{"emptyLinePlaceholder":561},[264,1197,1198],{"class":266,"line":685},[264,1199,784],{"class":296},[264,1201,1202],{"class":266,"line":921},[264,1203,303],{"class":273},[15,1205,936,1206,1208,1209,1211,1212,1214,1215,83],{},[154,1207,215],{}," represents an alternative execution path, and ",[154,1210,221],{}," works as a fallback similar to ",[154,1213,170],{},". With this counting approach, the total is 3, although some tools apply slightly different rules to ",[154,1216,204],{},[145,1218,1220],{"id":1219},"example-8-nested-conditions","Example 8: Nested conditions",[255,1222,1224],{"className":257,"code":1223,"language":259,"meta":260,"style":260},"bool CanAccessNested(User user)\n{\n  \u002F\u002F +1 (base)\n  if (user != null) \u002F\u002F +1 (if)\n  {\n    if (user.IsActive && user.HasPermission) \u002F\u002F +1 (if), +1 (&&)\n    {\n      return true;\n    }\n  }\n\n  \u002F\u002F Total: 4\n  return false;\n}\n",[154,1225,1226,1243,1247,1251,1268,1272,1286,1290,1298,1302,1306,1310,1315,1323],{"__ignoreMap":260},[264,1227,1228,1230,1233,1235,1238,1241],{"class":266,"line":267},[264,1229,710],{"class":270},[264,1231,1232],{"class":421}," CanAccessNested",[264,1234,425],{"class":273},[264,1236,1237],{"class":421},"User",[264,1239,1240],{"class":421}," user",[264,1242,440],{"class":273},[264,1244,1245],{"class":266,"line":287},[264,1246,290],{"class":273},[264,1248,1249],{"class":266,"line":293},[264,1250,449],{"class":296},[264,1252,1253,1255,1258,1261,1264,1266],{"class":266,"line":300},[264,1254,517],{"class":270},[264,1256,1257],{"class":273}," (user ",[264,1259,1260],{"class":270},"!=",[264,1262,1263],{"class":526}," null",[264,1265,530],{"class":273},[264,1267,533],{"class":296},[264,1269,1270],{"class":266,"line":457},[264,1271,538],{"class":273},[264,1273,1274,1276,1279,1281,1284],{"class":266,"line":472},[264,1275,1031],{"class":270},[264,1277,1278],{"class":273}," (user.IsActive ",[264,1280,242],{"class":270},[264,1282,1283],{"class":273}," user.HasPermission) ",[264,1285,758],{"class":296},[264,1287,1288],{"class":266,"line":552},[264,1289,1047],{"class":273},[264,1291,1292,1294,1296],{"class":266,"line":558},[264,1293,1165],{"class":270},[264,1295,769],{"class":526},[264,1297,549],{"class":273},[264,1299,1300],{"class":266,"line":565},[264,1301,1062],{"class":273},[264,1303,1304],{"class":266,"line":571},[264,1305,555],{"class":273},[264,1307,1308],{"class":266,"line":584},[264,1309,562],{"emptyLinePlaceholder":561},[264,1311,1312],{"class":266,"line":680},[264,1313,1314],{"class":296},"  \u002F\u002F Total: 4\n",[264,1316,1317,1319,1321],{"class":266,"line":685},[264,1318,460],{"class":270},[264,1320,791],{"class":526},[264,1322,549],{"class":273},[264,1324,1325],{"class":266,"line":921},[264,1326,303],{"class":273},[15,1328,1329],{},"Nested conditions add more execution paths, so the count keeps rising even in a short method.",[145,1331,1333],{"id":1332},"example-9-early-return","Example 9: Early return",[255,1335,1337],{"className":257,"code":1336,"language":259,"meta":260,"style":260},"bool CanAccessGuardClause(User user)\n{\n  \u002F\u002F +1 (base)\n  if (user == null) \u002F\u002F +1 (if)\n  {\n    return false;\n  }\n\n  if (!user.IsActive || !user.HasPermission) \u002F\u002F +1 (if), +1 (||)\n  {\n    return false;\n  }\n\n  \u002F\u002F Total: 4\n  return true;\n}\n",[154,1338,1339,1354,1358,1362,1377,1381,1389,1393,1397,1420,1424,1432,1436,1440,1444,1452],{"__ignoreMap":260},[264,1340,1341,1343,1346,1348,1350,1352],{"class":266,"line":267},[264,1342,710],{"class":270},[264,1344,1345],{"class":421}," CanAccessGuardClause",[264,1347,425],{"class":273},[264,1349,1237],{"class":421},[264,1351,1240],{"class":421},[264,1353,440],{"class":273},[264,1355,1356],{"class":266,"line":287},[264,1357,290],{"class":273},[264,1359,1360],{"class":266,"line":293},[264,1361,449],{"class":296},[264,1363,1364,1366,1368,1371,1373,1375],{"class":266,"line":300},[264,1365,517],{"class":270},[264,1367,1257],{"class":273},[264,1369,1370],{"class":270},"==",[264,1372,1263],{"class":526},[264,1374,530],{"class":273},[264,1376,533],{"class":296},[264,1378,1379],{"class":266,"line":457},[264,1380,538],{"class":273},[264,1382,1383,1385,1387],{"class":266,"line":472},[264,1384,543],{"class":270},[264,1386,791],{"class":526},[264,1388,549],{"class":273},[264,1390,1391],{"class":266,"line":552},[264,1392,555],{"class":273},[264,1394,1395],{"class":266,"line":558},[264,1396,562],{"emptyLinePlaceholder":561},[264,1398,1399,1401,1403,1406,1409,1411,1414,1417],{"class":266,"line":565},[264,1400,517],{"class":270},[264,1402,1008],{"class":273},[264,1404,1405],{"class":270},"!",[264,1407,1408],{"class":273},"user.IsActive ",[264,1410,247],{"class":270},[264,1412,1413],{"class":270}," !",[264,1415,1416],{"class":273},"user.HasPermission) ",[264,1418,1419],{"class":296},"\u002F\u002F +1 (if), +1 (||)\n",[264,1421,1422],{"class":266,"line":571},[264,1423,538],{"class":273},[264,1425,1426,1428,1430],{"class":266,"line":584},[264,1427,543],{"class":270},[264,1429,791],{"class":526},[264,1431,549],{"class":273},[264,1433,1434],{"class":266,"line":680},[264,1435,555],{"class":273},[264,1437,1438],{"class":266,"line":685},[264,1439,562],{"emptyLinePlaceholder":561},[264,1441,1442],{"class":266,"line":921},[264,1443,1314],{"class":296},[264,1445,1446,1448,1450],{"class":266,"line":931},[264,1447,460],{"class":270},[264,1449,769],{"class":526},[264,1451,549],{"class":273},[264,1453,1454],{"class":266,"line":1084},[264,1455,303],{"class":273},[15,1457,1458],{},"Cyclomatic complexity stays at 4, but the logic becomes flatter because it uses guard clauses.",[145,1460,1462],{"id":1461},"example-10-compound-condition","Example 10: Compound condition",[255,1464,1466],{"className":257,"code":1465,"language":259,"meta":260,"style":260},"bool HasAccess(bool a, bool b, bool c, bool d)\n{\n  \u002F\u002F +1 (base)\n  if ((a && b) || (c && d)) \u002F\u002F +1 (if), +1 (&&), +1 (||), +1 (&&)\n  {\n    return true;\n  }\n\n  \u002F\u002F Total: 5\n  return false;\n}\n",[154,1467,1468,1503,1507,1511,1536,1540,1548,1552,1556,1561,1569],{"__ignoreMap":260},[264,1469,1470,1472,1475,1477,1479,1481,1483,1485,1487,1489,1491,1494,1496,1498,1501],{"class":266,"line":267},[264,1471,710],{"class":270},[264,1473,1474],{"class":421}," HasAccess",[264,1476,425],{"class":273},[264,1478,710],{"class":270},[264,1480,430],{"class":421},[264,1482,354],{"class":273},[264,1484,710],{"class":270},[264,1486,437],{"class":421},[264,1488,354],{"class":273},[264,1490,710],{"class":270},[264,1492,1493],{"class":421}," c",[264,1495,354],{"class":273},[264,1497,710],{"class":270},[264,1499,1500],{"class":421}," d",[264,1502,440],{"class":273},[264,1504,1505],{"class":266,"line":287},[264,1506,290],{"class":273},[264,1508,1509],{"class":266,"line":293},[264,1510,449],{"class":296},[264,1512,1513,1515,1518,1520,1523,1525,1528,1530,1533],{"class":266,"line":300},[264,1514,517],{"class":270},[264,1516,1517],{"class":273}," ((a ",[264,1519,242],{"class":270},[264,1521,1522],{"class":273}," b) ",[264,1524,247],{"class":270},[264,1526,1527],{"class":273}," (c ",[264,1529,242],{"class":270},[264,1531,1532],{"class":273}," d)) ",[264,1534,1535],{"class":296},"\u002F\u002F +1 (if), +1 (&&), +1 (||), +1 (&&)\n",[264,1537,1538],{"class":266,"line":457},[264,1539,538],{"class":273},[264,1541,1542,1544,1546],{"class":266,"line":472},[264,1543,543],{"class":270},[264,1545,769],{"class":526},[264,1547,549],{"class":273},[264,1549,1550],{"class":266,"line":552},[264,1551,555],{"class":273},[264,1553,1554],{"class":266,"line":558},[264,1555,562],{"emptyLinePlaceholder":561},[264,1557,1558],{"class":266,"line":565},[264,1559,1560],{"class":296},"  \u002F\u002F Total: 5\n",[264,1562,1563,1565,1567],{"class":266,"line":571},[264,1564,460],{"class":270},[264,1566,791],{"class":526},[264,1568,549],{"class":273},[264,1570,1571],{"class":266,"line":584},[264,1572,303],{"class":273},[15,1574,1575],{},"The density of logical operators makes the count climb quickly, which shows how expensive compound conditions can become even in short functions.",[145,1577,1579,1580],{"id":1578},"example-11-trycatch","Example 11: ",[154,1581,1582],{},"try\u002Fcatch",[255,1584,1586],{"className":257,"code":1585,"language":259,"meta":260,"style":260},"string ParseInput(string input)\n{\n  \u002F\u002F +1 (base)\n  try\n  {\n    return int.Parse(input).ToString();\n  }\n  catch (FormatException) \u002F\u002F +1 (catch)\n  {\n    return \"Invalid format\";\n  }\n  catch (OverflowException) \u002F\u002F +1 (catch)\n  {\n    return \"Number too large\";\n  }\n\n  \u002F\u002F Total: 3\n}\n",[154,1587,1588,1604,1608,1612,1617,1621,1642,1646,1661,1665,1674,1678,1691,1695,1704,1708,1712,1717],{"__ignoreMap":260},[264,1589,1590,1592,1595,1597,1599,1602],{"class":266,"line":267},[264,1591,819],{"class":270},[264,1593,1594],{"class":421}," ParseInput",[264,1596,425],{"class":273},[264,1598,819],{"class":270},[264,1600,1601],{"class":421}," input",[264,1603,440],{"class":273},[264,1605,1606],{"class":266,"line":287},[264,1607,290],{"class":273},[264,1609,1610],{"class":266,"line":293},[264,1611,449],{"class":296},[264,1613,1614],{"class":266,"line":300},[264,1615,1616],{"class":270},"  try\n",[264,1618,1619],{"class":266,"line":457},[264,1620,538],{"class":273},[264,1622,1623,1625,1628,1630,1633,1636,1639],{"class":266,"line":472},[264,1624,543],{"class":270},[264,1626,1627],{"class":270}," int",[264,1629,83],{"class":273},[264,1631,1632],{"class":421},"Parse",[264,1634,1635],{"class":273},"(input).",[264,1637,1638],{"class":421},"ToString",[264,1640,1641],{"class":273},"();\n",[264,1643,1644],{"class":266,"line":552},[264,1645,555],{"class":273},[264,1647,1648,1651,1653,1656,1658],{"class":266,"line":558},[264,1649,1650],{"class":270},"  catch",[264,1652,1008],{"class":273},[264,1654,1655],{"class":421},"FormatException",[264,1657,530],{"class":273},[264,1659,1660],{"class":296},"\u002F\u002F +1 (catch)\n",[264,1662,1663],{"class":266,"line":565},[264,1664,538],{"class":273},[264,1666,1667,1669,1672],{"class":266,"line":571},[264,1668,543],{"class":270},[264,1670,1671],{"class":866}," \"Invalid format\"",[264,1673,549],{"class":273},[264,1675,1676],{"class":266,"line":584},[264,1677,555],{"class":273},[264,1679,1680,1682,1684,1687,1689],{"class":266,"line":680},[264,1681,1650],{"class":270},[264,1683,1008],{"class":273},[264,1685,1686],{"class":421},"OverflowException",[264,1688,530],{"class":273},[264,1690,1660],{"class":296},[264,1692,1693],{"class":266,"line":685},[264,1694,538],{"class":273},[264,1696,1697,1699,1702],{"class":266,"line":921},[264,1698,543],{"class":270},[264,1700,1701],{"class":866}," \"Number too large\"",[264,1703,549],{"class":273},[264,1705,1706],{"class":266,"line":931},[264,1707,555],{"class":273},[264,1709,1710],{"class":266,"line":1084},[264,1711,562],{"emptyLinePlaceholder":561},[264,1713,1715],{"class":266,"line":1714},17,[264,1716,784],{"class":296},[264,1718,1720],{"class":266,"line":1719},18,[264,1721,303],{"class":273},[15,1723,936,1724,1726],{},[154,1725,343],{}," represents an alternative control-flow path for a specific failure, so multiple handlers naturally increase cyclomatic complexity.",[108,1728,1730],{"id":1729},"limitations-of-cyclomatic-complexity","Limitations of cyclomatic complexity",[15,1732,1733],{},"This metric is useful, but it doesn't tell the whole story.",[15,1735,1736],{},"Its biggest limitation is that it doesn't penalise nesting very well.",[15,1738,1739],{},"That's why two pieces of code can have the same cyclomatic complexity and still feel very different to read, as in examples 8 and 9.",[10,1741,1743],{"id":1742},"cognitive-complexity","Cognitive complexity",[15,1745,1746],{},"This metric isn't about lines of code. It's about the mental effort needed to understand a piece of code.",[15,1748,1749],{},"Cognitive complexity measures how hard it is for a person to follow the flow of a function or method. The modern formulation most people know was popularised by SonarSource, and it is more heuristic and tool-dependent than cyclomatic complexity.",[15,1751,1752],{},"Unlike cyclomatic complexity, cognitive complexity starts at 0. The score increases when linear flow is interrupted, and it penalises nesting progressively. That better reflects the cost of holding multiple contexts in your head at once.",[15,1754,1755],{},"In practical terms, think of a method like this:",[27,1757,1758,1767,1770,1778,1783,1786],{},[30,1759,1760,354,1762,354,1764,1766],{},[154,1761,156],{},[154,1763,161],{},[154,1765,204],{},", and loops add +1",[30,1768,1769],{},"nesting is penalised more heavily",[30,1771,1772,1773,354,1775,1777],{},"compound logical operators (",[154,1774,242],{},[154,1776,247],{},") can also increase cognitive complexity because they force you to keep multiple conditions in mind and can hurt readability more than the cyclomatic number alone suggests",[30,1779,1780,1782],{},[154,1781,170],{}," doesn't count",[30,1784,1785],{},"early return is not penalised",[30,1787,1788],{},"syntax sugar tends not to be penalised",[15,1790,1791,1792,1794,1795,354,1797,1799],{},"Depending on the tool, other constructs such as ",[154,1793,1582],{},", recursion, ",[154,1796,357],{},[154,1798,360],{},", and ternary operators may also contribute to the score.",[15,1801,1802,1803,1806],{},"Another important consequence is that ",[19,1804,1805],{},"guard clauses"," (early return) usually make code easier to read and often reduce cognitive cost, even when cyclomatic complexity stays the same.",[15,1808,1809],{},"Note: the exact rules vary by implementation and tool, so use this metric as a guide to readability, not as an absolute mathematical truth.",[108,1811,404],{"id":1812},"practical-examples-1",[145,1814,1816],{"id":1815},"example-1-linear-function","Example 1: Linear function",[255,1818,1820],{"className":257,"code":1819,"language":259,"meta":260,"style":260},"int Sum(int a, int b)\n{\n  \u002F\u002F Total: 0\n  return a + b;\n}\n",[154,1821,1822,1842,1846,1851,1861],{"__ignoreMap":260},[264,1823,1824,1826,1828,1830,1832,1834,1836,1838,1840],{"class":266,"line":267},[264,1825,418],{"class":270},[264,1827,422],{"class":421},[264,1829,425],{"class":273},[264,1831,418],{"class":270},[264,1833,430],{"class":421},[264,1835,354],{"class":273},[264,1837,418],{"class":270},[264,1839,437],{"class":421},[264,1841,440],{"class":273},[264,1843,1844],{"class":266,"line":287},[264,1845,290],{"class":273},[264,1847,1848],{"class":266,"line":293},[264,1849,1850],{"class":296},"  \u002F\u002F Total: 0\n",[264,1852,1853,1855,1857,1859],{"class":266,"line":300},[264,1854,460],{"class":270},[264,1856,463],{"class":273},[264,1858,466],{"class":270},[264,1860,469],{"class":273},[264,1862,1863],{"class":266,"line":457},[264,1864,303],{"class":273},[15,1866,1867],{},"Purely linear flow. No decisions, no branching, and no nesting. The mental effort needed to understand it is zero.",[145,1869,481,1871],{"id":1870},"example-2-simple-if-1",[154,1872,156],{},[255,1874,1876],{"className":257,"code":1875,"language":259,"meta":260,"style":260},"int GetSign(int number)\n{\n  if (number > 0) \u002F\u002F +1 (if)\n  {\n    return 1;\n  }\n\n  \u002F\u002F Total: 1\n  return -1;\n}\n",[154,1877,1878,1892,1896,1910,1914,1922,1926,1930,1934,1944],{"__ignoreMap":260},[264,1879,1880,1882,1884,1886,1888,1890],{"class":266,"line":267},[264,1881,418],{"class":270},[264,1883,495],{"class":421},[264,1885,425],{"class":273},[264,1887,418],{"class":270},[264,1889,502],{"class":421},[264,1891,440],{"class":273},[264,1893,1894],{"class":266,"line":287},[264,1895,290],{"class":273},[264,1897,1898,1900,1902,1904,1906,1908],{"class":266,"line":293},[264,1899,517],{"class":270},[264,1901,520],{"class":273},[264,1903,523],{"class":270},[264,1905,527],{"class":526},[264,1907,530],{"class":273},[264,1909,533],{"class":296},[264,1911,1912],{"class":266,"line":300},[264,1913,538],{"class":273},[264,1915,1916,1918,1920],{"class":266,"line":457},[264,1917,543],{"class":270},[264,1919,546],{"class":526},[264,1921,549],{"class":273},[264,1923,1924],{"class":266,"line":472},[264,1925,555],{"class":273},[264,1927,1928],{"class":266,"line":552},[264,1929,562],{"emptyLinePlaceholder":561},[264,1931,1932],{"class":266,"line":558},[264,1933,454],{"class":296},[264,1935,1936,1938,1940,1942],{"class":266,"line":565},[264,1937,460],{"class":270},[264,1939,576],{"class":270},[264,1941,579],{"class":526},[264,1943,549],{"class":273},[264,1945,1946],{"class":266,"line":571},[264,1947,303],{"class":273},[15,1949,1950],{},"One clear decision at the top level. Cognitive complexity is 1.",[145,1952,1954],{"id":1953},"example-3-logical-operators","Example 3: Logical operators",[255,1956,1958],{"className":257,"code":1957,"language":259,"meta":260,"style":260},"bool IsValid(int age, bool hasPermission)\n{\n  if (age > 18 && hasPermission) \u002F\u002F +1 (if), +1 (&&)\n  {\n    return true;\n  }\n\n  \u002F\u002F Total: 2\n  return false;\n}\n",[154,1959,1960,1980,1984,2000,2004,2012,2016,2020,2024,2032],{"__ignoreMap":260},[264,1961,1962,1964,1966,1968,1970,1972,1974,1976,1978],{"class":266,"line":267},[264,1963,710],{"class":270},[264,1965,713],{"class":421},[264,1967,425],{"class":273},[264,1969,418],{"class":270},[264,1971,720],{"class":421},[264,1973,354],{"class":273},[264,1975,710],{"class":270},[264,1977,727],{"class":421},[264,1979,440],{"class":273},[264,1981,1982],{"class":266,"line":287},[264,1983,290],{"class":273},[264,1985,1986,1988,1990,1992,1994,1996,1998],{"class":266,"line":293},[264,1987,517],{"class":270},[264,1989,744],{"class":273},[264,1991,523],{"class":270},[264,1993,749],{"class":526},[264,1995,752],{"class":270},[264,1997,755],{"class":273},[264,1999,758],{"class":296},[264,2001,2002],{"class":266,"line":300},[264,2003,538],{"class":273},[264,2005,2006,2008,2010],{"class":266,"line":457},[264,2007,543],{"class":270},[264,2009,769],{"class":526},[264,2011,549],{"class":273},[264,2013,2014],{"class":266,"line":472},[264,2015,555],{"class":273},[264,2017,2018],{"class":266,"line":552},[264,2019,562],{"emptyLinePlaceholder":561},[264,2021,2022],{"class":266,"line":558},[264,2023,568],{"class":296},[264,2025,2026,2028,2030],{"class":266,"line":565},[264,2027,460],{"class":270},[264,2029,791],{"class":526},[264,2031,549],{"class":273},[264,2033,2034],{"class":266,"line":571},[264,2035,303],{"class":273},[15,2037,2038,2039,2041],{},"The conditional already breaks the linear flow, so that adds +1. The logical operator sequence (",[154,2040,242],{},") adds another +1 because the developer has to keep multiple logical states in mind at the same time.",[145,2043,697,2045,2048],{"id":2044},"example-4-ifelse-if-chain",[154,2046,2047],{},"if\u002Felse if"," chain",[255,2050,2052],{"className":257,"code":2051,"language":259,"meta":260,"style":260},"string GetCategory(int score)\n{\n  if (score > 90) \u002F\u002F +1 (if)\n  {\n    return \"A\";\n  }\n  else if (score > 70) \u002F\u002F +1 (else if)\n  {\n    return \"B\";\n  }\n\n  \u002F\u002F Total: 2\n  return \"C\";\n}\n",[154,2053,2054,2068,2072,2086,2090,2098,2102,2118,2122,2130,2134,2138,2142,2150],{"__ignoreMap":260},[264,2055,2056,2058,2060,2062,2064,2066],{"class":266,"line":267},[264,2057,819],{"class":270},[264,2059,822],{"class":421},[264,2061,425],{"class":273},[264,2063,418],{"class":270},[264,2065,829],{"class":421},[264,2067,440],{"class":273},[264,2069,2070],{"class":266,"line":287},[264,2071,290],{"class":273},[264,2073,2074,2076,2078,2080,2082,2084],{"class":266,"line":293},[264,2075,517],{"class":270},[264,2077,846],{"class":273},[264,2079,523],{"class":270},[264,2081,851],{"class":526},[264,2083,530],{"class":273},[264,2085,533],{"class":296},[264,2087,2088],{"class":266,"line":300},[264,2089,538],{"class":273},[264,2091,2092,2094,2096],{"class":266,"line":457},[264,2093,543],{"class":270},[264,2095,867],{"class":866},[264,2097,549],{"class":273},[264,2099,2100],{"class":266,"line":472},[264,2101,555],{"class":273},[264,2103,2104,2106,2108,2110,2112,2114,2116],{"class":266,"line":552},[264,2105,878],{"class":270},[264,2107,881],{"class":270},[264,2109,846],{"class":273},[264,2111,523],{"class":270},[264,2113,888],{"class":526},[264,2115,530],{"class":273},[264,2117,893],{"class":296},[264,2119,2120],{"class":266,"line":558},[264,2121,538],{"class":273},[264,2123,2124,2126,2128],{"class":266,"line":565},[264,2125,543],{"class":270},[264,2127,904],{"class":866},[264,2129,549],{"class":273},[264,2131,2132],{"class":266,"line":571},[264,2133,555],{"class":273},[264,2135,2136],{"class":266,"line":584},[264,2137,562],{"emptyLinePlaceholder":561},[264,2139,2140],{"class":266,"line":680},[264,2141,568],{"class":296},[264,2143,2144,2146,2148],{"class":266,"line":685},[264,2145,460],{"class":270},[264,2147,926],{"class":866},[264,2149,549],{"class":273},[264,2151,2152],{"class":266,"line":921},[264,2153,303],{"class":273},[15,2155,2156],{},"This is still relatively easy to read, even with two branches. The cognitive complexity is 2, which is reasonable for a small function.",[145,2158,2160,2161],{"id":2159},"example-5-nested-if","Example 5: Nested ",[154,2162,156],{},[255,2164,2166],{"className":257,"code":2165,"language":259,"meta":260,"style":260},"bool IsValidUser(User user)\n{\n  if (user != null) \u002F\u002F +1 (if)\n  {\n    if (user.IsActive) \u002F\u002F +1 (if) + 1 (nesting penalty)\n    {\n      return true;\n    }\n  }\n\n  \u002F\u002F Total: 3\n  return false;\n}\n",[154,2167,2168,2183,2187,2201,2205,2215,2219,2227,2231,2235,2239,2243,2251],{"__ignoreMap":260},[264,2169,2170,2172,2175,2177,2179,2181],{"class":266,"line":267},[264,2171,710],{"class":270},[264,2173,2174],{"class":421}," IsValidUser",[264,2176,425],{"class":273},[264,2178,1237],{"class":421},[264,2180,1240],{"class":421},[264,2182,440],{"class":273},[264,2184,2185],{"class":266,"line":287},[264,2186,290],{"class":273},[264,2188,2189,2191,2193,2195,2197,2199],{"class":266,"line":293},[264,2190,517],{"class":270},[264,2192,1257],{"class":273},[264,2194,1260],{"class":270},[264,2196,1263],{"class":526},[264,2198,530],{"class":273},[264,2200,533],{"class":296},[264,2202,2203],{"class":266,"line":300},[264,2204,538],{"class":273},[264,2206,2207,2209,2212],{"class":266,"line":457},[264,2208,1031],{"class":270},[264,2210,2211],{"class":273}," (user.IsActive) ",[264,2213,2214],{"class":296},"\u002F\u002F +1 (if) + 1 (nesting penalty)\n",[264,2216,2217],{"class":266,"line":472},[264,2218,1047],{"class":273},[264,2220,2221,2223,2225],{"class":266,"line":552},[264,2222,1165],{"class":270},[264,2224,769],{"class":526},[264,2226,549],{"class":273},[264,2228,2229],{"class":266,"line":558},[264,2230,1062],{"class":273},[264,2232,2233],{"class":266,"line":565},[264,2234,555],{"class":273},[264,2236,2237],{"class":266,"line":571},[264,2238,562],{"emptyLinePlaceholder":561},[264,2240,2241],{"class":266,"line":584},[264,2242,784],{"class":296},[264,2244,2245,2247,2249],{"class":266,"line":680},[264,2246,460],{"class":270},[264,2248,791],{"class":526},[264,2250,549],{"class":273},[264,2252,2253],{"class":266,"line":685},[264,2254,303],{"class":273},[15,2256,2257,2258,2260],{},"This is where nesting starts to hurt. The reader has to keep the context of the first ",[154,2259,156],{}," in mind while reading the second.",[145,2262,2264],{"id":2263},"example-6-multiple-levels-of-nesting","Example 6: Multiple levels of nesting",[255,2266,2268],{"className":257,"code":2267,"language":259,"meta":260,"style":260},"bool CanProcessOrder(Order order)\n{\n  if (order != null) \u002F\u002F +1 (if)\n  {\n    if (order.IsValid) \u002F\u002F +1 (if) + 1 (nesting penalty)\n    {\n      if (order.Total > 0) \u002F\u002F +1 (if) + 2 (nesting penalty)\n      {\n        return true;\n      }\n    }\n  }\n\n  \u002F\u002F Total: 6\n  return false;\n}\n",[154,2269,2270,2287,2291,2306,2310,2319,2323,2340,2345,2354,2359,2363,2367,2371,2376,2384],{"__ignoreMap":260},[264,2271,2272,2274,2277,2279,2282,2285],{"class":266,"line":267},[264,2273,710],{"class":270},[264,2275,2276],{"class":421}," CanProcessOrder",[264,2278,425],{"class":273},[264,2280,2281],{"class":421},"Order",[264,2283,2284],{"class":421}," order",[264,2286,440],{"class":273},[264,2288,2289],{"class":266,"line":287},[264,2290,290],{"class":273},[264,2292,2293,2295,2298,2300,2302,2304],{"class":266,"line":293},[264,2294,517],{"class":270},[264,2296,2297],{"class":273}," (order ",[264,2299,1260],{"class":270},[264,2301,1263],{"class":526},[264,2303,530],{"class":273},[264,2305,533],{"class":296},[264,2307,2308],{"class":266,"line":300},[264,2309,538],{"class":273},[264,2311,2312,2314,2317],{"class":266,"line":457},[264,2313,1031],{"class":270},[264,2315,2316],{"class":273}," (order.IsValid) ",[264,2318,2214],{"class":296},[264,2320,2321],{"class":266,"line":472},[264,2322,1047],{"class":273},[264,2324,2325,2328,2331,2333,2335,2337],{"class":266,"line":552},[264,2326,2327],{"class":270},"      if",[264,2329,2330],{"class":273}," (order.Total ",[264,2332,523],{"class":270},[264,2334,527],{"class":526},[264,2336,530],{"class":273},[264,2338,2339],{"class":296},"\u002F\u002F +1 (if) + 2 (nesting penalty)\n",[264,2341,2342],{"class":266,"line":558},[264,2343,2344],{"class":273},"      {\n",[264,2346,2347,2350,2352],{"class":266,"line":565},[264,2348,2349],{"class":270},"        return",[264,2351,769],{"class":526},[264,2353,549],{"class":273},[264,2355,2356],{"class":266,"line":571},[264,2357,2358],{"class":273},"      }\n",[264,2360,2361],{"class":266,"line":584},[264,2362,1062],{"class":273},[264,2364,2365],{"class":266,"line":680},[264,2366,555],{"class":273},[264,2368,2369],{"class":266,"line":685},[264,2370,562],{"emptyLinePlaceholder":561},[264,2372,2373],{"class":266,"line":921},[264,2374,2375],{"class":296},"  \u002F\u002F Total: 6\n",[264,2377,2378,2380,2382],{"class":266,"line":931},[264,2379,460],{"class":270},[264,2381,791],{"class":526},[264,2383,549],{"class":273},[264,2385,2386],{"class":266,"line":1084},[264,2387,303],{"class":273},[15,2389,2390],{},"With three levels of nesting, cognitive complexity jumps to 6. The reader has to keep three conditions in mind at once.",[15,2392,2393],{},[2394,2395],"img",{"alt":2396,"src":2397},"Code Kamehameha nesting penalty","https:\u002F\u002Fpbs.twimg.com\u002Fmedia\u002FD5tgy4cWsAMk3kf?format=png&name=small",[15,2399,2400],{},"At this point, the nesting penalty starts to look less like business logic and more like a Code Kamehameha charging up.",[145,2402,2404],{"id":2403},"example-7-guard-clauses","Example 7: Guard clauses",[255,2406,2408],{"className":257,"code":2407,"language":259,"meta":260,"style":260},"bool CanProcessOrder(Order order)\n{\n  if (order == null) \u002F\u002F +1 (if)\n    return false;\n\n  if (!order.IsValid) \u002F\u002F +1 (if)\n    return false;\n\n  if (order.Total \u003C= 0) \u002F\u002F +1 (if)\n    return false;\n\n  \u002F\u002F Total: 3\n  return true;\n}\n",[154,2409,2410,2424,2428,2442,2450,2454,2467,2475,2479,2494,2502,2506,2510,2518],{"__ignoreMap":260},[264,2411,2412,2414,2416,2418,2420,2422],{"class":266,"line":267},[264,2413,710],{"class":270},[264,2415,2276],{"class":421},[264,2417,425],{"class":273},[264,2419,2281],{"class":421},[264,2421,2284],{"class":421},[264,2423,440],{"class":273},[264,2425,2426],{"class":266,"line":287},[264,2427,290],{"class":273},[264,2429,2430,2432,2434,2436,2438,2440],{"class":266,"line":293},[264,2431,517],{"class":270},[264,2433,2297],{"class":273},[264,2435,1370],{"class":270},[264,2437,1263],{"class":526},[264,2439,530],{"class":273},[264,2441,533],{"class":296},[264,2443,2444,2446,2448],{"class":266,"line":300},[264,2445,543],{"class":270},[264,2447,791],{"class":526},[264,2449,549],{"class":273},[264,2451,2452],{"class":266,"line":457},[264,2453,562],{"emptyLinePlaceholder":561},[264,2455,2456,2458,2460,2462,2465],{"class":266,"line":472},[264,2457,517],{"class":270},[264,2459,1008],{"class":273},[264,2461,1405],{"class":270},[264,2463,2464],{"class":273},"order.IsValid) ",[264,2466,533],{"class":296},[264,2468,2469,2471,2473],{"class":266,"line":552},[264,2470,543],{"class":270},[264,2472,791],{"class":526},[264,2474,549],{"class":273},[264,2476,2477],{"class":266,"line":558},[264,2478,562],{"emptyLinePlaceholder":561},[264,2480,2481,2483,2485,2488,2490,2492],{"class":266,"line":565},[264,2482,517],{"class":270},[264,2484,2330],{"class":273},[264,2486,2487],{"class":270},"\u003C=",[264,2489,527],{"class":526},[264,2491,530],{"class":273},[264,2493,533],{"class":296},[264,2495,2496,2498,2500],{"class":266,"line":571},[264,2497,543],{"class":270},[264,2499,791],{"class":526},[264,2501,549],{"class":273},[264,2503,2504],{"class":266,"line":584},[264,2505,562],{"emptyLinePlaceholder":561},[264,2507,2508],{"class":266,"line":680},[264,2509,784],{"class":296},[264,2511,2512,2514,2516],{"class":266,"line":685},[264,2513,460],{"class":270},[264,2515,769],{"class":526},[264,2517,549],{"class":273},[264,2519,2520],{"class":266,"line":921},[264,2521,303],{"class":273},[15,2523,2524],{},"This is where guard clauses shine. The structure has the same number of decisions, but cognitive complexity drops to 3 because the nesting disappears.",[145,2526,2528],{"id":2527},"example-8-compound-logical-operators","Example 8: Compound logical operators",[255,2530,2532],{"className":257,"code":2531,"language":259,"meta":260,"style":260},"bool HasPermission(User user, Resource resource)\n{\n  if (user.IsAdmin || (resource.IsPublic && user.IsResourceOwner)) \u002F\u002F +1 (if), +1 (||), +1 (&&)\n  {\n    return true;\n  }\n\n  \u002F\u002F Total: 3\n  return false;\n}\n",[154,2533,2534,2557,2561,2581,2585,2593,2597,2601,2605,2613],{"__ignoreMap":260},[264,2535,2536,2538,2541,2543,2545,2547,2549,2552,2555],{"class":266,"line":267},[264,2537,710],{"class":270},[264,2539,2540],{"class":421}," HasPermission",[264,2542,425],{"class":273},[264,2544,1237],{"class":421},[264,2546,1240],{"class":421},[264,2548,354],{"class":273},[264,2550,2551],{"class":421},"Resource",[264,2553,2554],{"class":421}," resource",[264,2556,440],{"class":273},[264,2558,2559],{"class":266,"line":287},[264,2560,290],{"class":273},[264,2562,2563,2565,2568,2570,2573,2575,2578],{"class":266,"line":293},[264,2564,517],{"class":270},[264,2566,2567],{"class":273}," (user.IsAdmin ",[264,2569,247],{"class":270},[264,2571,2572],{"class":273}," (resource.IsPublic ",[264,2574,242],{"class":270},[264,2576,2577],{"class":273}," user.IsResourceOwner)) ",[264,2579,2580],{"class":296},"\u002F\u002F +1 (if), +1 (||), +1 (&&)\n",[264,2582,2583],{"class":266,"line":300},[264,2584,538],{"class":273},[264,2586,2587,2589,2591],{"class":266,"line":457},[264,2588,543],{"class":270},[264,2590,769],{"class":526},[264,2592,549],{"class":273},[264,2594,2595],{"class":266,"line":472},[264,2596,555],{"class":273},[264,2598,2599],{"class":266,"line":552},[264,2600,562],{"emptyLinePlaceholder":561},[264,2602,2603],{"class":266,"line":558},[264,2604,784],{"class":296},[264,2606,2607,2609,2611],{"class":266,"line":565},[264,2608,460],{"class":270},[264,2610,791],{"class":526},[264,2612,549],{"class":273},[264,2614,2615],{"class":266,"line":571},[264,2616,303],{"class":273},[15,2618,2619,2620,2622],{},"Here the cognitive complexity is 3 even with only one ",[154,2621,156],{},", because the condition forces the reader to hold several logical relationships in mind.",[145,2624,2626],{"id":2625},"example-9-nested-logical-operators","Example 9: Nested logical operators",[255,2628,2630],{"className":257,"code":2629,"language":259,"meta":260,"style":260},"bool HasPermission(User user, Resource resource)\n{\n  if (user.IsActive) \u002F\u002F +1 (if)\n  {\n    if (user.IsVerified && user.HasPermission) \u002F\u002F +1 (if), +1 (&&), +1 (nesting penalty)\n    {\n      return true;\n    }\n  }\n\n  \u002F\u002F Total: 4\n  return false;\n}\n",[154,2631,2632,2652,2656,2664,2668,2682,2686,2694,2698,2702,2706,2710,2718],{"__ignoreMap":260},[264,2633,2634,2636,2638,2640,2642,2644,2646,2648,2650],{"class":266,"line":267},[264,2635,710],{"class":270},[264,2637,2540],{"class":421},[264,2639,425],{"class":273},[264,2641,1237],{"class":421},[264,2643,1240],{"class":421},[264,2645,354],{"class":273},[264,2647,2551],{"class":421},[264,2649,2554],{"class":421},[264,2651,440],{"class":273},[264,2653,2654],{"class":266,"line":287},[264,2655,290],{"class":273},[264,2657,2658,2660,2662],{"class":266,"line":293},[264,2659,517],{"class":270},[264,2661,2211],{"class":273},[264,2663,533],{"class":296},[264,2665,2666],{"class":266,"line":300},[264,2667,538],{"class":273},[264,2669,2670,2672,2675,2677,2679],{"class":266,"line":457},[264,2671,1031],{"class":270},[264,2673,2674],{"class":273}," (user.IsVerified ",[264,2676,242],{"class":270},[264,2678,1283],{"class":273},[264,2680,2681],{"class":296},"\u002F\u002F +1 (if), +1 (&&), +1 (nesting penalty)\n",[264,2683,2684],{"class":266,"line":472},[264,2685,1047],{"class":273},[264,2687,2688,2690,2692],{"class":266,"line":552},[264,2689,1165],{"class":270},[264,2691,769],{"class":526},[264,2693,549],{"class":273},[264,2695,2696],{"class":266,"line":558},[264,2697,1062],{"class":273},[264,2699,2700],{"class":266,"line":565},[264,2701,555],{"class":273},[264,2703,2704],{"class":266,"line":571},[264,2705,562],{"emptyLinePlaceholder":561},[264,2707,2708],{"class":266,"line":584},[264,2709,1314],{"class":296},[264,2711,2712,2714,2716],{"class":266,"line":680},[264,2713,460],{"class":270},[264,2715,791],{"class":526},[264,2717,549],{"class":273},[264,2719,2720],{"class":266,"line":685},[264,2721,303],{"class":273},[15,2723,2724],{},"Here the score rises because the reader has to deal with both nesting and a compound condition at the same time.",[145,2726,2728,2729,2731],{"id":2727},"example-10-nested-for-loop","Example 10: Nested ",[154,2730,182],{}," loop",[255,2733,2735],{"className":257,"code":2734,"language":259,"meta":260,"style":260},"int countExpensiveItems(List\u003COrder> orders)\n{\n  int count = 0;\n\n  for (int i = 0; i \u003C orders.Count; i++) \u002F\u002F +1 (for)\n  {\n    for (int j = 0; j \u003C orders[i].Items.Count; j++) \u002F\u002F +1 (for) + 1 (nesting penalty)\n    {\n      if (orders[i].Items[j].Price > 100) \u002F\u002F +1 (if) + 2 (nesting penalty)\n      {\n        count++;\n      }\n    }\n  }\n\n  \u002F\u002F Total: 6\n  return count;\n}\n",[154,2736,2737,2759,2763,2776,2780,2811,2815,2846,2850,2866,2870,2879,2883,2887,2891,2895,2899,2905],{"__ignoreMap":260},[264,2738,2739,2741,2744,2746,2748,2750,2752,2754,2757],{"class":266,"line":267},[264,2740,418],{"class":270},[264,2742,2743],{"class":421}," countExpensiveItems",[264,2745,425],{"class":273},[264,2747,960],{"class":421},[264,2749,963],{"class":273},[264,2751,2281],{"class":421},[264,2753,968],{"class":273},[264,2755,2756],{"class":421},"orders",[264,2758,440],{"class":273},[264,2760,2761],{"class":266,"line":287},[264,2762,290],{"class":273},[264,2764,2765,2768,2770,2772,2774],{"class":266,"line":293},[264,2766,2767],{"class":270},"  int",[264,2769,989],{"class":421},[264,2771,992],{"class":270},[264,2773,527],{"class":526},[264,2775,549],{"class":273},[264,2777,2778],{"class":266,"line":300},[264,2779,562],{"emptyLinePlaceholder":561},[264,2781,2782,2785,2787,2789,2792,2794,2796,2799,2801,2804,2806,2808],{"class":266,"line":457},[264,2783,2784],{"class":270},"  for",[264,2786,1008],{"class":273},[264,2788,418],{"class":270},[264,2790,2791],{"class":421}," i",[264,2793,992],{"class":270},[264,2795,527],{"class":526},[264,2797,2798],{"class":273},"; i ",[264,2800,963],{"class":270},[264,2802,2803],{"class":273}," orders.Count; i",[264,2805,1055],{"class":270},[264,2807,530],{"class":273},[264,2809,2810],{"class":296},"\u002F\u002F +1 (for)\n",[264,2812,2813],{"class":266,"line":472},[264,2814,538],{"class":273},[264,2816,2817,2820,2822,2824,2827,2829,2831,2834,2836,2839,2841,2843],{"class":266,"line":552},[264,2818,2819],{"class":270},"    for",[264,2821,1008],{"class":273},[264,2823,418],{"class":270},[264,2825,2826],{"class":421}," j",[264,2828,992],{"class":270},[264,2830,527],{"class":526},[264,2832,2833],{"class":273},"; j ",[264,2835,963],{"class":270},[264,2837,2838],{"class":273}," orders[i].Items.Count; j",[264,2840,1055],{"class":270},[264,2842,530],{"class":273},[264,2844,2845],{"class":296},"\u002F\u002F +1 (for) + 1 (nesting penalty)\n",[264,2847,2848],{"class":266,"line":558},[264,2849,1047],{"class":273},[264,2851,2852,2854,2857,2859,2862,2864],{"class":266,"line":565},[264,2853,2327],{"class":270},[264,2855,2856],{"class":273}," (orders[i].Items[j].Price ",[264,2858,523],{"class":270},[264,2860,2861],{"class":526}," 100",[264,2863,530],{"class":273},[264,2865,2339],{"class":296},[264,2867,2868],{"class":266,"line":571},[264,2869,2344],{"class":273},[264,2871,2872,2875,2877],{"class":266,"line":584},[264,2873,2874],{"class":273},"        count",[264,2876,1055],{"class":270},[264,2878,549],{"class":273},[264,2880,2881],{"class":266,"line":680},[264,2882,2358],{"class":273},[264,2884,2885],{"class":266,"line":685},[264,2886,1062],{"class":273},[264,2888,2889],{"class":266,"line":921},[264,2890,555],{"class":273},[264,2892,2893],{"class":266,"line":931},[264,2894,562],{"emptyLinePlaceholder":561},[264,2896,2897],{"class":266,"line":1084},[264,2898,2375],{"class":296},[264,2900,2901,2903],{"class":266,"line":1714},[264,2902,460],{"class":270},[264,2904,1081],{"class":273},[264,2906,2907],{"class":266,"line":1719},[264,2908,303],{"class":273},[15,2910,2911,2912,2914],{},"Nested loops are penalised in the same way as nested ",[154,2913,156],{}," blocks.",[145,2916,1579,2918],{"id":2917},"example-11-switch",[154,2919,204],{},[255,2921,2923],{"className":257,"code":2922,"language":259,"meta":260,"style":260},"int GetStatus(string status)\n{\n  switch (status) \u002F\u002F +1 (switch)\n  {\n    case \"New\": return 1;\n    case \"InProgress\": return 2;\n    case \"Done\": return 3;\n    default: return 0;\n  }\n\n  \u002F\u002F Total: 1\n}\n",[154,2924,2925,2941,2945,2955,2959,2974,2990,3006,3018,3022,3026,3030],{"__ignoreMap":260},[264,2926,2927,2929,2932,2934,2936,2939],{"class":266,"line":267},[264,2928,418],{"class":270},[264,2930,2931],{"class":421}," GetStatus",[264,2933,425],{"class":273},[264,2935,819],{"class":270},[264,2937,2938],{"class":421}," status",[264,2940,440],{"class":273},[264,2942,2943],{"class":266,"line":287},[264,2944,290],{"class":273},[264,2946,2947,2949,2952],{"class":266,"line":293},[264,2948,1129],{"class":270},[264,2950,2951],{"class":273}," (status) ",[264,2953,2954],{"class":296},"\u002F\u002F +1 (switch)\n",[264,2956,2957],{"class":266,"line":300},[264,2958,538],{"class":273},[264,2960,2961,2963,2966,2968,2970,2972],{"class":266,"line":457},[264,2962,1141],{"class":270},[264,2964,2965],{"class":866}," \"New\"",[264,2967,1146],{"class":273},[264,2969,353],{"class":270},[264,2971,546],{"class":526},[264,2973,549],{"class":273},[264,2975,2976,2978,2981,2983,2985,2988],{"class":266,"line":472},[264,2977,1141],{"class":270},[264,2979,2980],{"class":866}," \"InProgress\"",[264,2982,1146],{"class":273},[264,2984,353],{"class":270},[264,2986,2987],{"class":526}," 2",[264,2989,549],{"class":273},[264,2991,2992,2994,2997,2999,3001,3004],{"class":266,"line":552},[264,2993,1141],{"class":270},[264,2995,2996],{"class":866}," \"Done\"",[264,2998,1146],{"class":273},[264,3000,353],{"class":270},[264,3002,3003],{"class":526}," 3",[264,3005,549],{"class":273},[264,3007,3008,3010,3012,3014,3016],{"class":266,"line":558},[264,3009,1175],{"class":270},[264,3011,1146],{"class":273},[264,3013,353],{"class":270},[264,3015,527],{"class":526},[264,3017,549],{"class":273},[264,3019,3020],{"class":266,"line":565},[264,3021,555],{"class":273},[264,3023,3024],{"class":266,"line":571},[264,3025,562],{"emptyLinePlaceholder":561},[264,3027,3028],{"class":266,"line":584},[264,3029,454],{"class":296},[264,3031,3032],{"class":266,"line":680},[264,3033,303],{"class":273},[15,3035,3036,3037,3039,3040,3042],{},"A ",[154,3038,204],{}," is often cognitively cheaper than a long chain of ",[154,3041,161],{}," branches because the reader follows one discriminator with explicit branches instead of re-evaluating a different condition in each branch.",[10,3044,3046],{"id":3045},"trade-offs","Trade-offs",[15,3048,3049],{},"All of these metrics are tools to help us write better code, but they aren't absolute rules. The goal is to balance clarity, maintainability, and performance.",[108,3051,3053],{"id":3052},"mathematical-expressions","Mathematical expressions",[255,3055,3057],{"className":257,"code":3056,"language":259,"meta":260,"style":260},"var result = (a * b + c \u002F d) * Math.Pow(x, y) \u002F (z + 1);\n",[154,3058,3059],{"__ignoreMap":260},[264,3060,3061,3063,3066,3068,3070,3073,3075,3077,3080,3083,3086,3088,3091,3094,3097,3099,3102,3104,3106],{"class":266,"line":267},[264,3062,1011],{"class":270},[264,3064,3065],{"class":421}," result",[264,3067,992],{"class":270},[264,3069,274],{"class":273},[264,3071,3072],{"class":270},"*",[264,3074,279],{"class":273},[264,3076,466],{"class":270},[264,3078,3079],{"class":273}," c ",[264,3081,3082],{"class":270},"\u002F",[264,3084,3085],{"class":273}," d) ",[264,3087,3072],{"class":270},[264,3089,3090],{"class":273}," Math.",[264,3092,3093],{"class":421},"Pow",[264,3095,3096],{"class":273},"(x, y) ",[264,3098,3082],{"class":270},[264,3100,3101],{"class":273}," (z ",[264,3103,466],{"class":270},[264,3105,546],{"class":526},[264,3107,3108],{"class":273},");\n",[15,3110,3111],{},"This example has cyclomatic complexity 1 (linear flow) and cognitive complexity 0 (no decisions). That still doesn't make it easy to understand. The reader still has to parse the expression itself, which can be mentally expensive even without branches.",[108,3113,3115],{"id":3114},"poor-variable-and-function-names","Poor variable and function names",[255,3117,3119],{"className":257,"code":3118,"language":259,"meta":260,"style":260},"if (x)\n{\n  z();\n}\n",[154,3120,3121,3128,3132,3139],{"__ignoreMap":260},[264,3122,3123,3125],{"class":266,"line":267},[264,3124,156],{"class":270},[264,3126,3127],{"class":273}," (x)\n",[264,3129,3130],{"class":266,"line":287},[264,3131,290],{"class":273},[264,3133,3134,3137],{"class":266,"line":293},[264,3135,3136],{"class":421},"  z",[264,3138,1641],{"class":273},[264,3140,3141],{"class":266,"line":300},[264,3142,303],{"class":273},[15,3144,3145,3146,3149,3150,3153],{},"Even with low cognitive complexity, poor naming can make code hard to understand. The reader has to work out what ",[154,3147,3148],{},"x"," represents and what ",[154,3151,3152],{},"z()"," actually does.",[108,3155,3157],{"id":3156},"breaking-code-apart","Breaking code apart",[15,3159,3160],{},"Sometimes, trying to reduce the local cyclomatic complexity of a method leads to extracting too many sub-methods that don't really need to exist. That forces the reader to jump between files and methods just to follow the logic, which can increase the mental effort required to understand the code even if each individual method has low cyclomatic complexity.",[15,3162,3163],{},"Lowering the local metric of a method doesn't guarantee a lower overall comprehension cost. If a refactor forces the reader to hop through too many methods, files, and intermediary names, the global cognitive cost can go up instead.",[108,3165,3167],{"id":3166},"tool-limitations","Tool limitations",[15,3169,3170],{},"To calculate these metrics, tools have to analyse the code statically. That means they can miss other important aspects such as business domains, design patterns, architecture, and more. Relying on these metrics alone can therefore lead to refactoring decisions that do not actually improve code quality.",[10,3172,3174],{"id":3173},"strategies-for-reducing-complexity-without-gaming-the-metrics","Strategies for reducing complexity (without gaming the metrics)",[27,3176,3177,3180,3183,3186,3189,3192,3195],{},[30,3178,3179],{},"prefer guard clauses when they simplify the flow",[30,3181,3182],{},"use smaller functions with clear responsibilities, without overslicing",[30,3184,3185],{},"reduce unnecessary nesting",[30,3187,3188],{},"extract business rules into more expressive names",[30,3190,3191],{},"avoid giant conditions when they are not needed",[30,3193,3194],{},"use metrics as an alarm, not as the final objective",[30,3196,3197],{},"follow SOLID principles, calisthenic principles, and good design practices",[10,3199,3201],{"id":3200},"does-it-still-make-sense-to-talk-about-code-complexity","Does it still make sense to talk about code complexity?",[15,3203,3204,3205,3207,3208,3211,3212,83],{},"That may sound like a contradiction to what I said at the start with the phrase ",[19,3206,21],{},". The way code is written is changing quickly. With the widespread adoption of AI, we can also say that ",[19,3209,3210],{},"the machine writes code for developers",", and in some cases ",[19,3213,3214],{},"the machine writes code for another machine",[15,3216,3217],{},"That can make a topic like this feel less relevant. I don't think that's true.",[15,3219,3220],{},"Agents are still orchestrated by developers, and we still set their rules, goals, and limits. We're also the ones who validate the code AI generates. One way or another, that means we still need to understand these concepts so we can configure the environment around those agents properly. We need to know how to set the right prompts, instructions, and validation criteria to make sure the code AI generates meets our standards for readability, maintainability, and correctness.",[15,3222,3223],{},"So this topic may not feel like the highest priority right now, and I can understand that view. It still matters, because in the end the responsibility for deciding what is right and what is wrong remains ours.",[3225,3226,3227],"style",{},"html pre.shiki code .snl16, html code.shiki .snl16{--shiki-default:#F97583}html pre.shiki code .s95oV, html code.shiki .s95oV{--shiki-default:#E1E4E8}html pre.shiki code .sAwPA, html code.shiki .sAwPA{--shiki-default:#6A737D}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html pre.shiki code .svObZ, html code.shiki .svObZ{--shiki-default:#B392F0}html pre.shiki code .sDLfK, html code.shiki .sDLfK{--shiki-default:#79B8FF}html pre.shiki code .sU2Wk, html code.shiki .sU2Wk{--shiki-default:#9ECBFF}",{"title":260,"searchDepth":287,"depth":287,"links":3229},[3230,3231,3237,3240,3246,3247],{"id":12,"depth":287,"text":13},{"id":86,"depth":287,"text":87,"children":3232},[3233,3234,3235,3236],{"id":110,"depth":293,"text":111},{"id":142,"depth":293,"text":143},{"id":403,"depth":293,"text":404},{"id":1729,"depth":293,"text":1730},{"id":1742,"depth":287,"text":1743,"children":3238},[3239],{"id":1812,"depth":293,"text":404},{"id":3045,"depth":287,"text":3046,"children":3241},[3242,3243,3244,3245],{"id":3052,"depth":293,"text":3053},{"id":3114,"depth":293,"text":3115},{"id":3156,"depth":293,"text":3157},{"id":3166,"depth":293,"text":3167},{"id":3173,"depth":287,"text":3174},{"id":3200,"depth":287,"text":3201},"2026-04-27","A practical guide to cyclomatic vs. cognitive complexity, covering testability, readability, nesting, guard clauses, and trade-offs.","md",null,"en",{},"\u002Fblog\u002Fcyclomatic-complexity-vs-cognitive-complexity",{"title":5,"description":3249},"blog\u002Fcyclomatic-complexity-vs-cognitive-complexity",[3258,3259,3260],"Coding","Complexity","Software Engineering","LHNlxT68xUNH5AwcMeKBaZfNK_L-287Zyx-RUVgCmzw",1777323149116]