Question

So I have a whole lot of variables I need to declare, and the original code looked like this:

    DIMENSION energy_t(20000),nrt(20000),npsh(1000),xx(1000),yy(1000),
    :step(1000),stepz(1000),r1(1000),rr(1000),ic(1000),diffrr(1000)

And I rewrote it as this:

    DIMENSION
    :energy_t(20000),
    :nrt(20000),
    :npsh(1000),
    :step(1000),
    :r1(1000),
    :rr(1000),
    :ic(1000),
    :diffrr(1000)

Is this considered good style, or are there better ways? Note that the second way allows for comments with each variable, and I don't have to use line continuations if I might add another variable.

P.S.: is there a consensus/style bible/widely regarded source on Fortran programming style & good practices?

Was it helpful?

Solution

Good style is not to use the dimension statement in the first place. Especially if you use implicit typing. Every variable should have a declared type and is better to put the array dimension there. Use attributes with the type declaration (Fortran 90+).

real :: energy_t(20000), nrt(20000)
real, dimension(1000) :: npsh, xx, yy, step, stepz, r1, rr, ic, diffrr

Keep lines not too long. Both ways of declaring size (shape) are possible.

If you need Fortran 77, you are more limited, but still

real energy_t(20000), nrt(20000)
real npsh(1000), xx(1000), yy(1000), step(1000), stepz(1000)
real r1(1000), rr(1000), ic(1000), diffrr(1000)

is probably better.

Try to group related variables on one line and the others on different lines.

I would also suggest to declare parameter constants for the sizes 1000 and 20000.

OTHER TIPS

Good style would be to parametrize the dimensions

integer, parameter:: NODES_MAX = 1000, TIMES_MAX = 2000, COORD_MAX = 1000
real energy_t(TIMES_MAX), ..
real npsh(NODES_MAX), xx(COORD_MAX) ...

so that the loops can be parameterized.

do ii = 1, COORD_MAX
    xx(ii) = ...
    yy(ii) = ..
end do

and error checks can be made

if (ii .gt. NODES_MAX) then
    print *, 'Please increase NODES_MAX oldvalue=', NODES_MAX, ' required=', ii
    pause
end if

This will also minimize the number of changes required when the dimensions are increased/decreased. This style could also have been applied 30+ years ago when F77 came out.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top