Slaesforce FAQ

how to reduce cyclomatic complexity in salesforce

by Estelle Cruickshank Published 2 years ago Updated 2 years ago
image

To reduce cyclomatic complexity, you can replace the conditional constructs in your code with polymorphism. You may add a lot of flexibility to your code by leveraging polymorphism; your code becomes more testable, and you can add, update, or even delete statements without much change in your code.

Reducing Cyclomatic Complexity
  1. Use small methods. Try reusing code wherever possible and create smaller methods which accomplish specific tasks. ...
  2. Reduce if/else statements. Most often, we don't need an else statement, as we can just use return inside the 'if' statement.
Jul 26, 2019

Full Answer

How to reduce the cyclomatic complexity of a method?

Splitting up a big method into different classes will definitely reduce cyclomatic complexity. This might be useful for continuous inspection of code quality by sonarqube or other tools for static code analysis.

How can I reduce the complexity of my code?

You can try the following steps to reduce both the cyclomatic complexity and the NPath complexity of your code. Try reusing code wherever possible and create smaller methods which accomplish specific tasks. This can significantly reduce the number of lines and improve readability of your code.

How to reduce cyclomatic responsibility in Java?

Use the Single Responsibility principle for extracting a piece of code to other methods and make the method for just one responsibility. That change will reduce Cyclomatic Responsibility and improve maintainability for your code.

What is the difference between npath and cyclomatic complexity?

Similar to cyclomatic complexity, NPath complexity is also calculated for a method of a function. NPath complexity is the number of execution paths possible for a given code i.e. the number of ways the given code can get executed (ignoring cycles).

image

How can cyclomatic complexity be reduced?

Reduce the Number of Decision Structures You might consider this one a no-brainer. If the decision structures—especially if-else and switch case—are what cause more branches in the code, it stands to reason that you should reduce them if you want to keep cyclomatic complexity at bay.

What is cyclomatic complexity in Salesforce?

Cyclomatic complexity assesses the complexity of a method by counting the number of decision points in a method, plus one for the method entry. Decision points are places where the control flow jumps to another place in the program.

Does switch case reduce cyclomatic complexity?

Currently, every case in a switch block increments the cyclomatic complexity regardless of whether or not it falls through.

What is high cyclomatic complexity?

Cyclomatic complexity is a measure of the number of paths through a particular piece of code (a higher number means the software is more complex). Critical software functions with high cyclomatic complexity are one type of "spaghetti" code that should be avoided.

How can we reduce the complexity of a program?

Therefore, a developer must be very cautious while writing a new code, testing, or upgrading one to avoid complexity....Here are a few pointers that can simplify the code and reduce code complexity.Is decoupling necessary for your code? ... Adopt minimalism. ... Work on code readability. ... Take a break from SOLID.More items...•

How can time complexity of algorithm be improved?

Create a balanced binary tree sorting the numbers according to their value. This can be done so that each operation takes linear time. In each node store the sum of all values below that node.

How can we reduce the complexity of switch case?

Avoid use of switch/case statements in your code. Use Factory or Strategy design patterns instead. Complexity of 8 (1 for each CASE and 1 for method itself). Refactoring using Factory design pattern and complexity changed to 1.

How do you calculate cyclomatic complexity of a switch case?

Cyclomatic Complexity measures the total number of individual paths through the code at each level. This is basically calculated by counting the number of decision points (such as if blocks, switch cases, and do, while, foreach and for loops) and adding 1.

What is cyclomatic complexity in software testing?

The Cyclomatic complexity defines the number of independent paths in the basis set of the program that provides the upper bound for the number of tests that must be conducted to ensure that all the statements have been executed atleast once.

Why is cyclomatic complexity bad?

High cyclomatic complexity increases the risk of the presence of defects in the code due to increased difficulty in its testability, readability, and maintainability.

How important is cyclomatic complexity?

It can be used as a quality metric, gives relative complexity of various designs. It is able to compute faster than the Halstead's metrics. It is used to measure the minimum effort and best areas of concentration for testing. It is able to guide the testing process.

Is cyclomatic complexity useful?

Guide for unit testing I find cyclomatic complexity useful because it measures the number of pathways through a method. As such, it indicates the minimum number of test cases you ought to furnish. This is useful when reviewing code and tests.

How to model state in a program?

The common way to model state is to create a state variable inside object, which keeps different states , and to handle them in conditional statements . For transforming state diagram into programming code, you should perform a few steps: 1) Gather all states;

Is cyclomatic complexity too complex?

Cyclomatic complexity for this method is 19 and it’s too complex. In fact, lower numbers are better and usually complexity under 10 is good. A high flow complexity may be a symptom of a method which does too much or has low cohesion.

Cyclomatic Complexity

This complexity is calculated for a method or a function. The cyclomatic complexity of a method without any branching code, such as ‘if’, ‘for’, ‘while’, ‘case’, ‘switch’, ‘try-catch’ statements, in the method is one.

NPath Complexity

Similar to cyclomatic complexity, NPath complexity is also calculated for a method of a function. NPath complexity is the number of execution paths possible for a given code i.e. the number of ways the given code can get executed (ignoring cycles).

Reducing Cyclomatic Complexity

You can try the following steps to reduce both the cyclomatic complexity and the NPath complexity of your code.

Principal Example

Take a look at this classic example. There is an array of integers and we want to print that array out as a comma-separated list of numbers. The point about this exercise is in the separator placed between numbers:

Generalizing the Solution

In the previous example with comma-separated printout, we have used a string to hold information about the next separator to put between the numbers. This solution is quite satisfactory to the problem at hand. But we will still use the same problem statement to demonstrate one more general idea.

Real World Example

In this series of articles we were refactoring one simple e-commerce application. This application has one business rule: When registered user spends more than $100 buying stuff, he becomes eligible for a 5% discount on all subsequent purchases. This requirement is implemented in the User class like this:

Factory Methods Instead of Branching

Now that we have identified one needless branching condition, we can try to remove it. The first step will be to isolate the decision to create the loyalty discount:

Analysis

Drawback of this technique is that it is a bit cryptic and not that easy to figure the first time. Therefore it is probably not the best option in simple cases.

Conclusion

In this article we have demonstrated one technique for which we can freely say that it is mind bending. Instead of branching the code to decide which object to create at the given situation, we use factory methods. But, these are not ordinary factory methods. These methods are dynamically produced using lambda expressions.

What is cyclomatic complexity?

Cyclomatic Complexity is a metric created by Thomas J. McCabe in 1976 to measure source code to check how complex it is. That metric goes from 1 to N, it means that there are no limits. Low values are better, so for greater values be careful, your code can be complex and difficult to maintain.

Does cyclomatic complexity affect performance?

Can Cyclomatic Complexity affect the performance of an application? The answer is Yes and No. It will affect performance if the complexity value is more than 50 and still depends on the kind of commands you are using. You will need to run a performance benchmark in your code to check that.

image
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1 2 3 4 5 6 7 8 9