IConfiguration vs IOptions NET
Synchronous and Asynchronous in .NET Core
Model Binding and Validation in ASP.NET Core
ControllerBase vs Controller in ASP.NET Core
ConfigureServices and Configure methods
IHostedService interface in .NET Core
ASP.NET Core request processing
| async vs parallel processing :👈 | 👉:What are PDBs |
Cyclomatic Complexity |
Cyclomatic Complexity is a software metric that measures the number of independent execution paths through a program's source code.
It was introduced by Thomas McCabe and helps quantify how complex a piece of code is.
Formula:
Cyclomatic Complexity = Number of Decision Points + 1
Decision points include:
ifelse ifforwhiledo whilecase statementscatch?:)&&, ||) in some analysis toolspublic void Process()
{
Console.WriteLine("Hello");
}
No decision points:
Complexity = 1
There is only one execution path.
public void Process(int age)
{
if (age >= 18)
{
Console.WriteLine("Adult");
}
Console.WriteLine("Done");
}
One decision point:
Complexity = 2
Paths:
public string GetGrade(int marks)
{
if (marks >= 90)
return "A";
else if (marks >= 75)
return "B";
else
return "C";
}
Decision points:
ifelse ifComplexity = 3
Independent paths:
Higher complexity generally means:
Example:
if (...)
{
...
}
else if (...)
{
...
}
else if (...)
{
...
}
else if (...)
{
...
}
The more branches, the more difficult the code becomes to maintain.
Cyclomatic Complexity represents the minimum number of test cases needed to achieve full branch coverage.
Example:
Complexity = 5
At least 5 independent test cases are needed to exercise every path.
Very high complexity may indicate that a method should be split into smaller methods.
Example:
Complexity = 25
This is often considered a warning sign and may suggest the method violates the Single Responsibility Principle.
Static analysis tools such as:
often flag methods with excessive complexity because they are more error-prone.
| Complexity | Risk Level |
|---|---|
| 1-10 | Low risk, easy to maintain |
| 11-20 | Moderate complexity |
| 21-50 | High complexity, harder to test |
| >50 | Very high risk, should be refactored |
Cyclomatic Complexity is a software metric that measures the number of independent execution paths through a program. It is calculated based on the number of decision points such as
if,switch, and loops. It is important because it indicates code complexity, helps estimate the number of test cases required for full path coverage, identifies code that may need refactoring, and serves as a predictor of maintainability and defect risk.
| async vs parallel processing :👈 | 👉:What are PDBs |