This section explains all the metrics used by FortranAnalyser to determine the quality of Fortran code. They are designed and implemented following well-established criteria on software development30–32.
List of metrics
1. the “IMPLICIT NONE” statement ( 30, pp. 57–66): Including ”IMPLICIT NONE” at the beginning of a file ensures that the compiler will generate the appropriate warnings when a variable lacks an explicit declaration. In Fortran, it is advisable to declare all the variables correctly to avoid unnecessary memory allocation (e.g. real variables need double memory than integer variables);
2. omments 30:
-
at the beginning of the document: it is verified that two out of the first three lines of each file are comments. These are supposed to contain a brief description of what does the code;
-
in the declaration of a variable, function and subroutines: each one of these must have associated a comment before or after. The comment must describe its type, use and what it does;
-
in control structures: each control structure must have a comment before or after. It must describe what condition is being checked to undertake a specific operation.
3. the “EXIT” statement ( 30, pp. 145–147): releases resources allocated, allowing their use in other task;
4. number of nested loops ( 30, pp. 148–150): nested loops increase the resources needed for a task. For this reason, it is established as a quality metric that there should be no more than three nested loops and no commentary at the opening of each loop;
5. ratio: refers to the ratio of the number of lines with comments to the number of computable elements. It checks whether all these computable elements (e.g. loops, "if" statements, and functions) include comments before or after them. It assumes that comments are in English, as it is the universal language of science33. However, it should be bear in mind that comments in other languages do not occupy the same number of characters. That said, FortranAnalyser can evaluate the ratio from comments in any language. However, the calculation is more accurate if they are in English;
6. the “CYCLE” statement ( 30, pp. 145–147): avoids unnecessary code execution, letting to iterate to the next element.
Justification of scores
In software programming, there are elements with a significant influence on the execution and structuring of the code, and others are not essential. For example, the "IMPLICIT NONE" statement is critical, as it helps save memory and code debugging and maintenance. Nested loops make it difficult to detect the exact point at which a run-time problem occur, especially when the code is very long. Less critical elements are the "EXIT" and "CYCLE" statements, as for the structuring of the code, they do not help avoid run-time errors. However, their use is highly advisable as, in some cases, they can affect performance.
Based on this, we define two types of metrics (see Table 2 ):
1.the use of the “IMPLICIT NONE”;
2.number of nested loops;
3. the use of comments;
4. ratio.
The sub-metrics on the use of comments sum 2.0 points, but this score is divided by the total number of items. For this reason, each of them is assigned a value of 0.4 points.
A Fortran project is composed of files, each with a given number of lines, the sum of which is the total number of lines in the software. Following the above mentioned metrics we rate each file or routine in a Fortran project. The total score of the full project will be the summation of the score for each file multiplied by the number of lines in the file and divided by the total number of lines of the project. Mathematically ("n": total number of files):
A total score is obtained by considering the weighted value of each file analysed in the project as a whole. The maximum score obtained by any project can be 10.
Quality report
From the information described in the preceding subsections, FortranAnalyser generates a quality report (see supplementary material for an example). Part of this information is used to compute the quality metrics32. Other is provided for illustrative purposes as it can help the developer to improve the code. The report includes information about:
1. number of lines in the file : it determines the size of the programme analysed. Obviously, there is no direct relationship between the number of lines and the complexity of the code. However, this variable is used in the calculation of other metrics, such as the number of lines with comments;
2. number of variables declared : it is important to economise on the variables declared, otherwise, they may waste memory;
3. number of calls to subroutines : every call increases the interdependence between modules and therefore, the coupling. An ideal and well-structured code is expected to show low coupling, which allows maintaining a high level of cohesion34;
4. number of subroutines declared : its use helps to design a project with a smaller number of lines, improving its mainte- nance and readability34;
5.number of functions declared : the use of functions avoids duplication when a task is performed repeatedly.
Once the analysis has been performed, the previous information is printed in the report. It contains a table explaining the ten metrics applied, together with the score obtained for each one of them, and last, the total score. For each file analysed, the score obtained is shown individually. An example is shown in Fig. 5.