0

I have the following code (which works just fine) but am curious as to the structure of it regarding my If statements. I suppose I could be using IF-Else Statements but am not sure why that would be preferable to what I have. Here is my code:

date dt = system.date.today().addDays(30);
    date qbr = system.date.today().addDays(90);

    List<Task> tList = new List<Task>();
    RecordType rt = [SELECT Id FROM RecordType WHERE Name = 'Publisher Org' AND sObjectType = 'Account' Limit 1];
    List<Account> acctList = [SELECT Id,Name, Assigned_to_PubOps__c,Primary_Contact__c, OwnerId, Next_QBR__c, Account_Status__c 
                              FROM Account WHERE Assigned_to_PubOps__c != null AND RecordTypeId = :rt.id AND Account_Status__c = 'Active'];
    List<Account> AcctUpList = new List<Account>();

    for(Account a : acctList){
        dt = date.newInstance(system.date.today().year(), system.date.today().month(), a.Assigned_to_PubOps__c.day());

        if(a.Assigned_to_PubOps__c.day() > 28){
            dt = date.newInstance(system.date.today().year(), system.date.today().month(), 28);
        }
        if(a.Assigned_to_PubOps__c.day() < 8){
            dt = date.newInstance(system.date.today().year(), system.date.today().addMonths(1).month(), a.Assigned_to_PubOps__c.day());
        }
        if(a.Next_QBR__c != null && a.Next_QBR__c.day() < 29){
            qbr = a.Next_QBR__c;
        }
        if(a.Next_QBR__c != null && a.Next_QBR__c.day() > 28){
            qbr = date.newInstance(a.Next_QBR__c.year(), a.Next_QBR__c.month(), 28);
        }
        if(a.Next_QBR__c.day() < 8){
            dt = date.newInstance(a.Next_QBR__c.year(), a.Next_QBR__c.addMonths(1).month(), a.Next_QBR__c.day());
        }
        if(system.date.today().daysBetween(dt) == 7 && system.date.today().daysBetween(qbr) != 7){
            Task tsk = new Task();
            tsk.WhoId = a.primary_Contact__c;
            tsk.WhatId = a.id;
            tsk.ActivityDate = dt;
            tsk.IsReminderSet = True;
            tsk.Status = 'Not Started';
            tsk.Subject = 'Monthly Check-In for '+a.Name;
            tsk.OwnerId = a.OwnerId;
            tsk.ReminderDateTime = datetime.newInstanceGmt(dt.year(), dt.month(), dt.day(), 08, 00, 00);
            tList.add(tsk);
        }
        else if(system.date.today().daysBetween(qbr) == 7){
            Task tsk = new Task();
            tsk.WhoId = a.primary_Contact__c;
            tsk.WhatId = a.id;
            tsk.ActivityDate = dt;
            tsk.IsReminderSet = True;
            tsk.Status = 'Not Started';
            tsk.Subject = 'Quarterly Business Review for '+a.Name;
            tsk.OwnerId = a.OwnerId;
            tsk.ReminderDateTime = datetime.newInstanceGmt(dt.year(), dt.month(), dt.day(), 08, 00, 00);
            a.Next_QBR__c = dt.addDays(90);
            tList.add(tsk);
            acctUpList.add(a);
        }
    }
    if(tList.size() > 0){
        insert tList;
    }
    if(acctUpList.size() > 0){
        update acctUpList;
    }        
}

Would it be best to use If-Else statements when setting the date variables instead of consecutive If statements?

2
  • This functionality would be a lot more clear if you broke it out into separate methods that each have one specific concern. E.g. getQBR, getSomeOtherDateWithMeaning, buildTask, etc.
    – Adrian Larson
    Sep 29 '15 at 16:48
  • else-if prevents you from inadvertently executing two "ifs" if your conditions aren't mutually exclusive. In some sense, you help avoid the next developer (or yourself) from screwing up.
    – cropredy
    Sep 29 '15 at 16:49
5

Here are two similar questions that aren't specific to Apex:

https://stackoverflow.com/questions/7970275/performance-difference-of-if-if-vs-if-else-if https://stackoverflow.com/questions/3790177/if-vs-else-if

I assume you understand the functional difference, so I won't discuss that.

In terms of performance, there is a potential disadvantage to if vs else if: you're forcing the server to check every subsequent if condition, whereas else if will only check the condition if your first condition was not met.

More important is the semantic difference. We want to write code whose intended functionality can be easily understood. This way, when we (or others) need to debug or extend our code years down the line, we can understand it and fix it.

If you use multiple if statements, future you does not know that you intend for only one conditional block to apply. Therefore, future you has to read every subsequent if condition to understand if more than one of them applies. If you use else if, future you immediately knows that none of the subsequent if statements will apply.

So do yourself a favour and use else if if you mean else if, even if it's more verbose to write in the short term.

0

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.