Skip to content

Instantly share code, notes, and snippets.

@dfinke
Last active December 28, 2025 23:09
Show Gist options
  • Select an option

  • Save dfinke/6266b12b6762c5f2a7893fc0c3ed9671 to your computer and use it in GitHub Desktop.

Select an option

Save dfinke/6266b12b6762c5f2a7893fc0c3ed9671 to your computer and use it in GitHub Desktop.

Cyclomatic Complexity Analysis

Cyclomatic Complexity is a software metric used to indicate the complexity of a program. The metric measures the number of linearly independent paths through a program's source code. A higher value means the code is more complex and potentially harder to test and maintain. Values above 10 are generally considered a warning sign.


Analysis of .\Samples\ComplexSample.ps1

  • Total Complexity: 11 (Threshold is typically 10)
  • File: .\Samples\ComplexSample.ps1

Insights

  • Value 11: Indicates this file exceeds recommended cyclomatic complexity.
  • The code likely contains multiple conditional branches (if, switch, loops), which make the program harder to test or reason about.
  • Files with high complexity should be considered for refactoring ΓÇô breaking down functions, simplifying logic, or separating concerns.

Details from the File

To provide the lines meeting complex criteria (e.g., branch points, decision statements), we need to identify the exact lines where decision points occur. However, since you haven't provided the content of ComplexSample.ps1, I'll provide a template for how this analysis would look if the source was available:

Line No. Code Snippet Complexity Contributor (Type)
5 if ($a -gt $b) Branch (if)
12 foreach ($item in $collection) Loop (foreach)
15 switch ($choice) Multi-branch (switch)
18 elseif ($x -eq 1) Branch (elseif)
20 case 1 { ... } Switch case
25 while ($continue) Loop (while)
28 if ($error) Branch (if)
29 try { ... } catch { ... } Exception (try/catch)
36 for ($i=0; $i -lt $count; $i++) Loop (for)
38 if ($flag) Branch (if)
42 return $result Path end (return)

(The list above is illustrative; actual lines may differ.)

Types contributing to complexity:

  • if, elseif, else
  • Loops: for, foreach, while
  • switch statements
  • Exception handling: try/catch

Suggestion

  • Refactor Large Functions: Split long methods into smaller ones.
  • Reduce Nested Logic: Flatten deep conditional or loop nesting.
  • Increase Readability: Add comments; rethink complex decision branches.
  • Unit Tests: Ensure comprehensive test coverage.

Want line-by-line analysis?
If you provide the actual code of ComplexSample.ps1, I can list the real lines contributing to cyclomatic complexity in table format.

Let me know if you'd like to proceed!

function ComplexFunction {
param($x, $y, $array)
if ($x -gt 10) {
Write-Host "x > 10"
}
elseif ($x -lt 5) {
Write-Host "x < 5"
}
else {
Write-Host "else"
}
switch ($y) {
1 { Write-Host "one" }
2 { Write-Host "two" }
default { Write-Host "other" }
}
for ($i = 0; $i -lt 5; $i++) {
Write-Host $i
}
foreach ($item in $array) {
if ($item -and $item.Length -gt 0) {
Write-Host $item
}
}
$someCondition = $true
while ($someCondition) {
# loop
$someCondition = $false
}
try {
# risky code
}
catch {
Write-Error "Caught an error"
}
}
# Example usage
$array = @("a", "b", "")
ComplexFunction -x 15 -y 2 -array $array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment