Tuesday, February 17, 2015

Keeping your OnInput code DRY in Zoho Creator

DRY (don't repeat yourself) is one of the most important software development patterns to follow. If you copy and paste code, then if there's a bug, you'll have to go back and edit several code blocks which is likely to lead to even more bugs.

With Zoho Creator OnInput scripts, it can be challenging to apply DRY because you can't really use functions within an OnInput script. You can call a function at an app level, but this function won't be able to access any of the current form fields.

Here's an example I encountered recently when reviewing code from one of my devs.

As you'll read from the comments, this code isn't DRY. The same exact code block was used for several other OnInput functions. (The goal of this block is to update a subtotal field at the bottom of the form when the quantity or price is changed).

As mentioned, you can't use a custom function to achieve this, but you can put the code in one place. I typically create a new decision box field (e.g. Update_Sub_Totals) and put the code in there.

if (input.Update_Sub_Totals == true){
             //update sub-total code
             code block goes here
             //reset the check box so it can be used
             input.Update_Sub_Totals = false;

No comments:

Post a Comment