Jump to content

Making this more concise and dynamic


Recommended Posts

I just had a job that was a coupon where each card had a unique code, and they wanted the job delivered in multiple sets of differing quantities. Here is the code I used to successfully produce the job:

 

// 4 sets of 380

if ((CurrentRecordNumber() >= 1) && (CurrentRecordNumber() <= 1520))

   {   
       SetQuantity = 380
   }

// 9 sets of 350

else if ((CurrentRecordNumber() >= 1521) && (CurrentRecordNumber() <= 4670))

   {   
       SetQuantity = 350
   }

// 34 sets of 255

else if ((CurrentRecordNumber() >= 4671) && (CurrentRecordNumber() <= 13340))

   {   
       SetQuantity = 255
   }

// 26 sets of 215

else if ((CurrentRecordNumber() >= 13341) && (CurrentRecordNumber() <= 18930))

   {   
       SetQuantity = 215
   }

// 28 sets of 170

else if ((CurrentRecordNumber() >= 18931) && (CurrentRecordNumber() <= 23690))

   {   
       SetQuantity = 170
   }

// 11 sets of 115

else if ((CurrentRecordNumber() >= 23691) && (CurrentRecordNumber() <= 24955))

   {   
       SetQuantity = 115
   }

// 1 set of 45

else if ((CurrentRecordNumber() >= 24956) && (CurrentRecordNumber() <= 25000))

   {   
       SetQuantity = 45
   }

if (SetCounter == 1)

   {
       FusionPro.Composition.SetBodyPageUsage('divider', true)
   }

if (SetCounter < SetQuantity)
   {
       SetCounter++;
   }
else
   {
       SetCounter = 1;
   }

 

In javascript globals I have defined SetCounter to 1 and SetQuantity to 0.

 

Is there a way to make this more concise, and to determine the record numbers in a more dynamic way? I see this as being a repeating job in the future, but with differing set quantities and amounts per set. It would be nice to just enter the number of sets and quantities per set and have code to figure out the record numbers.

 

I would appreciate any guidance anyone is willing to share!

 

Thank you...

Link to comment
Share on other sites

Great question! Sure, this logic can be reduced in several ways.

 

First, since you're in an "if...else" chain, you know that each block is gated by the one before. For instance, if the number is less than 1521, you'll go to the first case, so that next "if" doesn't need to re-check that it's less that 1521 again, and so on. And you don't need to check that the number is at least one at the start; it always is, by definition. You can also get rid of those braces since each statement block is just a single statement. Also, you probably want a final "else" at the end, just in case. So, skipping the "SetCounter" logic at the end, the first reduction is:

// 4 sets of 380
if (CurrentRecordNumber() <= 1520)
        SetQuantity = 380
// 9 sets of 350
else if (CurrentRecordNumber() <= 4670)
       SetQuantity = 350
// 34 sets of 255
else if (CurrentRecordNumber() <= 13340)
       SetQuantity = 255
// 26 sets of 215
else if (CurrentRecordNumber() <= 18930)
       SetQuantity = 215
// 28 sets of 170
else if (CurrentRecordNumber() <= 23690)
       SetQuantity = 170
// 11 sets of 115
else if (CurrentRecordNumber() <= 24955)
       SetQuantity = 115
// 1 set of 45
else if (CurrentRecordNumber() <= 25000)
       SetQuantity = 45
else
       SetQuantity = 30

The next thing I would do is set this logic up for debugging. This means that you want to be able to test various input values to make sure that the output value is correct. I would do something like this:

var recNum = CurrentRecordNumber();

recNum = 5000; // debugging

// 4 sets of 380
if (recNum <= 1520)
        SetQuantity = 380
// 9 sets of 350
else if (recNum <= 4670)
       SetQuantity = 350
// 34 sets of 255
else if (recNum <= 13340)
       SetQuantity = 255
// 26 sets of 215
else if (recNum <= 18930)
       SetQuantity = 215
// 28 sets of 170
else if (recNum <= 23690)
       SetQuantity = 170
// 11 sets of 115
else if (recNum <= 24955)
       SetQuantity = 115
// 1 set of 45
else if (recNum <= 25000)
       SetQuantity = 45
else
       SetQuantity = 30

return [recNum, SetQuantity]; // debugging

Now, you can set the number to whatever you want on line 3, and click Validate to see if you get the result you want, right in the Rule Editor, without having to compose. Once you get the logic right, you can just comment out that line that sets the number, and the return line that validates it (with the "debugging" comments).

 

Now we can try other ways of reducing this code. There are probably different schools of thought about this, but here's where I would land:

var recNum = CurrentRecordNumber();

//recNum = 20000; // debugging

var Quantities = 
{
   1520: 380, // 4 sets of 380
   4670: 350, // 9 sets of 350
   13340: 255, // 34 sets of 255
   18930: 215, // 26 sets of 215
   23690: 170, // 28 sets of 170
   24955: 115, // 11 sets of 115
   25000: 45, // 1 set of 45
   0: 30 // default
};

for (var q in Quantities)
{
   if (recNum <= q || q == 0)
   {
       SetQuantity = Quantities[q];
       break;
   }
}

//return [recNum, SetQuantity]; // debugging

Basically, this abstracts the logic of "if x is less than or equal to y, return z" for different values of y and z. Now, you can see all the values in one little table, and it's easy to change them without modifying multiple lines of code. This is an example of the programming principle called DRY, for "Don't Repeat Yourself," basically, try to have the logic written in one definitive place.

Edited by Dan Korn
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...