Let's get started with the following set of questions:
Does this unit of code have more than four
if
statements?Methods with more than four
if
statements (if()
,else if()
,else()
) pose a number of related problems. First, due to the verbosity of Apex, numerousif
statements will necessarily mean longer methods. Second, because each statement creates another logical path through the method, each also represents at least one additional test case to be written. The fewer the number of logical paths through your code, the simpler it is to understand, test, and refactor. Unfortunately, sometimes the business logic we are calculating requires a number of conditionals. For instance, calculating tax for goods sold needs to know the category of product, the buyer's tax exempt status, the price, and the location it was sold. That's easily four or moreif
statements. Look at this example:public Double calculateTax(Id goodsSold, Account buyer){...