← All articles
◈ Tech & ServiceNowArchitectureServiceNow

Clean Implementation

Posted on 7 Jul 2022 · 2 min read

We can learn so many things from software development and architecture principles. Writing clean code is crucial in software development. But ServiceNow is a low-code platform — how does this apply?

Let us look at the tenets of clean code and see how we can apply them to our ServiceNow implementation.

Reliability

If new features work half the time, we're probably better off without them. For a function, feature, or implementation to be reliable, it must be correct, stable, and resilient.

  • Correct — the code (client script, business rule, flow) conforms to the requirement
  • Stable — behaviour is consistent given different valid inputs and situations
  • Resilient — the implementation avoids failure; it keeps working when things go wrong

Examples of unreliable implementation:

  • A client script that uses DOM manipulation
  • A UI policy that sometimes works and sometimes doesn't due to an ordering issue (not stable)
  • A flow with no retry policy for REST/SOAP calls — if a network issue occurs, the flow simply stops (not resilient)

Efficiency

Resources are finite. When we design solutions in the NOW Platform, we should do so with efficiency in mind. Two dimensions to consider: time and space.

Time — how long does it take to perform an action?

Consider these two blocks of code that both count P1 incidents:

// Block 1 — Inefficient
var count = 0;
var gr = new GlideRecord('incident');
gr.addQuery('category', 'software');
gr.query();
while (gr.next()) {
  count++;
}
gs.info('COUNT: ' + count);
// Block 2 — Efficient
var gr = new GlideAggregate('incident');
gr.addQuery('category', 'software');
gr.addAggregate('COUNT', 'sys_mod_count');
gr.query();
if (gr.next()) {
  gs.info('COUNT: ' + gr.getAggregate('COUNT', 'sys_mod_count'));
}

Both are reliable, but Block 1 loops through every record unnecessarily. In a large organisation, these inefficient patterns accumulate and negatively affect platform usability.

Space — how large are the things you're creating?

When building a portal with images, optimise image size so it doesn't affect page load time. Small savings compound.

Maintainability

Maintainability refers to the ease with which appropriate changes can be made. A classic example: a hard-coded value in a business rule vs. a system property.

// Hard-coded — brittle
gr.assignment_group = '4baec2fe07d3f0101263f19d7c1ed0a6';

// Maintainable — configurable
gr.assignment_group = gs.getProperty('incident.default_assignment');

Ask yourself during implementation:

  • Is my solution rigid? How easy is it to extend?
  • How fragile is it — if I change one thing, what else breaks?

Usability

Usability is about creating a good experience — for end-users and for technical users. It's not only about fulfilling requirements; it's about enabling users to achieve their goals with minimal time and effort.

For technical users, usability means following consistent patterns:

  • Always use comments to explain what your code does
  • Name your client scripts, business rules, and flows clearly to indicate their function
  • Link update sets to the original user story: e.g., STRY0010001-New Incident Caller-02022022

Clean code isn't about perfection — it's about leaving things better than you found them.