Share via


Clear Code Examples

Applies To: Microsoft Dynamics AX 2012 R3, Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynamics AX 2012

The code in the two examples below can be rewritten to be much clearer.

Example 1

From:

if (args.parmEnumType() != enumnum(BMBuildIdEnum))

{

    if (args.record().tableId == tableNum(BMmoduleTable))

    {

        moduleTable = args.record();

        buildId = moduleTable.buildId;

    }

    else

    {

        return null;

    }

}

else

{

    buildId = args.parmEnum();

}

...

To:

if (args.parmEnumType() == enumNum(BMBuildIdEnum))

{

    buildId = args.parmEnum();

}

else

{

    if (args.record().tableId == tableNum(BMmoduleTable))

    {

        moduleTable = args.record();

        buildId = moduleTable.buildId;

    }

    else

    {

        return null;

    }

}

...

The rewrite puts the most important case first, and removes the negative logic used in the first if statement in the first version of the code.

Example 2

From:

ledgerJournalTrans = this.ledgerJournalTransInitFromCreate(_tmpProjAdjustmentCreate);

    if (ledgerJournalTrans.validateWrite())

    {

        ledgerJournalTrans.insert();

        ProjPostLedger = ProjPost::construct(ledgerJournalTrans,ledgerVoucherTrans);

        if (projPostLedger.checkTrans())

        {

            projPostLedger.PostTrans();

        }

        else

        {

            throw error("@SYS21628");

        }

    }

    else

    {

        throw error("@SYS21628");

    }

    ledgerjournalTrans.delete(false);

...

To:

ledgerJournalTrans = this.ledgerJournalTransInitFromCreate(_tmpProjAdjustmentCreate);

if (!ledgerJournalTrans.validateWrite())

{

    throw error("@SYS21628");

}

ledgerJournalTrans.insert();

ProjPostLedger = ProjPost::construct(ledgerJournalTrans,ledgerVoucherTrans);

     

if (!projPostLedger.checkTrans())

{

    throw error("@SYS21628");

}

    projPostLedger.PostTrans();

    ledgerjournalTrans.delete(false);

...

See also

Dead Code Examples

X++ Coding Standards

Announcements: New book: "Inside Microsoft Dynamics AX 2012 R3" now available. Get your copy at the MS Press Store.