Fortran 简明教程
Fortran - Programming Style
编程风格涉及的是在开发程序时遵循一些规则。这些良好的做法会给您的程序带来可读性、明确性等价值。
Programming style is all about following some rules while developing programs. These good practices impart values like readability, and unambiguity into your program.
一个好的程序应具备以下特点:
A good program should have the following characteristics −
-
Readability
-
Proper logical structure
-
Self-explanatory notes and comments
例如,如果您做出如下评论,则不会有太大帮助:
For example, if you make a comment like the following, it will not be of much help −
! loop from 1 to 10
do i = 1,10
然而,如果您正在计算二项式系数,并且需要这个循环求 nCr,那么这样的注释会有帮助:
However, if you are calculating binomial coefficient, and need this loop for nCr then a comment like this will be helpful −
! loop to calculate nCr
do i = 1,10
-
Indented code blocks to make various levels of code clear.
-
Self-checking codes to ensure there will be no numerical errors like division by zero, square root of a negative real number or logarithm of a negative real number.
-
Including codes that ensure variables do not take illegal or out of range values, i.e., input validation.
-
Not putting checks where it would be unnecessary and slows down the execution. For example −
real :: x
x = sin(y) + 1.0
if (x >= 0.0) then
z = sqrt(x)
end if
-
Clearly written code using appropriate algorithms.
-
Splitting the long expressions using the continuation marker ‘&’.
-
Making meaningful variable names.