Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Smells like Code Duplication - Spotting Violations of the DRY Principle

DZone's Guide to

Smells like Code Duplication - Spotting Violations of the DRY Principle

Consider whether the code in your project is effective and concise, or complex and superfluous by testing it for DRY-compliance.

· Agile Zone ·
Free Resource

Discover how TDM Is Essential To Achieving Quality At Speed For Agile, DevOps, And Continuous Delivery. Brought to you in partnership with CA Technologies

Is Your ServiceNow Instance DRY...?

Adherence to the Don't Repeat Yourself (DRY) principle is one of the key requirements for building a robust software system. This principle was formulated by Andy Hunt and Dave Thomas in their book The Pragmatic Programmer. They define it in the following way:

"Every piece of knowledge must have a single, unambiguous, authoritative representation within a system."

While the context for which The Pragmatic Programmer was written is that of traditional software development of multi-tiered applications, it certainly also applies to low-code development platforms such as ServiceNow. Indeed, the argument could be made that since ServiceNow and other low code development platforms require less code to be written to implement any given functionality, introducing code duplication in these platforms should be considered a more severe violation of best practices than in "traditional" software development environments.

...or WET?

However one may feel about this conceptually, the fact remains that no one likes fixing the same bug twice, or even worse, releasing a fix for a bug into production only to have it show up again because a piece of duplicated code had not been identified and corrected. In fact, the opposite of a DRY system is often referred to as a WET one, with this acronym taking on the somewhat humorous meanings of:

"Write Everything Twice"

"We Enjoy Typing"

or

"Waste Everyone's Time"

So whatever your role in a ServiceNow SaaS platform may be, as a manager, an architect, or a developer, it is certainly in your best interest to keep your instance as DRY as possible.

Unfortunately, until now there were not many tools you could use to ensure that this was the case, other than manual code reviews, which are not an effective tool to detect code duplication anyway, unless the duplicated code is in the same Configuration Element.

Detecting Code Duplication with Quality Clouds for ServiceNow

With Quality Clouds for ServiceNow you now have a tool which can effectively assist you in identifying duplicated code. Specific code duplication indicators are in the development pipeline for this quarter, but you can already use the information generated by a Quality Clouds scan to identify duplicated code and duplicated code elements in your instance. From our experience working with various customers who are already taking advantage of this feature, the Service Catalog is an area where code duplication is seen often, as Catalog Client Scripts especially are repeatedly copied and pasted to be associated with slightly different Catalog Items. This is certainly an area where it pays to be especially vigilant before the amount of code duplication builds up.

There are two main ways to detect possible code duplication with Quality Clouds for ServiceNow. They both involve analyzing the list of detected issues, as follows:

Look for duplicated Configuration Elements—these are likely to hold duplicate code:

  1. Once on your Quality Clouds for ServiceNow platform, navigate to the Team overview dashboard.
  2. Scroll down to the detailed issues table and export it to Excel.
  3. Use Excel's conditional formatting feature to highlight Configuration Elements with the same name.
  4. Any rows with the same Configuration Element type are likely Configuration Elements that have been copied and pasted whole, keeping even their names.

Bingo! You've just sniffed out a code duplication. Follow the links to the exact code lines from our exported sheet to confirm and rewrite as required.

Look for repeating patterns of issues across different Configuration Elements—these are likely to be duplicated code blocks:

  1. Once on your Quality Clouds for ServiceNow platform, navigate to the Application overview dashboard.
  2. Scroll down to the detailed issues table and export it to Excel.
  3. Look for the same issue types being repeated on the same lines of code across Configuration Elements with different Sys Ids. These almost certainly identify blocks of code that have been copied and pasted across different Configuration Elements.

Well, there you have it. So now, do you know if your instance is DRY or WET?

Set up a demo of Quality Clouds for if you want to learn more.

See how three solutions work together to help your teams have the tools they need to deliver quality software quickly. Brought to you in partnership with CA Technologies

Topics:
agile ,wet principle ,dry principle ,code duplication ,pragmatic programming ,low code development

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}