fortran
via PatrickJS/awesome-cursorrules
Modern Fortran best practices for scientific computing with modules, explicit interfaces, and memory safety.
What is fortran?
Enforces modern Fortran standards (2003+), module-based organization with explicit interfaces, kind parameters for numeric types, and memory-safe patterns. Use this rule when writing scientific computing code, numerical libraries, or any Fortran project that needs maintainability, safety, and portability across compilers.
- Require implicit none, modules with explicit interfaces, and modern Fortran standards (2003+)
- Enforce kind parameter definitions for numeric types and clear naming conventions
- Mandate intent declarations, early validation, and single-purpose procedures
- Promote allocatable arrays over pointers and safe memory management patterns
- Establish testing, build system consistency, and compiler warning practices
- Flag obsolete features (COMMON blocks, GOTO, numeric labels) and common mistakes
Applies to
File patterns this rule matches.
Rule definition (reference)
Source of truth, from the repository.
Fortran Programming Guidelines
Basic Principles
- Use modern Fortran standards such as Fortran 2003, 2008, or newer.
- Use
implicit nonein every program unit. - Put procedures in modules to provide explicit interfaces.
- Keep modules focused and place each major module in its own file.
- Prefer clear, structured code over clever language tricks.
- Avoid obsolete features such as COMMON blocks, GOTO-heavy control flow, and numeric labels.
Kinds and Types
- Define numeric kind parameters in one shared module, such as
kind_mod. - Use
real(kind=dp)or the project-approved real kind for floating point values. - Use
integer(kind=i4)or the project-approved integer kind for integer values. - Define constants such as pi explicitly.
- Include units in comments for physical quantities.
- Use derived types to group related data instead of passing many primitive arguments.
Naming and Style
- Use lowercase for language keywords and most identifiers.
- Use underscores for multi-word names.
- Avoid names that differ only by case.
- Use descriptive names for procedures and state.
- Repeat the procedure or module name after
endstatements. - Keep indentation consistent in
do,if,select case, and module blocks.
Procedures
- Keep subroutines and functions short and single-purpose.
- Use
intent(in),intent(out), orintent(inout)for every dummy argument. - Keep functions free of side effects whenever possible.
- Prefer early validation and clear returns over deep nesting.
- Use
use, only:when importing from modules.
Memory and Arrays
- Prefer allocatable arrays over pointers unless pointer semantics are required.
- Check allocation state and array sizes before use.
- Deallocate allocatable arrays when their lifetime is not naturally scoped.
- Specify array bounds clearly when they matter.
- Avoid unnecessary dynamic allocation in hot loops.
Testing and Build
- Use CMake, fpm, Make, or the project-standard build system consistently.
- Compile with warnings enabled and treat important warnings as failures in CI.
- Add unit tests for public procedures and integration tests for numerical workflows.
- Test boundary conditions, invalid inputs, and representative scientific cases.
- Verify numerical tolerances explicitly rather than relying on exact floating point equality.
Common Mistakes
- Do not declare variables after executable code unless using a block construct.
- Do not assume
random_numberis a function; it is a subroutine. - Do not write to stdout from pure procedures.
- Do not declare the same variable twice in the same scope.
- Do not assume pi, dp, or project kinds already exist without importing or defining them.
Related rules
Senior full-stack TypeScript, React, Node.js guidance with clean architecture, testing, and WHY-oriented reasoning.
Quantitative factor research skills for designing, evaluating, and mining alpha factors in equities markets.
Android development with Jetpack Compose, clean architecture, and Material Design 3.
Angular development with Novo Elements UI library using standalone components.
Expert Angular 18 + TypeScript development with Jest, emphasizing clean code and performance.
Manage Kubernetes clusters, add-ons, stacks, and credentials via the Ankra CLI platform.