Development Best Practices
  • Home
  • Clean Code
  • Apex
    • Naming Conventions
    • Triggers
    • Unit Testing
    • Principles
      • KISS
      • DRY
      • Single Responsibility
      • Open/Closed
      • YAGNI
    • Patterns
    • SOQL
    • Exception Handling
    • Magic Strings
  • Lightning Components
    • Aura
    • LWC
  • Automation
    • Process Builder
  • Salesforce DX and VSCode
  • Git Source Control
    • Install Git
    • Using Git
    • Development Workflow
      • The Rules
    • Feature Branch Workflow
Powered by GitBook
On this page
  • Summary
  • Easily understandable by human beings
  • Keep your methods small
  • Avoid Ternary Operators
  1. Apex
  2. Principles

KISS

Keep it simple stupid

Summary

Your code should be simple and straightforward, so its easily understandable by human beings.

It does not mandate that you sacrifice on maintainability, extensibility, or features

Easily understandable by human beings

Consider the below example:

Easy to understand

public String weekday(Integer day) {  
    switch (day) {  
        case 1:  
            return 'Monday';  
        case 2:  
            return 'Tuesday';  
        case 3:  
            return 'Wednesday';  
        case 4:  
            return 'Thursday';  
        case 5:  
            return 'Friday';  
        case 6:  
            return 'Saturday';  
        case 7:  
            return 'Sunday';  
        default:  
            throw new CustomException('day must be in range 1 to 7');  
    }  
}

Complex to understand

public String weekday(Integer day) {  
    if ((day < 1) || (day > 7)) throw new CustomException('day must be in range 1 to 7');  
    string[] days = {  
        'Monday',  
        'Tuesday',  
        'Wednesday',  
        'Thursday',  
        'Friday',  
        'Saturday',  
        'Sunday'  
    };  
    return days[day - 1];  
}  

Keep your methods small

Keep functions as small as possible and make sure they do just one thing, rather have ten functions doing just one thing than one function doing ten things.

If you have a lot of conditions in the method, break these out into smaller methods. It will not only be easier to read and maintain but also can find bugs a lot faster.

Avoid Ternary Operators

Avoid putting ternary operators within blocks of code.

Ternary operators are best used within small methods, where the method named explains the purpose.

public Boolean isOpportunityImportant(Opportunity deal) {
    return (deal.Amount > 1000000) ? true : false;
}

PreviousPrinciplesNextDRY

Last updated 5 years ago