NAV2013 Beta to RTM – Some Striking Code Changes #8: IF-THEN-ELSE

This last item in my NAV2013 Beta to RTM – Some Striking Code Changes series is addressing code changes NAV core team made to get IF-THEN-ELSE statements coding standards compliant.

If you read the Microsoft Dynamics NAV 2009 Developer and IT Pro Documentation on IF-THEN-ELSE, and also on the use of parentheses, you’ll notice that there are quite some conventions to take into account. Quite some to violate, isn’t it?

Let’s take some exemplar code changes that underline these conventions. It think it’s a good way to refresh your coding standards knowledge, like most of the previous posts in this series could have done that somehow.

1. EXIT or ERROR as last statement in the THEN part of an IF-THEN-ELSE?
    Do not continue with an ELSE statement

Correct

IF <Condition> THEN
  <Statement Block ending with ERROR or EXIT>
<Statement>

Incorrect

IF <Condition> THEN
  <Statement Block ending with ERROR or EXIT>
ELSE
  <Statement>

Click on the image to get a full view that’s readable.

2. Compound or long expressions after IF?
    THEN should be on a new line and aligned with IF

Correct

IF <Condition1> AND
    <Condition2>
THEN
  <Statement>

Incorrect

IF <Condition1> AND
    <Condition2> THEN
  <Statement>

Click on the images to get a full view that’s readable.

Note

This also applies to the DO part of a WHILE-DO statement

3. Use parentheses only to enclose compound expressions inside compound expressions

I.e. do not use redundant parentheses.

Correct

IF Boolean1 AND Boolean2 AND (x = y) THEN
  <Statement>

Incorrect

IF ((Boolean1) AND Boolean2 AND (x = y)) THEN
  <Statement>

Click on the images to get a full view that’s readable.

Note

This also applies to WHILE-DO and CASE statements

3 Comments

  1. Good question, Erik. Sanely suspicious. 😉

    To be honest I did not check that, but seeing the code it seemed clear to me that most examples were about already existing code.

    And I just did a check on my examples and saw that my assumption were in most cases right (luckily 😉 ). So there was some NAV 2013 specific code updated between Beta and RTM, but most of these "striking code changes" do show up when comparing NAV 2009 R2 and NAV 2013.

  2. Read my lips.

    Our (at least my) abiltiy to recognize patterns is heavily delayed when I get to review/browse code that is using different structuring/layout. And thus we are (again at least I am) more liable to make errors.

  3. For many the "big picture" is way beyond their "backyard", unfortunately. So many times projects and resources have to learn it the hard way.

Leave a Reply

Your email address will not be published. Required fields are marked *