In software development, especially in languages like Fortran, code modernization goes beyond keeping up with the times; it’s about enhancing quality, maintainability, compatibility and performance. That’s why Codee places a strong focus on modernizing Fortran, while also detecting critical optimization issues along the way. We have developed a range of Fortran autofixes to help developers automatically correct specific issues in the source code, always under the control of the programmer and preserving 100% code correctness.
In this post, we will explore the current autofixes for Fortran modernization:
Disable implicit declaration of variables
Implicit data typing in Fortran is enabled by default, which can lead to errors and decrease code legibility. To address this, Codee automatically adds the implicit none statement after the program or module declaration, and missing variable declarations to ensure that all are explicit. This practice helps prevent unintended behavior due to misinterpretation of variable types and enhances the clarity and maintainability of the code.
Read more and see an example: PWR007: Disable implicit declaration of variables
Declare the intent for each procedure parameter
Declaring the intent for each procedure parameter is essential to facilitate understanding the flow of data in and out of functions. Codee explicitly stating the intent and the source code becomes more legible and the function’s interactions with external data are clearly outlined. This practice allows for easy determination of how a procedure operates by inspecting its call sites, improving reasoning about the code. Compilers and code analysis tools can use this information to enforce correctness, performance, and best programming practices. Implicit intent can lead to unexpected runtime errors, such as incorrect variable assignment and mapping, which might go undetected by the compiler. Explicit intent declarations help identify and prevent these issues, ensuring robust and reliable code.
Read more and see an example: PWR008: Declare the intent for each procedure parameter
Use the keyword only to explicitly state what to import from a module
Importing modules without specifying elements using the only keyword can obscure their origin, reduce code readability, and lead to name conflicts. To enhance code clarity, Codee automatically adds the only keyword in use statements to explicitly specify which parts of a module are needed. By default, the use statement makes all public parts of a module accessible, complicating the tracing of each element’s origin and potentially causing name conflicts, especially in large codebases. These issues negatively impact the readability and maintainability of the code. In contrast, the only keyword restricts visibility to explicitly mentioned elements, effectively preventing these problems and improving overall code quality.
Read more and see an example: PWR069: Use the keyword only to explicitly state what to import from a module
Split the variable initialization from the declaration to prevent the implicit save behavior
In Fortran, initializing a variable in its declaration implicitly assigns it the save attribute, causing it to retain its value across function calls. To avoid unexpected behavior, it is recommended to separate the declaration and initialization of the variable. If the variable is intended to retain its value between invocations, explicitly declaring the save attribute is preferred. This approach improves readability and prevents bugs by making variable behavior explicit and avoiding unintended persistence.
Read more and see an example: PWR072: Split the variable initialization from the declaration to prevent the implicit ‘save’ behavior
Conclusion
At Codee, we are committed to the continuous improvement of our tools to facilitate developers’ work. The current Fortran autofixes and upcoming releases are another step towards automating modernizing and optimizing Fortran code.
Take a look at the Open Catalog of Code Guidelines for Correctness, Modernization, and Optimization
to explore the checks that Codee currently offers. What additional autofixes for modernizing legacy Fortran would you suggest? Let us know your thoughts in the comments.
You can watch the Codee in action in this video.
Updated: November, 2024
Build correct, secure, modern and fast Fortran, C and C++ scientific software
This company is capitalized by INNVIERTE, AN INVESTMENT PROGRAM OF CDTI, E.P.E
Leave a Reply