Optimizing and refactoring Apex code
During many Salesforce projects, there is a metric that is usually the most difficult to refactor: cyclomatic complexity. Cyclomatic complexity is a software metric that’s used to indicate the complexity of a program. It’s calculated by developing a control flow graph of the code and then determining the number of paths through the graph. Higher values indicate more complex and potentially more error-prone code.
Let’s have a look at a class with high cyclomatic complexity:
public class HighComplexityClass { public void complexMethod(Integer val) { if (val == 1) { System.debug('Value is 1'); } else if (val == 2) { System.debug('Value is 2')...