Fortran Coding Standards and Style

Similar documents
ISO/IEC : TECHNICAL CORRIGENDUM 2

International Standards Organisation. Parameterized Derived Types. Fortran

Chapter 4. Fortran Arrays

Information technology Programming languages Fortran Part 1: Base language

Walt Brainerd s Fortran 90 programming tips

ISO/IEC JTC 1/SC 22/OWGV N 0220

The new features of Fortran 2003

Subroutines and Functions

Review More Arrays Modules Final Review

Parallel Programming in Fortran with Coarrays

Co-arrays to be included in the Fortran 2008 Standard

Evolution of Fortran. Presented by: Tauqeer Ahmad. Seminar on Languages for Scientific Computing

Chapter 3. Fortran Statements

Introduction to Modern Fortran

Tokens, Expressions and Control Structures

Our Strategy for Learning Fortran 90

Modern Fortran OO Features

Examining the Code. [Reading assignment: Chapter 6, pp ]

Annotation Annotation or block comments Provide high-level description and documentation of section of code More detail than simple comments

FORTRAN 90: Functions, Modules, and Subroutines. Meteorology 227 Fall 2017

2 3. Syllabus Time Event 9:00{10:00 morning lecture 10:00{10:30 morning break 10:30{12:30 morning practical session 12:30{1:30 lunch break 1:30{2:00 a

Fortran 90 - A thumbnail sketch

Computational Astrophysics AS 3013

Fortran classes and data visibility

Padasalai.Net s Model Question Paper

UNIT- 3 Introduction to C++

Microsoft Visual Basic 2015: Reloaded

SPECIFICATION 2 STATEMENTS

Scientific Programming in C VI. Common errors

Reusing this material

Fortran. (FORmula TRANslator) History

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

G Programming Languages - Fall 2012

Lecture V: Introduction to parallel programming with Fortran coarrays

CS 211 Programming Practicum Fall 2018

PACKAGE SPECIFICATION HSL 2013

Introduction to C++ with content from

Programming in C++ 4. The lexical basis of C++

Overview of the Ruby Language. By Ron Haley

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

5. Control Statements

Microsoft Visual Basic 2005: Reloaded

Fortran Coarrays John Reid, ISO Fortran Convener, JKR Associates and Rutherford Appleton Laboratory

Subroutines, Functions and Modules

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Introduction to Programming Using Java (98-388)

Course Coding Standards

ME 461 C review Session Fall 2009 S. Keres

INTRODUCTION 1 AND REVIEW

Short Notes of CS201

Welcome. Modern Fortran (F77 to F90 and beyond) Virtual tutorial starts at BST

CS201 - Introduction to Programming Glossary By

CSE 230 Intermediate Programming in C and C++ Functions

Pace University. Fundamental Concepts of CS121 1

PART A : MULTIPLE CHOICE Circle the letter of the best answer (1 mark each)

Fortran 2008: what s in it for high-performance computing

0. Overview of this standard Design entities and configurations... 5

CS 142 Style Guide Grading and Details

Coding Standard for ECE3090 August 24, 2005

Languages and Compilers

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

9/7/17. Outline. Name, Scope and Binding. Names. Introduction. Names (continued) Names (continued) In Text: Chapter 5

Chapter 2. Designing a Program. Input, Processing, and Output Fall 2016, CSUS. Chapter 2.1

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object

Introduction to Fortran Programming. -Internal subprograms (1)-

Chapter 2.5 Writing maintainable programs

REGCM 4.3 Developer Guide

C interfaces to HSL routines. J. D. Hogg. Version 1.0 5th December Numerical Analysis Group Internal Report

SKILL AREA 306: DEVELOP AND IMPLEMENT COMPUTER PROGRAMS

COMP 202 Java in one week

The New Features of Fortran 2003

9/5/17. The Design and Implementation of Programming Languages. Compilation. Interpretation. Compilation vs. Interpretation. Hybrid Implementation

Lecture 2 Tao Wang 1

NAGWare f95 Recent and Future Developments

Chapter 5 Names, Bindings, Type Checking, and Scopes

VARIABLES AND TYPES CITS1001

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

Absolute C++ Walter Savitch

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

C-LANGUAGE CURRICULAM

Flow Control: Branches and loops

Fundamental Concepts and Definitions

The Warhol Language Reference Manual

Programming Languages

The Design Process. General Development Issues. C/C++ and OO Rules of Thumb. Home

The PCAT Programming Language Reference Manual

Introduction to OpenMP. Tasks. N.M. Maclaren September 2017

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Guidelines for Writing C Code

(Refer Slide Time 01:41 min)

Do these criteria apply to work in this course?

Introduction to: Computers & Programming Defining Identifiers: Objects with Names

Fortran Bill Long, Cray Inc. 21-May Cray Proprietary

7. Procedures and Structured Programming

COP Programming Assignment #7

CSI33 Data Structures

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Transcription:

Fortran Coding Standards and Style The Fortran Company Version 20160112 Copyright 2015-2016, The Fortran Company All rights reserved. Redistribution, with or without modification, is permitted provided that the following conditions are met: 1. Redistributions must retain the above copyright notice, this list of conditions, and the following disclaimer. 2. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. The source file for this document in Open Office format may be downloaded from http://www.fortran.com/fortran_style.odt This is intended to be an evolving document. Please send comments and suggestions to info@fortran.com.

1 Introduction The following coding standards and style suggestions have been put together with the goal of making it easier to debug and maintain Fortran code. Some the recommendations have arbitrary components, such as the number of spaces to indent in a block. The main thing is to pick a consistent style for a program, a project, or an organization. Others are recommendations that should definitely be followed whenever creating new code (e. g., put procedures in a module). Nobody is going to agree to all of these suggestions. A good use of this document might be to serve as a basis for a customized style, obtained by doing some deleting, changing, and adding. 1.1 Program Structure 1. Programs should consist of a main program, modules, and submodules. Module procedures may be contained in libraries. All procedures should be internal or in a module or submodule. 2. Procedures should be small, typically 50 lines or less. 3. Derived-type definitions should be in a module. 4. Use a function if the procedure returns a single value (including possibly an array or structure) and the procedure has no side effects; otherwise, use a subroutine. 1.1.1 Main Program 1. The main program should be used only to call one or more high-level procedures. 1.1.2 Modules 1. Each module should be contained in its own source file, except that closely related modules might be put in the same file. 2. With the following two exceptions, the default accessibility of each module should be private by including the private statement: A module containing only parameters A collector module containing only use statements that access other modules 2

2 Declarations 2.1 Implicit None All program units should contain implicit none so that all variables, parameters, and functions must be declared. 2.2 Form of Declarations 1. Pick a consistent style from among the many equivalent declaration statements. The simplest rule is to put all characteristics of an entity to the left of a double colon (::) and a list of names (possibly with initialization) to the right. 2. Use optional keywords to provide readability, for example real(kind=dp), dimension(:, :) :: x_array, y_array 3. Put the declaration of each entity on a separate line, unless they are closely related, so that changing the declaration for one should also change the others. 2.3 Names character(len=max_char_len), dimension(n, N) :: char_a, char_b 1. Use short names for entities whose use is clear. For example, i and j are acceptable for array subscripts and x and y are fine as function dummy arguments. Use longer names for entities whose purpose is not obvious: distance_to_the_moon. Use different and consistent schemes for keywords, variables, parameters, procedures, modules, and types. The following is just one possible scheme. Some prefer to use lower case for everything. Keywords Variables Parameters Procedures Modules Type print, select case time, distance_to_the_moon PI, GOLDEN_RATIO Distance, Number_of_Points Point_Mod, Reactor_Simulation_Mod Point_Type, Reactor_Type 3

2. Do not use the same name for two different things in the same scope, even when allowed. For example, do not use print as a variable name. 3. Do not use the same name with different uppercase/lowercase spelling, e. g., N and n. 4. Use the letters O and l and the digits 0 and 1 in names only when it will be perfectly clear which character is meant. 5. Each file name should be the name of the containing entity with the.f90 extension. For example, the module Point_Mod should be in the file Point_Mod.f90. 2.4 Arrays 1. Index arrays with meaningful bounds. For example, an array containing the population of a city for the years 1900 to 2015 can be declared as integer, dimension(1900:2015) :: population_of_tucson 4

3 Program Form 1. Use free-form source format. 2. Limit the line length to 80 characters. 3. Break lines in logical places. Indent continued lines double the number of spaces that each block is indented (see #6 below). 4. Insert meaningful comments frequently. 5. Use blank lines to separate related parts of a program. 6. Indent blocks a consistent number of spaces (such as 2, 3, or 4). 7. Use white space freely in statements (e. g., around delimiters). 5

4 Statements 4.1 Control of Flow 1. Use only the block, if, do, and select case constructs to control flow. 2. Labels should not be used except for a very rare go to / continue pair (see #3 and 4.2 below). 3. No form of go to (computed, assigned, etc.) should be used, except very rarely (e. g., exception handling?) a go to statement may branch forward to a labeled continue statement. 4.2 Format 1. Use a character string for each format. If it is used once, the string may appear in the data transfer statement itself. print (a, f0.2), The price is $, price If it is used multiple times and does not change, use a character parameter; otherwise, a character variable. character(len = *), parameter :: PRICE_FORMAT = (a, f0.2)... print PRICE_FORMAT, The price is $, price 6

5 Miscellaneous 1. Do not use nonstandard or obsolescent features. 2. Do not write tricky code to save a few lines or a few milliseconds. If execution efficiency is important, write code that reflects the algorithm, instrument the code, try some variations, and comment liberally including the original code. 3. Do frequent error checking. For example, test if dummy argument values are reasonable, use status variables for input/output and allocation, and use the inquire statement prior to opening a file. Always test a status variable after it is used. If the problem is not clear, try removing the status check and see what the system produces. 4. Use parameters (named constants) instead of integer, real, or complex literal constants. 5. All character dummy arguments and parameters should be declared with length *. 6. All functions should be pure and have no side effects (note: what constitutes a side effect is controversial). This restriction may be removed temporarily to use debugging output statements, if a graphical debugger is not being used. 7. All array dummy arguments should be assumed shape (:). 8. All dummy arguments should have their intent declared. All function dummy arguments should be intent in. 9. Use keyword argument calls frequently, including with intrinsic procedures. 10. Use keywords frequently in structure constructors. 11. Each use statement should have an only clause to identify the source of the used entities. An exception might be for a module with a very large number of used entities. 12. When both pointers and allocatables will do the job, use allocatables. 13. Use the NEWUNIT intrinsic to select am input/output unit number. Never use singledigit unit numbers for input/ouput. 14. For standard and error input/output units, use INPUT_UNIT, OUTPUT_UNIT, and ERROR_UNIT from the intrinsic module ISO_FORTRAN_ENV. 15. Do not check for equality or inequality between real or complex values; use closeness instead. 16. Use logical variables for flags, not integers (and certainly not reals). 7

17. In a context that requires conversion of complex to real or integer or conversion of real to integer, use the intrinsic conversion functions real or int. 18. Put at least one digit on each side of the decimal point in real and complex literal constants. 8

Index A allocatable...7 allocation...7 array...4 array dummy argument...7 assigned go to...6 assumed shape...7 B blank lines...5 block...5p. C character dummy argument...7 character parameter...7 colon...3 comment...5 computed go to...6 continue...6 continued line...5 control flow...6 conversion of type...7 D declaration...3 delimiter...5 derived-type definition...2 do...6 dummy argument...7 E efficiency...7 error checking...7 error i/o unit...7 F file name...4 flag...7 format...6 free-form source...5 function...2 G go to...6 I if...6 implicit none...3 indent...5 initialization...3 input/output...7 inquire statement...7 intent...7 internal...2 intrinsic procedure...7 ISO_FORTRAN_ENV...7 K keyword...3, 7 L label...6 library...2 line length...5 literal constant...7 logical variable...7 M module...2 N name...3 O only clause...7 P parameter...2, 6p. pointer...7 private...2 procedure...2

Index 10 S select case...6 side effect...2, 7 standard i/o unit...7 status variable...7 submodule...2 subroutine...2 subscript...3 U unit number...7 use statement...7 V variable...6 W white space...5

Index 11