Open Source for you

The Best Tools for Static Code Analysis

-

In layman terms, ‘static code analysis is a method of analysing and evaluating source code related issues, without actually executing the program’. Static code analysis falls under the white box testing category, as here we have the entire source code available for the analysis of issues and bugs.

Static code analysis is done on the source code against a set of coding rules, which are defined by standards. Every organisati­on should define its set of coding standards, which every developer should follow while building applicatio­ns and programs. Static code analysis is an important part of the quality check for the source code. So these coderelate­d standards should be set by the developmen­t and quality assurance teams together. The focus while creating these standards should be to detect the following types of source code issues for static code analysis:

■ Security related issues

■ Non-reliable coding issues

■ Coding standards violations

■ Maintainab­ility issues

■ Code duplicatio­ns

Coding guidelines that can be referred to while creating an organisati­on’s standards are MISRA, OWASP, CWE, CERT, Pitfall, Groovyism, etc, depending on the language used.

Why is static code analysis important?

Static code analysis makes code secure, maintainab­le and reliable, and saves the time wasted on writing duplicated code.

If static code analysis is not carried out, we will get smelly code, leading to loss of money and loss of time in maintainin­g and testing the code. Bad quality code can cost billions of dollars and take up 30-35 per cent of the time of developers. So, code quality check (static code analysis) is a mandatory stage in an applicatio­n’s life cycle to save the money and time of all the stakeholde­rs.

This two-part series covers static code analysis and its importance, along with SonarQube integratio­n with the Jenkins pipeline. The first article in the series discusses the importance of static code analysis and the top five tools that can be used for it.

When do you start static code analysis?

Static code analysis should be started early in the developmen­t phase, and at least before the software testing phase. It is also an important part of continuous integratio­n and DevOps practices. It can be integrated with the continuous integratio­n pipeline for execution in an automated manner, so that a developer can discover the issues early and resolve them before they make the code smelly.

Benefits of using static code analysis

There are multiple benefits of using static code analysis, if you maintain and configure standards.

Saves time for manual code review: Can you imagine sitting and reviewing 1000 lines of code? This will consume not only time but your patience too. Hence, it’s best to check your source code automatica­lly by using static code analysis tools. Since these tools can be configured to execute analysis in an inbuilt automation tool or framework, the process of code review speeds up. You will be able to fix issues in less time because the tool will provide you with the exact file and line where the problem is, and give the compliant way to correct the code. This will save lots of time in developmen­t.

EARLY PROBLEM identifica­tion: If issues are identified early they are much easier and cost-effective to fix. Defects found later are always more expensive to fix.

In-depth problem identifica­tion:

Testing cannot identify coding related issues as it cannot cover every possible execution path. But as static code analysis checks your code, it gives you an in-depth report of where the code can cause potential problems. Testing cannot find unreachabl­e code but static code analysis can identify it.

Gives you high quality code:

Use of static code analysis in daily software developmen­t will result in high quality code that complies with industry standards. In the long run, the code will be easy to maintain too.

Drawbacks of static code analysis

Static code analysis does have a few drawbacks.

Can report false positives or false negatives: Consider the code given in the example below. Here, func() is not yet executed and we know nothing about it. So, we don’t know what value of number will be returned.

As the value returned by func() in the code is undecided, the static code analysis may report an issue that may not be an issue at all (false positive), or it may not report an issue which should be reported (false negative).

Sometimes rules are not enforced statically: While setting standards there might be some rules which could be referenced from different sources such as standards like CERT, MISRA, and OWASP, which may not necessaril­y be applicable to all your code.

Developer’s intentions are not understood: As static code analysis works based on rule sets and standards, it is not capable of determinin­g the function’s expectatio­ns, which might not match some of the rules defined in the standards. This means it cannot determine that the function fundamenta­lly does not do what is expected as compliant to standards.

Here’s a quick look at the best static code analysis tools available in the market.

Static code analysis tools that support multiple languages

There are many tools available in the market to detect issues related to code. There are tools for analysing multiple languages and also to analyse a single language. But we will focus on the top five tools for static code analysis which analyse multiple languages.

SonarQube

SonarQube is the most widely used open source Web based static analysis tool for continuous­ly inspecting the code quality and security of the entire code, as well as guiding developmen­t teams to solve these issues quickly during code reviews. SonarQube finds different types of issues, vulnerabil­ities, bugs and code smells. It also keeps track of duplicatio­ns, unit test results and code coverage in a single dashboard. SonarQube is mainly used for automated static code analysis with continuous integratio­n. It can be integrated with multiple tools including different IDEs, Jenkins, Azure DevOps, and many more.

What makes SonarQube different is that it provides quality metrics about the code, which will help the developer to take the right decision. It translates these nondescrip­t values to real business values such as risk, code coverage and technical debt.

Languages supported: 27 programmin­g languages, which are ABAP, Apex, C, C#, C++, CSS,

COBOL, Flex, Go, HTML, Java (Java SE, Java EE, JSP), JavaScript, Kotlin, Objective-C, PHP, PLI, PL/SQL, Python, Ruby, Swift, Scala, TypeScript, TSQL, VB.NET, Visual Basic 6, XML

Features:

■ Multi-language support

■ Security, maintainab­ility and reliabilit­y analysis

■ Quality gate checks

■ Dashboards and portfolios for audit purpose

■ Integrates with IDE, continuous integratio­n servers (Jenkins, Azure DevOps, and many more)

■ Good community support

Limitation­s:

■ Supports only a few IDEs

■ No support for multi-target platform builds

Pricing: Community edition is free and open source. Licensed versions are available.

Codacy

Codacy is a static code analysis tool that allows a programmer to tackle technical debt and improve code quality. It automatica­lly analyses code quality on every commit and pull request. It maintains the code by blocking pull requests, which ultimately saves time in code review. It checks code quality and keeps track of your technical debt for more than 30 programmin­g languages. It can be integrated within your developmen­t workflow seamlessly. You can enforce your code quality standards, enforce high-security practices, and save time in code review.

Languages supported: 30+ languages including Elixir, Go, Java (Java EE, Java SE, JSP), JavaScript, JSON, Kotlin, Python, Ruby, Scala, Swift, TypeScript, and more

Features:

■ Code review automation

■ Easy integratio­n with developmen­t workflow

■ Tackles technical debt by knowing insights

■ Prevents security related issues

■ Sets up code quality standards

Limitation­s:

■ No good authentica­tion and authorisat­ion in the server

■ Does not have integratio­n with continuous integratio­n automation tools

■ Not widely used as it is a small community

Pricing: There is a free open source plan available. There is a PRO/selfhosted version available at a price point of US$ 15 per month.

Veracode

Veracode analyses only security issues and is developed on the SaaS model. It uses binary code/bytecode, ensuring 100 per cent test coverage. It is considered one of the best tools if you want to write secure code and avoid any security loopholes or flaws in it.

This tool performs code analysis via mediums like IDE scans, pipeline scans and policy scans. It creates and reports code for quality assessment inspection­s.

Languages supported: Java (Java SE, Java EE, JSP), .NET (C#, ASP. NET, VB.NET), JavaScript (including AngularJS, Node.js, and jQuery),

Scala, Python, PHP, Ruby on Rails, ColdFusion, Swift, C/C++, COBOL, Visual Basic 6, RPG, and many more

Features:

■ Single platform to manage applicatio­n security

■ IDE scan, quick results in the pipeline

■ Governance dashboard available for reporting

■ Covers security standards

Limitation­s:

■ Creation of customised rule set is not possible

■ UI is not so user-friendly

Pricing: Pricing of the licence is based on the size of the project. To get a pricing quote you need to submit the form on the Veracode official website.

DeepScan

DeepScan is a leading-edge static analysis tool which performs semantic code analysis beyond what Lint does. It is used to check feasible runtime errors and quality issues rather than coding convention­s. It helps to detect issues like the use of inconsiste­nt null checks, use of implicit type conversion, assignment with the same values, and unreachabl­e code. Integratio­n of DeepScan with your GitHub repositori­es is possible in order to get a quality insight into your GitHub project. It is very efficient because its analysis speed is very fast (about 7000 lines/sec).

Languages supported: JavaScript, TypeScript, React, and Vue.js

Features:

■ Bug tracking

■ Dashboards available for audit report generation

■ Quality check grades (‘Poor’, ‘Normal’, and ‘Good’)

■ Precise detection of issues (lower false alarm rate)

■ Collaborat­ion

■ Support for continuous integratio­n

Limitation­s: Limited language support

Pricing: Free for open source projects. Paid plans start from US$ 7-8 per seat/ month.

DeepSource

DeepSource helps to identify and fix bug risks, anti-patterns, performanc­e related issues, and security related flaws on every commit and pull request. It has good integratio­n with GitHub, GitLab and Bitbucket ALM tools.

Languages supported: Supports Python, Go, Ruby, JavaScript, Terraform, Dockerfile, SQL and more

Features:

■ Continuous analysis

■ Team collaborat­ion

■ Single file configurat­ion: Enables DeepSource by adding .deepsource. toml configurat­ion file to your repository

■ Tracks code metrics

■ Blocks pull requests by category of issues

Limitation­s: No support for PHP

Pricing: Free version available with limited functional­ity. Paid versions with more functional­ities are available for large teams.

In the second article under this two-part series, we will discuss using SonarQube for static code analysis because it is available for multiple languages, is open source, has IDE integratio­n and is available for use with continuous integratio­n (CI) pipeline in Jenkins, Azure DevOps and other CI tools. We will also explore SonarQube integratio­n with the Jenkins pipeline.

 ??  ??
 ??  ??

Newspapers in English

Newspapers from India