Ensuring Your Scripts Are Easy to Maintain

When writing a script, your first instinct is to get your business logic working correctly. Over time, as you iteratively add functionality, the script for a complex business process can grow very long.

However, Oracle recommends limiting each script to 400 lines. Since one script can invoke other functions, in practice this restriction does not hamper your ability to solve business problems. It is always possible to decompose a lengthy script into a shorter alternative that invokes other object functions or global functions as needed.

For example, instead of writing a 600–line trigger script, create a shorter trigger that invokes other object functions or global functions. In turn, if one of these functions starts getting long, reorganize its code into additional, smaller functions that the original function can invoke. For each function you create, choose a meaningful name that describes the task it performs.

Suppose you have written a “Before Insert” trigger that executes before each new PurchaseOrder object is created. Imagine it contains a large amount of code that conditionally creates a default set of LineItem child objects for new orders. To avoid exceeding the 400–line limit and improve readability of your code, reorganize it into two object functions named requiresDefaultLineItems() and createDefaultLineItems(). Then rewrite the original trigger to be:
// Before Insert Trigger on PurchaseOrder
if (requiresDefaultLineItems()) {
  createDefaultLineItems()
}

By following these recommendations, your code will avoid “Code too large!” errors and will become much easier for colleagues to understand and maintain as well.