DCDFLIB Example Code Re-Factoring

by Codewiz51 January 22, 2010 20:39

This is a simple example of the basic code refactoring exercise I am performing with DCDFLIB.  All of these code examples were examined with reflector after compiling to determine a "best path" to follow.

C++ code converted from FORTRAN:

    if(!(which < 1 || which > 4)) goto S30;
    if(!(which < 1)) goto S10;
    bound = 1.0e0;
    goto S20;
S10:
    bound = 4.0e0;
S20:
    status = -1;
    return;
S30:

Re-factored code in managed c++:

    if (which < 1)
    {
        bound = 1.0e0;
        status = -1;
        return;
    }
    else if (which > 4)
    {
        bound = 4.0e0;
        status = -1;
        return;
    }

I looked at re-factoring using more of the existing code, but I found the compiler could do a better optimizing job using the first code snippet.

Re-factoring code path I decided not to use:

    if ((which < 1) || (which > 4))
    {
        if (which < 1)
            bound = 1.0e0;
        else
            bound = 4.0e0;

        status = -1;
        return;
    }

If I include just a bit more code when attempting the re-factor operation, the following re-factor code is possible.  It did not seem to offer any advantages when the IL assembly was examined using reflector.

    double bound = 0.0;

    if (which < 1)
        bound = 1.0;
    else if (which > 4)
        bound = 4.0;

    if (0.0 != bound)
    {
        status = -1;
        return;
    }

Comments are closed

Powered by BlogEngine.NET 1.6.0.0
Theme by Mads Kristensen | Modified by Mooglegiant

Disclaimer

This blog represents my personal hobby, observations and views. It does not represent the views of my employer, clients, especially my wife, children, in-laws, clergy, the dog, the cats or my daughter's horse. In fact, I am not even sure it represents my views when I take the time to reread postings.

© Copyright 2008-2011