mfp's two cents

...on Dynamics AX Development!
  • mfp's two cents

    Writing less code: The "else" statement

    • 17 Comments

    Source code is written once; and read over and over again. So make sure it is easy to read and understand.

    I keep seeing a poor coding practice. I see it in production code, in test code, in X++ code, in C# code, in C++ code, in examples on the web, when interviewing candidates. I see it where ever I look. It is a practice that adds unnecessary complexity to source code, it make the source code harder to read, and harder to write. And what disturbs me the most is, how easy it is to avoid.

    Take a look at this simple code:

    boolean isNegative(int value)
    {
        if (value<0)
        {
            return true; 
        }
        else
        {
            return false;
        }
    }  
    The "else" statement is completely superfluous. This rewrite does exactly the same, and is easier to read:
    boolean isNegative(int value)
    {
        if (value<0)
        {
            return true; 
        }

        return false;   
    }
    Yes; the compiler will optimize the "else" statement away - but that is not my point. The compiler can easily see through the weirdest constructs. I (and most other human beings) cannot. Granted; every developer worth his salt should have no problem understanding both of the above implementations. However often the requirements are more complex.
    int foo(int bar)
    {
        if ( /*expr1*/ )
        {
            throw /*some exception*/; 
        }
        else
        {
            if ( /*expr2*/ ) 
            {
                return 1;
            }
            else
            {
                if ( /*expr3*/ ) 
                {
                    return 2;
                }
                else
                {
                    if ( /*expr4*/ ) 
                    {
                        return 3;
                    }                
                    else
                    {
                        throw /*some exception*/;
                    }
                }
           }
        }
    }
    Or, this slightly better version: 
    int foo(int bar)
    {
        if ( /*expr1*/ )
        {
            throw /*some exception*/; 
        }
        else if ( /*expr2*/ ) 
        {
            return 1;
        }
        else if ( /*expr3*/ ) 
        {
            return 2;
        }
        else if ( /*expr4*/ ) 
        {
            return 3;
        }
        else
        {
            throw /*some exception*/;
        }
    }
    Could be simply rewritten as:
    int foo(int bar)
    {
        if ( /*expr1*/ )
        {
            throw /*some exception*/; 
        }
        if ( /*expr2*/ ) 
        {
            return 1;
        }
        if ( /*expr3*/ ) 
        {
            return 2;
        }
        if ( /*expr4*/ ) 
        {
            return 3;
        }      
        throw /*some exception*/;
    }
    Never-ever-under-any-circumstances-for-any-reason-what-so-ever write an "else" statement if the preceding block unconditionally returns or throws an exception.

    Now I got it off my chest :-)

  • mfp's two cents

    Upgrading to Microsoft Dynamics AX 4.0 and Microsoft Dynamics AX 2009 @ Convergence 2008

    • 2 Comments

    Deepak Kumar and myself will be hosting a session on the upgrade process at Convergence in Orlando next month. I hope to see you there. 

    Upgrading to Microsoft Dynamics AX 4.0 and Microsoft Dynamics AX 2009; 3/13/2008 10:00AM-11:00AM; W204

    This session will take you through the end-to-end flow of upgrading from Microsoft Dynamics AX 3.0 to Microsoft Dynamics AX 4.0 and Microsoft Dynamics AX 2009. You will see an overview of the code upgrade as well as the data upgrade process along with recommended best practices. Examples of the improved upgrade documentation, with a focus on the implementation guidelines that come with a dedicated section for upgrade, will also be covered. This session will also include tips on performing data upgrade and code upgrade more efficiently and effectively. Additionally, you'll learn tips on the extra steps needed to upgrade if you've extended your solution. This session is designed for partners and customers who are planning to upgrade or would like to learn more about a Microsoft Dynamics AX upgrade.

  • mfp's two cents

    Erik and Anni goes to Hollywood

    • 1 Comments

    First episode of Erik Damgaard's new endeavor is now available.

    http://www.kanal4.dk/erik_og_anni_goes_to_hollywood/

    (in Danish)

    Best wishes from here.




Page 1 of 1 (3 items)

February, 2008

mfp's two cents

...on Dynamics AX Development!