The Now Platform® Washington DC release is live. Watch now!

Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Dawn Jurek
ServiceNow Employee
ServiceNow Employee

Has this ever happened to you? You test out a block of carefully crafted code, only to find that the results are totally unexpected. After painstakingly tracking down the problem, it turns out that one of your seemingly benign variables trampled on a variable already in use in the system. So what's the best way to avoid this? In this eleventh installment of our best practices series, we look at how functions can help you tame your variables.

What are functions and why should you use them?

Functions are self-contained modules of code that perform a particular task. In a ServiceNow instance, they're commonly used in business rules, client scripts, script includes, and anywhere else you can write code. Functions serve to encapsulate, or isolate, code so that the variables therein don't go running amok elsewhere in the system.

Here's an example of code encapsulated in a function, contrasted with unencapsulated code:

Encapsulate code in functions example.png

There are many reasons to encapsulate code in functions, but in the ServiceNow platform, one of the main concerns is variables "stepping on" one another—that is, taking on incorrect values outside their intended scope. When a variable is defined outside a function, its scope is global by default. If your non-encapsulated script uses a variable that's already in use by the system, the variable could take on a value that produces unexpected results whose causes are very difficult to track down.

Encapsulating code in functions helps you keep your variable namespace clean, so that variables in your script stay local and don't interact with global variables.

Functions also offer these benefits:

  • Code reuse. The function can be called from many different places in the system, including from inside other functions.
  • Ease of testing and debugging. Functions allow you to test and debug code in small chunks.
  • Ease of maintenance. It's easier to update code and add features when that code is self-contained in the function it performs.

Coding best practice

Encapsulate all your code in functions to keep variables from going rogue, to facilitate code reuse, and to make testing and maintenance easier.

--

Behind the scenes here at ServiceNow, the Knowledge Management and Multimedia teams work closely with subject matter experts to disseminate critical information to our customers. We've found that certain topics come up frequently, in the form of best practices that can help you keep your ServiceNow instances running smoothly. This series aims to target those topics so that you and your organization can benefit from our collective expertise. If you have a best practices topic you'd like us to cover in this series, please let us know in the comments below.

To access all of the blog posts in this series, search for "nowsupport best practices series."

1 Comment