Skip to main content

Static Code Analysis

This document outlines the best practice for static code analysis and tools.

Description

Static code analysis is a method of analysing code without executing it. At CRUK we use a number of static code analysis tools to ensure that our code is of a high quality and to help us identify potential issues before they are introduced into the codebase.

These checks should be performed as part of the PR checks and can also be run locally before pushing code to the remote repository. To see PR checks using GitHub actions see the Deployment Strategy best practice.

This list is not exhaustive and is intended to be a starting point for team discussion. Each team is responsible for the quality of their software and If you have a tool that you think should be included please raise a PR.

Checks Summary

CheckDescriptionExample ToolRequirement
LintingAnalysing code for potential errors.ESLintYes
FormattingEnsuring that code is formatted in a consistent way.PrettierYes
Unit TestsTests covering low level logic.JestYes
CoverageEnsuring coverage remains above 80%JestRecommended
UI TestsTest UI components holistically.Playwright & Applitools EyesYes for UIs
AccessibilityTest accessibility in UI applications.AccessibilityRecommended for UIs
Spell CheckEnsuring words are spelt correctly.cspellNo
Commit Message CheckEnsuring the commit message is formatted correctly.opinionated-commit-messageNo
PR Message CheckEnsuring the PR description is formatted correctly.pr-compliance-actionNo
Broken Link CheckEnsuring all links are still valid.broken-link-checkerNo

Rationale

The checks listed above are intended to help us ensure that our code is of a high quality and to help us identify potential issues before they are introduced into the codebase.

At a minimum all repos should have linting and formatting. These checks are quick to add and provide significant benefits at catching issues early. Formatting is particularly important as it helps to ensure that code is consistent across the codebase and makes it easier to read.

Unit tests are also a minimum requirement. These tests should cover the low level logic of the application and should be run as part of the PR checks.

UI tests are also a minimum requirement for UI applications. These tests should cover the UI components and should be run as part of the PR checks.

Other checks such as accessibility, spell check, commit message check, PR message check and broken link check are not required but could be useful depending on the application. This repo benefits greatly from the spell check due to the large amount of documentation.

Ways of Working

When doing static code analysis, it is highly recommended to pair often with the developer who wrote the code to understand what the code is doing, why decisions have been made in the logic thinking/code creation process and where the code change can potentially impact the user journey.

The advantages of pairing:

  • Helps QAs have a better understanding of what the code is doing and therefore can help catch issues early through asking questions in the development process before testing has commenced.
  • Highlight any testing scope changes since refinement.
  • Improve working relationships with the development team.
  • Build on overall product knowledge.

Examples

References & Further Reading