0

There is an Apex method where are are checking whether the status of the campaign is Extinct or not:

public static Boolean isExtinct(Campaign vCampaign) {
        return (vCampaign.Status__c == 'Extinct');
    }

There is another Apex method with similar logic, the only difference is that we have added one more condition to check whether Is_Logged_in_User__c is true or not.

public static Boolean isExtinctForLiggedInUser(Campaign vCampaign) {
        return (vCampaign.Status__c == 'Extinct'
                && vCampaign.Is_Logged_in_User__c);
    }

As you see, I have defined 2 methods. Is it possible to define only one method, and based on some Boolean parameter, I can switch the logic?

I can use if-else condition, but I am looking for a cleaner approach.

1
1

You can use a combination of Boolean logic:

public static Boolean isCampaignExtinct(Campaign vCampaign, Boolean forCurrentUser) {
    return vCampaign.Status__c == 'Extinct' && (!forCurrentUser || vCampaign.Is_Logged_in_User__c);
}
4

If both methods are used, then keep them both as their names are quite descriptive. Below is one way to re-use a bit of the logic. There are lots of options.

public static Boolean isExtinct(Campaign vCampaign) {
    return vCampaign.Status__c == 'Extinct';
}

public static Boolean isExtinctForLoggedInUser(Campaign vCampaign) {
    return isExtinct(vCampaign) && vCampaign.Is_Logged_in_User__c;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.