You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: chapters/Coding_style.tex
+4-4Lines changed: 4 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -46,14 +46,14 @@ \chapter{Coding style}
46
46
Don't worry about the overhead of calling a function.
47
47
If you use optimization flags like \texttt{-O3}, the compiler will try to inline them anyway.
48
48
\item\textbf{Modules}---Separate your program into logical units.
49
-
Combine related functions and subroutines into modules\footnote{It's also convenient to put all physical parameters (number of particles, $\pi$, $c$, $k_B$, etc.) into their own module, even if they're just constant numbers. You can then ``load'' the parameters into your subroutines with the \keyword{use} statement instead of having absurdly long lists of arguments, or worse, global variables.}.
49
+
Combine related functions and subroutines into modules\footnote{You may also find it convenient to put all physical parameters (number of particles, $\pi$, $c$, $k_B$, etc.) into their own module, even if they're just constant numbers. You can then ``load'' the parameters into your subroutines with the \keyword{use} statement instead of having absurdly long lists of arguments, or worse, \Biohazard\ global variables\Biohazard.\index{global variables|see {biohazard}}\index{biohazard}}.
50
50
For example, a single module with function integrators, or ODE solvers.
51
51
If you use the object-oriented features of Fortran, it is also good practice to create separate modules for classes, \eg, a sparse-matrix object.
52
52
\item\textbf{Comments}---Comments are kind of a double-edged sword.
53
-
If used wrongly, they can actually hurt readability.
54
-
The golden rule is: never explain \emph{how} it works, just \emph{what} it does.
53
+
If used improperly or left unmaintained, they actually hurt readability.
54
+
The general idea is to avoid explaining \emph{how} your code works and instead just mention\emph{what} it does.
55
55
The how should be evident from the code itself.
56
-
\emph{``Good code is its own best documentation.''} If you follow the other guidelines regarding naming and keeping functions small, you can generally suffice with a small comment at the beginning of every function or subroutine.
56
+
\emph{``Good code is its own best documentation.''} If you follow the other guidelines regarding naming and keeping functions small, you can generally suffice with nothing more than a small comment at the beginning of every function or subroutine.
57
57
\end{itemize}
58
58
One final point, not of style, but important nonetheless: start every program and module with \keyword{implicit none}\marginnote{The \texttt{-fimplicit-none}\index{compiler flags!-fimplicit-none} compiler flag will also turn off implicit typing globally.}\index{Fortran!implicit none@\keyword{implicit none}}.
59
59
This is an unfortunate holdover from older versions of Fortran; it forces you to explicitly declare all variables, instead of allowing Fortran to infer the type from the name.
0 commit comments