open-catalog icon indicating copy to clipboard operation
open-catalog copied to clipboard

[Fortran] NAG compiler requires to define variable before its usage

Open foxtran opened this issue 8 months ago • 1 comments

NAG compiler requires to define variable before its usage in type declaration section. For example, the following code is not correct:

subroutine test(a, n)
  implicit none
  real(8) :: a(n)
  integer :: n
end subroutine test

since usage of n happens before actual type definition.

The proper code is:

subroutine test(a, n)
  implicit none
  integer :: n
  real(8) :: a(n)
end subroutine test

foxtran avatar Jul 01 '25 12:07 foxtran

Hi @foxtran,

Thanks for bringing this up!

Additionally, even if more permissive compilers accept the original code, the second form is significantly better in terms of readability. I think it's a good candidate for a new recommendation.

I'll leave this issue open so we don't lose sight of it. Naturally, feel free to submit a PR if you'd like to make a proposal! Below you will find a few tips. Otherwise, we'll note this idea and let you know of any relevant updates here.

Steps for adding a new recommendation (click me)
  1. Each recommendation resides in its own directory under Checks/. Create the directory and update the list of checks in the README.md at the root of the repository.

  2. Add a README.md file that includes the following:

    • Title: ID (e.g., the next available PWR number) + Name of the recommendation.
    • Issue: A brief description (1-2 sentences) of how the discouraged practice impacts the code.
    • Actions: A brief explanation (1-2 sentences) of how to refactor the code from the discouraged to the encouraged practice.
    • Relevance: A more detailed explanation of why adopting the recommended practice is beneficial, expanding on the previous points.
    • Code examples: Include a before-and-after code example to illustrate the improvement.
    • References: Add links or references for further reading, if available.
  3. Create standalone example and solution source files containing the code examples.

  4. Optionally, include a benchmark demonstrating the before-and-after performance impact of the change. In cases like this, the expectation is that the performance of the code should not degrade.

For reference, feel free to check out previous PRs to see how these steps are implemented in practice. For example, this is the PR that added PWR075.

Naturally, don't hesitate to ask at any moment if you have any issues. We'll be glad to help!

alvrogd avatar Jul 02 '25 07:07 alvrogd