1521-9615/12/$31.00 © 2012 IEEE
Published by the IEEE Computer Society
Fortranning It with Style
The author reviews the book Modern Fortran: Style and Usage, explaining why it's a good reference guide not only for novice Fortran users, but for anyone who wants to improve their code for clarity and performance.
The book Modern Fortran: Style and Usage is a powerful tool for any programmer who uses Fortran. More specifically, it's a great quick reference. Whether you're writing Fortran code from scratch; updating code for readability, efficiency, and performance improvements; or learning what the newest version of Fortran has to offer, this book will have something for you.
As the authors note in the preface, this book is written for Fortran programmers of all levels. However, this book shouldn't be used as a textbook, and it shouldn't be read straight through. The authors sought to make each chapter and section independent of the others so that quick referencing for a topic could be accomplished without reading the previous chapters, and I think they've succeeded. This was how I read the book when preparing for this review (jumping around from chapter to chapter), and I found it to work quite well.
Overview and Organization
The book is organized as a collection of strategies that the authors refer to as rules for creating powerful, well-organized Fortran programs, using the modern structure and performance capabilities of the current Fortran standard. Many of these rules describe general coding techniques and styles not specific to Fortran. Other rules apply specifically to new features in the latest versions of Fortran. In this book, as in life, there are some rules that must be followed and some that are mere suggestions. Overall, though, following the rules in this book will alleviate many headaches that you would otherwise encounter when designing programs.
The authors organize each chapter in the following way:
• introducing the chapter's topic and outlining the sections and subsections;
• presenting a rule;
• presenting the reasons why this rule should be followed;
• discussing what will happen if the rule is ignored;
• sampling code (if applicable or available); and
• showing the output for either correct adherence or dismissing the rule (if applicable or available).
I found this organization particularly useful for quickly finding a relevant rule and example. Also, each rule was presented clearly and concisely with consistent reasoning. If the rule involves a change to how an earlier version of Fortran did something, then this is explained, as well as the improvement by the updated feature. If it's a new feature, then the authors explain the needed improvement it provides.
For Novice Fortran Programmers
For organization, completeness, and to appeal to programmers new to Fortran, the earlier chapters in the book contain rules for more general programming, but are given with specific reference to the Fortran standard and its new features when available. Chapter 1 contains the font key and source code description. The rules begin in Chapter 2, outlining the reasons for some basic commonsense rules: making code easy to read, compile, efficient, transferrable to other problems, documented, reusable, and accessible to others. Chapters 3–9 describe rules that will accomplish these things by adhering to certain formatting (Chapter 3), naming (Chapter 4), documentation (Chapter 5), programming (Chapter 7), and packaging (Chapter 9) conventions.
Formatting contains the updates in spacing, line length, continuation, and commenting. The naming conventions were useful for learning how to use new features in Fortran to give more descriptive, clear, and accurate names to routines and variables, with the use of underscores. There's a self-documentation feature that's explained and advised in Chapter 5. Chapter 6 explains basic programming principles for designing modules conforming to the Fortran standard, and contains the first source code examples. Chapter 7 involves programming conventions—from initializing pointers to declaring arrays, stressing portability for different compilers and usage. Input and output features are explained in Chapter 8, from general read and write statements to formatting, such as "Use formatting reversion where possible to simplify format statements." Chapter 9 introduces submodules, in addition to basic good packaging practices of using modules and external functions, and placing separate programs, modules, and submodules in separate files.
Chapter 10 handles generic programming so that identical or slightly modified code can be used for different applications. By obeying these rules, a program's generic modules, subroutines, and functions can be applied easily to other data types with less error than by simply copying and manually changing the code. There's also a small section about using preprocessors within Fortran.
Detailing the Advancements
When reading this book, I started with Chapter 12 on parallel processing, which had the most relevance for me at the time. This chapter is self-contained, explaining the differences between the two supported methods of parallel computing in Fortran: Open Multiprocessing (OpenMP) and message passing interface (MPI). It covers the basics, from "Hello World" in parallel for the unfamiliar programmer to newer parallel features. I found the section on new Fortran 2008 features—such as incorporating coarrays into parallel programming and supplied source program with output—quite useful. I then proceeded to jump around a bit, sticking to most of the advanced chapters in the book (Chapters 11–15).
Chapter 11 introduces how object-oriented programming (OOP) is a new feature to Fortran 2003, but it began with limitation in Fortran 90. It also describes how Fortran's usage of "derived types" is similar to a "class" in other languages that already use OOP. The authors give a great introduction to OOP for those who haven't encountered it before, as well as those who might need a refresher. They also point out how Fortran incorporates OOP into existing and new features, and they note the differences between its usage and other languages' use of OOP. The final section of this chapter discusses polymorphism, with sample source code and output, specifically warning against the confusion of the polymorphic keyword class in Fortran and the derived data-type keyword type in Fortran. I found the clarifications given in this chapter to be highly beneficial.
Chapter 13 deals with advancements in how Fortran handles numerical precision, explaining "kinds" of floating-point operations, exceptions, and bit manipulation. Chapter 14 is useful for programmers who need a program written in Fortran to interact with a C program or vice versa. It explains recent new features beginning with Fortran 2003 for mapping between the programs. Finally, in Chapter 15, the book explains step by step how to update existing codes that contain obsolete or unsupported features for some compilers to the Fortran standard.
Ifelt the book was well-organized, in a manner that's similar to how you would expect a programmer to build a program. The chapters were all well laid out and consistent in their organization, making it easy to navigate through the book from chapter to chapter, finding different topics of interest. The book provides a number of full-sample source codes and demos for use by the reader as examples. I liked that each rule had a consistent framework of stating the rule and then giving an example. There's also a list of all the source code for the book at the front, as well as two full source code demos in the appendix. Some of the source codes were incomplete by design, and I would have enjoyed a file version of all the sample source code to avoid typing them in, to save time and errors. A second appendix contains a comprehensive list of all the rules given in the book.
Reading this opened my eyes to the many inconsistencies in programming style today. There are some great rules to add to how I program, to improve my code for clarity and performance. I have begun using underscore names, self-documentation for different versions of code, and plurals for arrays. This has also provided me with a great guide for learning how to incorporate the new features of 2003 and 2008 into my work. After reading it, I wish more people coded like this.
is an assistant professor in the Department of Physics and Astronomy at Francis Marion University. He's spent the past seven years programming in Fortran and other languages, and teaches a course in high-performance computational physics. His research interests include computational physics and cosmology. Moldenhauer has a PhD in physics from the University of Texas at Dallas. Contact him at firstname.lastname@example.org.