Static Code Analysis in CI/CD for Structured Text
To raise the quality and maintainability of our B&R projects, we integrated Lizard, a static code analysis tool, into our CI/CD pipeline.
We found it really useful for keeping our code maintainable, and thought it might be interesting for other developers here, who struggle with an ever growing code base.
Even though we are not affiliated with Lizard, we made some contributions for supporting Structured Text. This allowed us to apply it on our whole code base, which is on large parts written in ST.
Why Static Analysis?
Unlike testing, static analysis inspects code without executing it. This means potential issues are caught early and automatically:
-
Functions with high cyclomatic complexity (CCN) or high nesting depth (ND) are flagged before they become unmaintainable.
-
Duplicate code blocks are detected, encouraging refactoring and reuse.
-
Quality metrics are tracked consistently over time.
Example Test Report
================================================
NLOC CCN ND token PARAM length location
----------------------------------------------------
35 12 6 120 3 50 src/MotionControl.st:func_MoveAxis()
20 4 2 80 2 25 src/Sensor.st:func_CheckLimits()
================================================
-
CCN=12β function has a high branching complexity, should be refactored. -
ND=6β deep nesting, code may be hard to follow and maintain. -
CCN=4 / ND=2β acceptable, no action needed.
Integration in CI/CD
We already implemented a CI/CD pipeline, which basically executes the following steps if a developer commits some new code:
-
Build & test: Execute all Unit Tests and report failure.
-
Code Linter: Checks Spelling, formatting, etc.
-
Static analysis with Lizard: Report complexity and code duplicates.
This ensures immediate feedback for the developer and enforces at least a minimum of code quality.
Looking for Insights
We would be interested to hear from your experiences with similar tools:
-
Do you already use static code analysis in your PLC projects?
-
Have you tried other tools that work well with Structured Text?
-
Any lessons learned or best practices to share?