Let's say you have a Fortran 90 module containing lots of variables, functions and subroutines. In your USE
statement, which convention do you follow:
- explicitly declare which variables/functions/subroutines you're using with the
, only :
syntax, such asUSE [module_name], only : variable1, variable2, ...
? - Insert a blanket
USE [module_name]
?
On the one hand, the only
clause makes the code a bit more verbose. However, it forces you to repeat yourself in the code and if your module contains lots of variables/functions/subroutines, things begin to look unruly.
Here's an example:
module constants
implicit none
real, parameter :: PI=3.14
real, parameter :: E=2.71828183
integer, parameter :: answer=42
real, parameter :: earthRadiusMeters=6.38e6
end module constants
program test
! Option #1: blanket "use constants"
! use constants
! Option #2: Specify EACH variable you wish to use.
use constants, only : PI,E,answer,earthRadiusMeters
implicit none
write(6,*) "Hello world. Here are some constants:"
write(6,*) PI, &
E, &
answer, &
earthRadiusInMeters
end program test
Update Hopefully someone says something like "Fortran? Just recode it in C#!" so I can down vote you.
Update
I like Tim Whitcomb's answer, which compares Fortran's USE modulename
with Python's from modulename import *
. A topic which has been on Stack Overflow before:
‘import module’ or ‘from module import’
- In an answer, Mark Roddy mentioned:
don't use 'from module import *'. For any reasonable large set of code, if you 'import *' your will likely be cementing it into the module, unable to be removed. This is because it is difficult to determine what items used in the code are coming from 'module', making it east to get to the point where you think you don't use the import anymore but its extremely difficult to be sure.
- In an answer, Mark Roddy mentioned:
What are good rules of thumb for python imports?
- dbr's answer contains
don't do from x import * - it makes your code very hard to understand, as you cannot easily see where a method came from (from x import *; from y import *; my_func() - where is my_func defined?)
- dbr's answer contains
So, I'm leaning towards a consensus of explicitly stating all the items I'm using in a module via
USE modulename, only : var1, var2, ...
And as Stefano Borini mentions,
[if] you have a module so large that you feel compelled to add ONLY, it means that your module is too big. Split it.
Yes, please use
use module, only: ...
. For large code bases with multiple programmers, it makes the code easier to follow by everyone (or just usegrep
).Please do not use include, use a smaller module for that instead. Include is a text insert of source code which is not checked by the compiler at the same level as use module, see: FORTRAN: Difference between INCLUDE and modules.
Include
generally makes it harder for both humans and computer to use the code which means it should not be used. Ex. from mpi-forum: "The use of the mpif.h include file is strongly discouraged and may be deprecated in a future version of MPI." (http://mpi-forum.org/docs/mpi-3.1/mpi31-report/node411.htm).