Best Practices & Style

39.1 Modern Fortran guidelines

Always implicit none; prefer implicit none (type, external).

Use free-form source, lowercase keywords, consistent 2–4 space indentation.

Put every procedure in a module (explicit interfaces); private by default.

Declare intent for every dummy argument.

Use kind parameters from iso_fortran_env; suffix literals (1.0_dp).

Prefer allocatable over pointer; let automatic deallocation manage memory.

Use assumed-shape arrays (a(:)) for dummy arguments.

Mark procedures pure/elemental when possible.

Avoid global mutable state; pass derived types instead.

Use newunit=, iostat=, iomsg=, stat=, errmsg= for robust I/O and allocation.

Name constructs and use exit/cycle instead of goto.

Avoid declaration-time initialization of locals (implicit save).

Write tests, use version control, CI, and FORD docs.

Compile with warnings and runtime checks in development; optimize for release.

Follow the fortran-lang style guide and use fprettify/fortitude.

39.2 Naming conventions

snake_case, often _mod/_m suffix snake_case or _t suffix verbs, snake_case snake_case or UPPER pi, MAX_ITER dp, sp, i8 match module name

39.3 Common pitfalls

Missing implicit none add it everywhere real(dp):: x = 0.1 (single-precision literal) Integer division 1/2 = 0 1.0/2, real(i)/n Row-major loop order innermost loop over the first index

Non-short-circuit .and. with array bounds Initialized local = implicit save assign in executable code

Mismatched argument types to external procedures module procedures / interfaces Array out of bounds -finit-real=snan, -Wuninitialized Forgetting to compile modules first build tools (fpm/CMake) Memory leaks with pointers allocatables; deallocate + nullify Assuming real = 4 bytes / real*8 Hidden temporary array copies -Warray-temporaries, contiguous data Race conditions in OpenMP default(none), private, reduction