Maintainability and onboarding

Speed is the argument teams have and the cost they pay is the change. This page applies the same requirement to the same program in both languages and counts what it took: a median, which needs the values in order, which needs a sort. Fortran has no sort in its standard library; C++ has std::sort. The difference in the diff is 25 lines against 10, and both programs print the same three numbers.

One requirement change

The program before the change prints the mean and the standard deviation of five values, in one file, in both languages. The change adds a median to the output. A median is the middle value of the sorted data, and neither language gives it: C++ has the sort and Fortran has neither the sort nor the median, so the Fortran answer has to carry both.

The Fortran answer

The file stats_after.f90 after the change:

program stats_f use, intrinsic :: iso_fortran_env, only: dp => real64 implicit none real(dp) :: x(5) x = [2.0_dp, 4.0_dp, 4.0_dp, 5.0_dp, 9.0_dp] print '(a,f8.3)', 'mean = ', mean(x) print '(a,f8.3)', 'sd = ', sd(x) print '(a,f8.3)', 'median = ', median(x) contains pure function mean(v) result(m) real(dp), intent(in) :: v(:) real(dp) :: m m = sum(v) / real(size(v), dp) end function mean pure function sd(v) result(s) real(dp), intent(in) :: v(:) real(dp) :: s, m m = mean(v) s = sqrt(sum((v - m)**2) / real(size(v) - 1, dp)) end function sd pure function median(v) result(m) real(dp), intent(in) :: v(:) real(dp) :: m real(dp) :: w(size(v)) real(dp) :: key integer :: i, j, n w = v n = size(w) do i = 2, n key = w(i) j = i - 1 do while (j >= 1) if (w(j) <= key) exit w(j + 1) = w(j) j = j - 1 end do w(j + 1) = key end do if (mod(n, 2) == 1) then m = w((n + 1) / 2) else m = 0.5_dp * (w(n / 2) + w(n / 2 + 1)) end if end function median end program stats_f

gfortran -Wall -Wextra -std=f2018 stats_after.f90 -o after_f
./after_f

mean   =    4.800
sd     =    2.588
median =    4.000

The insertion sort is the one from the records capstone, twelve lines, stable and quadratic. For five values that is the right choice; for a million it is the wrong one, and the project would then need a library or a merge sort of its own. That is the real cost of the change: the Fortran programmer writes an algorithm the C++ programmer calls.

The C++ answer

The file stats_after.cpp after the same change:

#include <algorithm> #include <cmath> #include <cstdio> #include <numeric> #include <vector> double mean(const std::vector<double>& v) { return std::accumulate(v.begin(), v.end(), 0.0) / static_cast<double>(v.size()); } double sd(const std::vector<double>& v) { const double m = mean(v); double s = 0.0; for (double x : v) { s += (x - m) * (x - m); } return std::sqrt(s / static_cast<double>(v.size() - 1)); } double median(std::vector<double> v) { std::sort(v.begin(), v.end()); const size_t n = v.size(); if (n % 2 == 1) { return v[n / 2]; } return 0.5 * (v[n / 2 - 1] + v[n / 2]); } int main() { const std::vector<double> x = {2.0, 4.0, 4.0, 5.0, 9.0}; std::printf("mean = %8.3f\n", mean(x)); std::printf("sd = %8.3f\n", sd(x)); std::printf("median = %8.3f\n", median(x)); return 0; }

g++ -std=c++23 -Wall -Wextra stats_after.cpp -o after_c
./after_c

mean   =    4.800
sd     =    2.588
median =    4.000

std::sort sorts in place, so the function takes its argument by value and sorts the copy. That is the same decision the Fortran version makes with w = v, and in both languages a reader has to notice it: a sort that modified the caller's data would be a bug that the tests might not catch.

What the change cost

FortranC++
lines added to the file2510
new procedures1 (and a sort inside it)1
new includes01 (<algorithm>)
algorithm written by handa sort, 12 linesnone
outputthe same three numbersthe same three numbers

The counts are from diff -u between the two versions of each file, so they are a measurement of this change and not of the languages in general. A different requirement reverses the table: add a new numeric type to the program and Fortran's KIND machinery and array syntax usually cost less than C++'s templates and explicit loops, and copy the same function for a second array and Fortran's ELEMENTAL is one keyword.

What a new reader needs

A reader who has to change the Fortran file next needs to know: what PURE forbids, that W = V copies an array, that a function result needs a RESULT name when it is not obvious, and that the sort is there because the language has none. Four facts, all of them local to the file.

A reader of the C++ file needs to know: that std::sort mutates, that taking the argument by value is what protects the caller, that size_t and the index arithmetic in the even case are correct, and which header declares the function. Four facts as well, of a different kind: three of them are about a library rather than the language.

The practical difference for a team is not which list is shorter. It is that the Fortran reader can see the whole algorithm in the file, and the C++ reader has to know a library that is not in the file. For a numerical team that changes its code once a year and hires people who know Fortran, the first is worth more; for a team that hires from a large pool of C++ programmers and changes the code every week, the second is.

Exercises

  1. Add a min and a max to both programs. Which takes fewer lines now, and why is the answer different from the median?
  2. Change the requirement to "the median of each column of a 100 by 5 matrix". Which version is easier to change, and which needs the sort to be reusable?
  3. Make both programs take the values from a file. Which language needs more code for the parsing, and which for the error handling?
  4. Count the lines a reader must understand to verify the median in each language, including the library. Which count is larger, and does that match your intuition?

The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and G++ (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev

Banner image