logo

NJP

The Art and Importance of Code Review: A Developer's Guide

New article articles in ServiceNow Community · Oct 14, 2024 · article

In the fast-paced world of development, code reviews are the unsung heroes that elevate our work from good to great. They foster collaboration, enhance code quality, and create a learning environment that benefits everyone involved.

As a developer, the significance of code reviews didn’t hit me immediately. Through projects, some wins, and a fair share of oops moments, I’ve come to realize how essential this process is for creating quality, maintainable code.

Let me take you through my learning and experiences of the code review process. I will also share the practices I currently use to keep my code clean and efficient.

Why Code Review?

Code review is more than just bug-hunting. Here’s why I believe code reviews are essential:

  1. Improved Code Quality: Having a peer review your code means a fresh set of eyes that can spot potential issues or improvements.
  2. Knowledge Sharing: Code reviews encourage a learning culture. Junior developers can learn from seniors, and even seniors can pick up new tricks.
  3. Better Collaboration: Code reviews align the team on coding standards, architectural decisions, and best practices.
  4. Preventing Future Issues: Catching small problems now prevents technical debt from piling up, saving you from future headaches.

Who should do Code Reviews?

Different team members contribute to code reviews at various stages:

  • Developers: Review your own code before submitting it, ideally with a checklist in hand.
  • Peers: A peer review ensures that the code adheres to the team’s standards before merging.
  • Architects or Senior Developers: Senior members focus on the architectural aspects to ensure the code fits the overall project goals and is scalable.

A good code review is a team effort, with everyone following a consistent set of guidelines.

How to Conduct Effective Code Reviews?

Below is a general Code Review Checklist, focused more on the process rather than technical errors or specific ServiceNow JavaScript details. This checklist helps ensure that your code is well-structured, maintainable, and aligns with best practices.

1. Naming Conventions

  • Clear naming: Use short, clear names that indicate the variable’s purpose and type.
    • Good: `var requestDateTo`.
    • Bad: `var rdt`.
  • Sentence case: Sentence case for variable names.
    • Stick to `var requestDateTo` instead of `RequestDateto`.
  • Reverse notation: It helps in organizing and searching variables more easily.
    • Example: Group related variables like `employeeName`, `employeeID`, `employeeDepartment`.

2. Proper Spacing and Formatting

  • Maintain readability: Readable code is maintainable code. Use proper spacing to make it clear.

if (condition) {

doSomething ();

}

Instead of:

if(condition){doSomething ();}

  • Indentation Consistency: Stick to team standards for indentation and line breaks. Consistency is key!

3. Descriptions and Comments

  • Code comments: Explain the why, not the what. Comments should clarify complex logic or decisions, not state the obvious.

// Adjust date format to match external system’s requirements

formattedDate = adjustDate(inputDate);

  • Function Documentation: Provide a clear explanation of any function or class above it.

/*Description of the function

**Param 1 : Datatype - Required/Optional – Description of the parameter

**Param 2 : Datatype - Required/Optional – Description of the parameter

**Returns : Datatype – Description*/

4. Review Code Implementation

  • Method reuse: Instead of duplicating code, create reusable methods where appropriate.
    • Example: If you find yourself copying and pasting the same logic, it’s time for a method.
  • Clean up after yourself: Remove any unnecessary code. No one likes debugging through dead code.
  • Input Validation: Ensure user inputs are validated.
  • Efficiency of Algorithms: Evaluate the algorithms and data structures for optimal performance.
    • Example: Consider whether a more efficient sorting algorithm can replace a standard one.
  • Database Queries: Look for opportunities to optimize database queries.
  • Security Considerations: Authentication and Authorization.
    • Confirm security measures are in place to restrict access to sensitive functions or data.
  • Robust Error Handling: Ensure errors are handled gracefully with informative messages.
    • Example: Use try-catch blocks effectively and log meaningful error messages for easier debugging.

5. General Checks

  • Architectural Fit: Is the code aligned with the project architecture?
    • Ensure your solution fits within the system's overall design. If unsure, check with your architect.
    • Example: Before writing a complex query, verify it fits with existing patterns used across the app.
  • Happy path testing: Test under ideal conditions to ensure the code does what it should.
  • Negative path testing: Don’t forget to test for edge cases and unexpected inputs - this is crucial.
    • Example: What happens if a user enters incorrect data or null values?
  • Team reviews: Review together with the team.
    • Multiple eyes on the code catch more bugs, and it builds a shared understanding of best practices.

What Resources/Methods to use for Code Review Practices?

1. Version Control System: ServiceNow's built-in version control system enhances code reviews.

  • Allows developers to easily compare different versions of code.
  • Facilitates the identification of changes and potential issues.
  • Enables to roll back to previous versions to reduce the risk of introducing bugs during the review process.

2. Integrated Development Environments (IDEs): Using the built-in ServiceNow Studio, developers can review code changes directly within the environment.

  • It provides a clear view of code and the ability to track revisions
  • It provides a bird’s eye view of all the configuration present in an application, allowing to check for consistency through naming conventions.
  • It provides easier navigation to switch through various configurations.

3. ServiceNow Script Debugger: This can be incredibly useful for reviewing and testing code in a live environment. For debugging specific scripts, the ServiceNow Script Debugger allows developers to

  • Step through their code.
  • Set breakpoints.
  • Inspect variable values.

4. Peer review: Peer review involves developers evaluating each other's code to ensure quality and adherence to standards, enhancing code quality through diverse perspectives. This collaborative process fosters knowledge sharing and accountability, ultimately leading to more robust and maintainable code.

5. Code Review Checklists: Create and utilize checklists during the review process to ensure all key points are covered. This can include naming conventions, coding standards, and performance considerations specific to your project.

How do you do your code reviews? let me know in the comments :slightly_smiling_face:

View original source

https://www.servicenow.com/community/developer-blog/the-art-and-importance-of-code-review-a-developer-s-guide/ba-p/3073033