The SonarQube COBOL Plugin Tracks Sneaky Bugs in Conditions
Not long ago, I wrote that COBOL is not a dead language and there are still billions lines of COBOL code in production today. At COBOL’s inception back in 1959, the goal was to provide something close to natural language so that even business analysts could read the code. As a side effect, the language is really, really verbose. Each time a ruby, python or scala developer complains about the verbosity of Java, C# or C++, he should have a look at a COBOL program to see how much worse it could be :). Moreover, since there is no concept of a local variable in COBOL, the ability to factorize common pieces of code in PARAGRAPHS or SECTIONS is limited. In the end, the temptation to duplicate logic is strong. When you combine those two flaws: verbosity and duplicated logic, guess what the consequence is: it’s pretty easy in COBOL to inject bugs in conditions.
Let’s take some examples we’ve found in production code:
Inconsistencies between nesting and nested IF statements
In the following piece of code the condition of the second nested IF statement is always TRUE. Indeed, when starting evaluation of the nested condition, we already know that the value of
ZS-LPWN-IDC-WTR-EMP is ‘X’ so by definition this is TRUE:
ZS-LPWN-IDC-WTR-EMP NOT EQUAL 'N' AND 'O'. What was the intent of the developer here? Who knows?
And what about the next one? The second condition in this example is by definition always TRUE since the nesting
ELSE block is executed if and only if
KST-RETCODE is not equal to ’02′ and ’13′:
Inconsistencies in the same condition
In the following piece of code and in the same condition we’re asking
ZS-RPACCNT-NB-TRX to be both equal to 1 and 0. Obviously Quantum Theory is not relevant in COBOL, and a data item can’t have two values at the same time.
The next example is pretty similar, except that here it is “just” a sub part of the condition which is always TRUE: (
ZS-BLTRS-KY-GRP NOT = 'IH' OR ZS-BLTRS-KY-GRP NOT = 'IN'). We can probably assume that this was not what the developer wanted to code.
Inconsistencies due to the format of data items
ZS-RB-TCM-WD-PDN cannot be greater than 9 since it’s declared as a single-digit number:
With version 2.6 of the COBOL plugin, you can track all these kinds of bugs in your COBOL source code. So let’s start hunting them to make your COBOL application even more reliable!