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;
}

Last updated