Poll: refactoring a chunk of legacy code

It is noteworthy that the segment of code that @ivanpribec made the topic of this thread occurs in Minpack-1. In one refactored version of Minpack-1, lines 793-803, we still see vestiges of this one-trip-do legacy, as well as the non-standard and unnecessary intrinsic DFLOAT, and see the intrinsic SUM rendered unavailable:

            sum = zero
            nm1 = n - 1
            if (nm1 >= 2) then
                do j = 2, nm1
                    sum = sum + dfloat(j)*x(j)
                end do
            end if
            do i = 1, m
                Fvec(i) = dfloat(i - 1)*sum - one
            end do
            Fvec(m) = -one

A manual refactoring into modern Fortran could have produced the more readable version:

sumj = sum([(j*x(j),j=2, n-1)])
fvec = [-1.0, ((i-1)*sumj - 1.0, i = 2, m-1), -1.0]

Note that this version rhymes better with the mathematical description from a journal article :

4 Likes