- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 11-09-2024 01:54 PM
Use Case:
Numerous developers possess their own unique coding styles to fulfill requirements. How can one ascertain that they are adhering to good coding standards and that their practices do not adversely affect the platform's score and overall health?
Description:
When developers fail to adhere to the code standards and best practices established by ServiceNow, it can lead to numerous adverse effects on the platform and user experiences. This undermines customer confidence in the ServiceNow platform and its functionalities. It is essential for developers to be aware of the appropriate actions to take and those to avoid, as highlighted in various articles.
Solution:
The following recurring mistakes are prevalent among developers. By eliminating these from our coding standards, we can better promote adherence to best practices.I have frequently observed the following 5 mistakes, which I have personally made and have also noticed among other developers.
1. Use gs.log(), gs.info() functions:
It is common for developers to utilize the gs.log() and gs.info() functions during the development phase to verify the functionality of their code on the front end. However, after completing the tests and obtaining successful results, developers often neglect to disable these functions. If left enabled, these functions will generate entries in the log file each time the code is executed, thereby increasing the load on the log file.
2. Use current.update() function:
When calling the update() function, any associated Business Rules to the object of the update() function are also called which can end up causing a string of recursive calls to the same Business Rule. While the system has internal detectors for this and will prevent an endless series of such Business Rule executions, the detection of this has been known to cause performance issues in that instance.
https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0715782
3. Hardcode Values in Scripts:
The use of hardcoded values can compromise the quality of your code. In ServiceNow, there is a guideline that stipulates that any hardcoded value should be defined in sys_properties and accessed through the gs.getProperty() function. This approach ensures that the same value can be easily retrieved and utilized in a recursive manner. If you anticipate that a particular operation in your code will be invoked from multiple locations, it is advisable to encapsulate that logic within a script include or function.
4. Variable definition:
The definition of variables is crucial in your code, as it clearly communicates the intended operations and the manner in which they will be invoked, along with the types of functions involved. It is essential to adhere to appropriate naming conventions for both global and local variables. A key recommendation is to avoid using the generic name 'gr' for a variable. The rationale for this advice will be elaborated upon in the link provided below.
5. Server Scripts usage in Client Scripts:
It is acknowledged that the code for ServiceNow Server scripts can be utilized within client scripts. However, incorporating that code directly into client scripts is not considered a best practice. ServiceNow offers functions known as GlideAjax(), which should be employed in client scripts to invoke server-side scripts.
Does ServiceNow provide a detailed document outlining the guidelines that developers should adhere to and those they should avoid? Additionally, what is the process for obtaining this document, along with tips and best practices? The following link offers comprehensive information. It is essential for anyone transitioning into a developer role or currently working as a developer to read this document.
#codeStandard #codebestpractises #developersmistakes #platformImprovment
Hope you like it. If any questions or remarks, let me know!
If this helped you in any way, I would appreciate it if you hit bookmark or mark it as helpful.
Regards,
Suresh.
- 693 Views