Overview
This article is a collection of all the various App Script best practice guidelines mentioned throughout the App Script articles. Refer to the points listed below for more information regarding the suggested Best Practices to follow when writing App Scripts.
App Script Event Types Best Practices
When an "After Event" script makes changes, those changes must progress through the order of operations again until reaching the Save Event, which can re-trigger "Before Event" scripts. If not carefully scoped, this can lead to repeated execution and potential timeout errors.
π₯ To avoid this, ensure "Before Event" scripts are narrowly targeted and efficient.
Script Writing - General Best Practices
Refer to the Script Writing - Basic Concepts article for more information and examples.
π₯ Use Commenting to provide clarifying details and to activate/deactivate code.
- Typing
//
at the beginning of a line of code will cause that line to be "commented out" or deactivated, and the App Script will not read it or perform any actions from that line of code.
- For single-line commenting, use//
- For multi-line or interline commenting, use/**/
- You can also highlight a section of multiple lines of code and press CNTRL+ / to comment those lines in or out simultaneously.
π₯ Above each section of script, define the script's objective, include the date the section of script was written, and the script's author.
π₯ Variables should be named in camelCase: the first word is in lowercase, and all following words begin with an uppercase letter (e.g. dueDate) which helps to provide clarity while reading the script.
π₯ Use Constants to prevent scripts from throwing errors if an App name, Table name, or Field name is changed. (see Constants section in this article for more information).
Script Writing - Best Practices for Using Variables
Refer to the Script Writing - Using Variables article for more information and examples.
π₯ We recommend using BigDecimal
instead of Integer when performing any math with variables.
π₯Best Practices for using def
:
- Use descriptive variable names to improve readability.
- Use def
when the data type is obvious from the context to keep your code clean and concise.
- Use type casting when working with numbers or dates to help avoid errors in math or comparisons.
Script Writing - Best Practices for Using Constants
Refer to the Script Writing - Using Constants article for more information and examples.
π₯ Use Constants instead of "hardcoding" field names and Relationships as Variables in the script to avoid breaking the script if the field name is ever changes in the future.
π₯Use clear, descriptive names written in camelCase for your Constants.
π₯Use Commenting //
to organize Constants by their Table, and to clarify the field type.
π₯ Since both the Relationship field and the Relationship have the same name, be sure to clearly differentiate which Constant is for the each one by appending differentiators to the end of each Constant name (e.g. stateRelField
as the name for the State Relationship field, and stateLink
as the name for the State Relationship).
π₯ When used in Functions, Constants should be enclosed in parentheses ()
instead of being wrapped in quotes ""
like static field names.
Script Writing - Best Practices for Using Loggers
Refer to the Script Writing - Using Loggers article for more information and examples.
π₯Use Loggers Early and Often During Development: Use logger.info()
to track values at different stages while building out your script.
π₯Check for null Before Logging: Avoid null
value confusion or errors in logs by checking for null
before logging.
π₯Use Loggers to Check Conditional Logic: When using if
statements or branching logic, log the path taken.
π₯Use Loggers to View Results Returned: If your script loops through records, you can log the number of items returned.
π₯Use Meaningful Labels in Logger Messages: Make it easy to search and understand logs by labeling what you're logging.
π₯User Loggers Sparingly in Production: It is important to keep in mind that loggers tend to slow down the execution time of the script and so it is recommended to use them sparingly. You can leave loggers commented out in your script so that they are not running each time the script executes, and then if the script begins throwing errors, you can temporarily comment the loggers back in to use them while troubleshooting the script errors.
π₯Checking Edge Cases: Use logs to trace values when exceptions could occur.
π₯Avoid Logging Sensitive Data: Don't log confidential or personal information (e.g. passwords, SSNs) as the error logs may be accessible by other admins.
Related Articles:
- App Script Limitations
- Script Writing - Syntax Rules
- Script Writing - Basic Concepts
- App Script Troubleshooting Basics
Comments
0 comments
Please sign in to leave a comment.