Search code examples
variablescoding-stylefortranderived-types

Good practice in naming derived types in fortran


I'd like to optimize the readability of my codes in Fortran by using OOP. I thus use derived types. what is the best practice to name the types and derived types?

For example, is it better to:

type a
    real :: var
end type
type(a) :: mya

or always begin type names by type_ like in type_a? I like this one but maybe better ideas can be foud.

Also, is it better (then why) to use short names that are less readable or longer names that end up quite difficult to read if the type has too many "levels". For example, in a%b%c%d%e, if a, b, c, d and e are 8 or more letters long as in country%hospital%service%patient%name, then once again readability seems to be a concern.

Advices from experts are really welcome.


Solution

  • This not anything special to Fortran. You can use coding recommendation for other languages. Usually, type names are not designated by any prefix or suffix. In many languages class names start with a capital letter. You can use this in Fortran also, even if it is not case sensitive. Just be sure not to reuse the name with a small letter as a variable name.

    One example of a good coding guideline is this and you can adapt it for Fortran very easily. Also, have a look on some Fortran examples in books like MRC or RXX. OS can be also useful.

    I would recommend not to use too short component names, if the letter is not the same as used in the written equation. In that case it can be used.

    Use the associate construct or pointers to make aliases to nested names like country%hospital%service%patient%name.