0

I'm comparing records based on their dates, if the date is equal to a particular month its data goes to a particular object. Example: If this the record is in the current month then it will be saved in the current month field. If the record is in the previous month then it will also be saved in the previous month field. Now, I have two sets of code snippets. Which is heavy when being executed:

First Snippet: (declaring variables)

DateTime prevDate = currentDate.AddMonths(-1),
                    nextDate = currentDate.AddMonths(1),
                    thirdDate = currentDate.AddMonths(2),
                    fourthDate = currentDate.AddMonths(3);

if (prevDate .Month == invoiceDetail.InvoiceDate.Month
     && prevDate.Year == invoiceDetail.InvoiceDate.Year)
{
   //code here
}

if (nextDate.Month == invoiceDetail.InvoiceDate.Month
     && nextDate.Year == invoiceDetail.InvoiceDate.Year)
{
   //code here
}

if (thirdDate.Month == invoiceDetail.InvoiceDate.Month
     && thirdDate.Year == invoiceDetail.InvoiceDate.Year)
{
   //code here
}

if (prevDate .Month == invoiceDetail.InvoiceDate.Month
     && prevDate.Year == invoiceDetail.InvoiceDate.Year)
{
   //code here
}

Second Snippet: (directly insert them in the if statement)

if (currentDate.AddMonths(-1).Month == invoiceDetail.InvoiceDate.Month
     && currentDate.AddMonths(-1).Year == invoiceDetail.InvoiceDate.Year)
{
   //code here
}

if (currentDate.AddMonths(1).Month == invoiceDetail.InvoiceDate.Month
     && currentDate.AddMonths(1).Year == invoiceDetail.InvoiceDate.Year)
{
   //code here
}

if (currentDate.AddMonths(2).Month == invoiceDetail.InvoiceDate.Month
     && currentDate.AddMonths(2).Year == invoiceDetail.InvoiceDate.Year)
{
   //code here
}

if (currentDate.AddMonths(3).Month == invoiceDetail.InvoiceDate.Month
     && currentDate.AddMonths(3).Year == invoiceDetail.InvoiceDate.Year)
{
   //code here
}

2 Answers 2

1

Other than some mistakes in the code (i.e. the year and month are not properties but methods), I'd say the 1st is the most efficient.

Even though in the 2nd snippet there's possibility for short-circuiting, the 2 conditions in each if() are based on the same evaluated date. So you need the dates to be already evaluated by default - can't take advantage of any short-circuit. (Read http://en.wikipedia.org/wiki/Short-circuit_evaluation if needed)

So the 1st approach is the way/the more efficient: with that, you "addMonths()" only once per condition, instead of doing it twice inline directly in the if().

1

I would be inclined to introduce local variables for invoiceDetail.InvoiceDate.Month and invoiceDetail.InvoiceDate.Year. It may be the case that the Apex compiler recognises that you are accessing the same prperties repeatedly, but then again, it may not.

1
  • I'm using WSDL, the snippet is part of the program we're building using .NET. invoiceDetail is an object that is part of the this program that has sets of properties. So i don't think this would be the concern of the Apex compiler.
    – rpm07
    Commented Nov 7, 2014 at 14:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .