Functional decomposition

Usually, a developer starts building a trigger or page controller and ends up writing all the business logic within the trigger or controller itself. It is definitely easy and fast but hard to maintain.

Say, for example, we have to develop a Visualforce page with the custom functionality of generating an invoice. So, we begin our controller, as follows:

public class GenerateInvoiceController{ 
     
    public Invoice__c Invoice {get; set;} 
     
    //generate invoice 
    public void generateInvoice(){ 
         
        if(validate(Invoice )){ 
            insert Invoice ; 
        } 
        else{ 
            // handle validation errors 
        } 
    } 
     
    //validate invoice 
    public Boolean validate(Invoice__c Invoice){ 
        Boolean isValid = false; 
         
        // validation logic 
         
        return isValid; 
    } 
} 

A key thing that is missing here is ...

Get Apex Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.