# Poll: refactoring a chunk of legacy code

**URL:** <https://fortran-lang.discourse.group/t/poll-refactoring-a-chunk-of-legacy-code/2796>\
**Category:** Poll\
**Created:** [February 15, 2022, 5:24am UTC](https://fortran-lang.discourse.group/t/poll-refactoring-a-chunk-of-legacy-code/2796 "2022-02-15T05:24:02Z")\
**Posts on this page:** 1\
**Showing post:** 17

<div class="post-metadata">

**Author:** ![mecej4](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/mecej4/32/855_2.png) [@mecej4](https://fortran-lang.discourse.group/u/mecej4)\
**Post date:** [February 15, 2022, 4:03pm UTC](https://fortran-lang.discourse.group/t/poll-refactoring-a-chunk-of-legacy-code/2796/17 "2022-02-15T16:03:18Z")

</div>

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](https://github.com/jacobwilliams/minpack/blob/main/test/test_lmder.f90), 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:

```auto
            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:

```auto
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](https://www.caam.rice.edu/~yzhang/caam454/nls/MGH.pdf) :

 ![f34](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/1/1f72b968e7f09d4ecce24486b40c58b02f6f38c1.jpeg)

---

_[View the full topic](https://fortran-lang.discourse.group/t/poll-refactoring-a-chunk-of-legacy-code/2796)._
