DEV Community

Luis Rodrigues
Luis Rodrigues

Posted on

.NET: Call Func delegates from Dictionary objects

How about store Func delegates in a Dictionary object and invoke them when you really need them?

This approach can help us to avoid having nested conditional in our code and keep its cyclomatic complexity lower.

“Cyclomatic complexity is a software metric used to determine the complexity of a program”. Click here to know more about that.

For example, suppose we want to create a method that returns a result from a math operation between two numbers. Its result will depend on the operation type that the user will provide to us: addition, subtraction, multiplication, and division.

A simple way to solve that is by creating a method that accepts three parameters: a number that will apply some operation and the operation type. Like this:

public class MyNewMath
{
public static int DoSomeCalc(OperationType operationType, int numberOne, int numberTwo)
{
int result = 0;
Dictionary<OperationType, Func<int, int, int>> operations = new Dictionary<OperationType, Func<int, int, int>>
{
[OperationType.Addition] = new Func<int, int, int>((x, y) => x + y),
[OperationType.Subtraction] = new Func<int, int, int>((x, y) => x - y),
[OperationType.Multiplication] = new Func<int, int, int>((x, y) => x * y),
[OperationType.Division] = new Func<int, int, int>((x, y) => x / y)
};
if (operations.TryGetValue(operationType, out Func<int, int, int> operation))
{
result = operation.Invoke(numberOne, numberTwo);
}
return result;
}
public enum OperationType
{
Addition,
Subtraction,
Multiplication,
Division
}
}

As you can see, we need to add four conditions to this method. From that, we have the cyclomatic complexity metric below:

nested conditions

Now check the same method with a Dictionary type that stores Func delegates:

public class MyNewMath
{
public static int DoSomeCalc(OperationType operationType, int numberOne, int numberTwo)
{
int result = 0;
Dictionary<OperationType, Func<int, int, int>> operations = new Dictionary<OperationType, Func<int, int, int>>
{
[OperationType.Addition] = new Func<int, int, int>((x, y) => x + y),
[OperationType.Subtraction] = new Func<int, int, int>((x, y) => x - y),
[OperationType.Multiplication] = new Func<int, int, int>((x, y) => x * y),
[OperationType.Division] = new Func<int, int, int>((x, y) => x / y)
};
if (operations.TryGetValue(operationType, out Func<int, int, int> operation))
{
result = operation.Invoke(numberOne, numberTwo);
}
return result;
}
public enum OperationType
{
Addition,
Subtraction,
Multiplication,
Division
}
}

In this case, we don’t need a lot of “if” statements in our code and it’s getting easier to read. Also as you can see in the picture below our cyclomatic complexity is lower than in the first approach:

without nested conditions

Of course, this sample is so simple. Now, try to think how easier can be our life in complex scenarios where we need to support code with higher cyclomatic complexity.

How about buying me a coffee? :D

buy me a coffe?

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay