-
Notifications
You must be signed in to change notification settings - Fork 10
Open
Labels
Description
This is probably one of the most personal guideline.
I have to admit that I have not yet found yhe one that I love so I often change my mind resulting into somehow disorder style (that really hurts my soul).
I read about many nice idea. I will try to report them here (I need some times, but do not wait me, start suggestiong your convention) in order to promote our discussion.
From Richard Maine (stated here)
- Use verb forms for subroutine names and noun forms for functions, e.g. a function to return some useful thing might be named
useful_thingwhile a subroutine would be better named something likeget_useful_thing.
From one of the @cmacmackin guides
- Names should be descriptive, especially for publicly visible entities. Underscores should be used to separate words if a name is more than two words long. If a name is only two words long then an underscore may be used at your discretion. Sometimes they make the name more readable, sometimes they just make it longer. As with anything else in the code, use lower cases characters only.
- For identifiers avoid to exploit Case to indicate different words within them when Fortran is case-insensitive, the language doesn't recognize it.
- submodule could be useful for type/module names disambiguation (and for avoid recompilation cascade penalties when implementation changes, but not the interface), but add verbosity: should be not recommended for small project.
From @certik best practices
- Use lowercase for all Fortran constructs (
do, subroutine, module, ...).- as consequence from lowercase adoption, exploit underscores rather than
camelCase.- but the except is to follow short mathematical notation for mathematical variables/functions (
Ylm, Gamma, gamma, Enl, Rnl, ...).- For other names use all lowercase: try to keep names to one or two syllables; if more are required, use underscores to clarify (
sortpair, whitechar, meshexp, numstrings, linspace, meshgrid, argsort, spline, spline_interp, spline_interpolate, stoperr, stop_error, meshexp_der).camelCasefor variables comes from Java and I pretty strongly do not recommend it.
From @milancurcic best practices
- reserved Fortran words in all uppercase - this is what I have mostly been doing in the past, however I recommend all lowercase for reserved words just as well.
- variables lower-case
- underscores for multi-word variables
- camelCase for procedure names
TYPE :: datetime
INTEGER :: year = 1 ! Year [1-HUGE(year)]
INTEGER :: month = 1 ! Month in year [1-12]
INTEGER :: day = 1 ! Day in month [1-31]
INTEGER :: hour = 0 ! Hour in day [0-23]
INTEGER :: minute = 0 ! Minute in hour [0-59]
INTEGER :: second = 0 ! Second in minute [0-59]
INTEGER :: millisecond = 0 ! Milliseconds in second [0-999]
REAL(KIND=real_dp) :: tz = 0 ! Timezone offset from UTC [hours]
CONTAINS
PROCEDURE :: addMilliseconds
PROCEDURE :: addSeconds
PROCEDURE :: addMinutes
PROCEDURE :: addHours
PROCEDURE :: addDays
PROCEDURE :: isocalendar
PROCEDURE :: isoformat
PROCEDURE :: isValid
PROCEDURE :: now
PROCEDURE :: secondsSinceEpoch
PROCEDURE :: strftime
PROCEDURE :: tm
PROCEDURE :: tzOffset
PROCEDURE :: utc
PROCEDURE :: weekday
PROCEDURE :: weekdayLong
PROCEDURE :: weekdayShort
PROCEDURE :: yearday
ENDTYPE datetimeFrom @rouson best practices
- prefer all lowercase
- prefer underscores over camelCase =>
add_millisecondsoveraddMilliseconds- use mixed case in situations where underscores are not allowed, e.g., in defined operators and in
name=field of bind(C) pricedures when applicable:type foo contains procedure :: negate_foo generic :: operator(.negateFoo.) => negate_foo end type
- procedure names must have an action verb in them.
- logical variables must be named to reflect the state they are used to convey, most with the verb to be, e.g. :
lib_is_initializedvslib_init;obj_has_parentvsobj_parent;- do not strive to much in local shortening of variables name, prefer meaningful name: local shortening is often possible with the
associateconstruct.- avoid single character suffix for types and modules, e.g.
_tand_m, because:
- a single character has the lowest information entropy possible in a language. Probably for this reason, conveying meaning with a single character is bound to confuse anyone who is not already familiar with the convention.
- I have no problem with borrowing from other languages, but there just aren’t enough Fortran programmers doing this for it to be widely a widely recognizable convention.
- on the contrary prefer suffix like
_interfaceto module names and_implementationto submodule names.
From @victorsndvg best practices
- exploit camelCase over underscores;
- prefer a complete meaningful action-descriptive name for procedures, even if the resulting name is very very long, e.g.
PerformAnActionWithThisVariableOfTypeXXXWhileThisContext- prefix type bound procedures (TBP) with the type name for allowing an easy refactoring of codes (it becomes very easy to move class from a module into a new one without change the procedures names), e.g.
type :: my_object contains procedure :: action => my_object_PerformAnActionWithThisVariableOfTypeXXXWhileThisContext end type
From @zbeekman best practices
- exploit
camelCasefor procedures to be able to immediately identify an entity as a procedure.- names must have meaning.
- procedure names must have an action verb in them.
- named constants (parameters?) are in
UPPERCASE.- logical variables must be named to reflect the state they are used to convey, most with the verb to be, e.g. :
lib_is_initializedvslib_init;obj_has_parentvsobj_parent;- append
_tto derived type names and_mto module names to help namespace: you can have modules with effectively the same name as the class they implement, and one could even declare an object with the same name as the derived type/class if one wanted to do so; also the_tis a common convention in some other languages like C.
From @LadaF best practices
- prefer undescores over
camelCasedue to consistency with the Fortran standard naming convention and the case insensitivity of names Fortran.- structure constructors and other similar function should not have any verb in them, their name is just what they return, due to consistency with the Fortran standard default and overloaded structure constructors naming.
- avoid module suffixes, expect (maybe) for type names disambiguation, e.g.
type(Sphere) :: sphere ! won't work type(spere_t) :: sphere ! works
From @tclune best practices
- prefer undescores over
camelCasedue to consistency with the Fortran standard naming convention and the case insensitivity of names Fortran.- however,
camelCasecould be admissible for module names and type names; in fact, type names are proper nouns in the language, and as such, I at least one to capitalize them; moreover,camelCasehelps to accentuate this when the noun is multiple words; but more importantly it often solves type names disambiguation, e.g.type (SphereShape) :: sphere_shape ! or just sphere
- append
_tto derived type names to disambiguation aims could be help, it could be considered superfluous, i.e. my usual pattern is to define one “class” per Fortran module prefixpkg_(over suffix with_mod) for all modules in the package, e.g.module pkg_SphereShape_mod ... type :: SphereShape