Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics NAV (Archived)

Coding Guidelines: Single lines, BEGIN/END and Readability

Posted on by 220

Hello,

as already commented on the blog entry regarding community guidelines, I don't agree with everything (although I really appreciate the effort! It's great, thank you! =)).

One particular thing I don't like is the use (or better lack of use) of BEGIN and END to enclose statements. MS recommends, that we only use it, when we need more than one statement. While I do not like this rule at all, I can understand its charme. But only if it is used with single line or easy to understand statements. Today I found this code in Table 39 Purchase Line and it drove me mad:

IF Quantity = 0 THEN BEGIN
  ...
END ELSE BEGIN
  GetPurchHeader;
  IF PurchHeader.Status = PurchHeader.Status::Released THEN
    AmountInclVAT := "Amount Including VAT"
  ELSE
    IF PurchHeader."Prices Including VAT" THEN
      AmountInclVAT := "Line Amount" - "Inv. Discount Amount"
    ELSE
      IF "VAT Calculation Type" = "VAT Calculation Type"::"Sales Tax" THEN BEGIN
        IF "Use Tax" THEN
          AmountInclVAT := "Line Amount" - "Inv. Discount Amount"
        ELSE
          AmountInclVAT :=
            "Line Amount" - "Inv. Discount Amount" +
            ROUND(
              SalesTaxCalculate.CalculateTax(
                "Tax Area Code","Tax Group Code","Tax Liable",PurchHeader."Posting Date",
                "Line Amount" - "Inv. Discount Amount","Quantity (Base)",PurchHeader."Currency Factor"),
              Currency."Amount Rounding Precision")
      END ELSE
        AmountInclVAT :=
          ROUND(
            ("Line Amount" - "Inv. Discount Amount") *
            (1 + "VAT %" / 100 * (1 - PurchHeader."VAT Base Discount %" / 100)),
            Currency."Amount Rounding Precision");
 VALIDATE(
   "Outstanding Amount",
   ROUND(
     AmountInclVAT * "Outstanding Quantity" / Quantity,
     Currency."Amount Rounding Precision"));
...

There are open ended IFs and ELSEs, which again call IFs and ELSEs and so on. It is really hard to read this code and to top it all off, there are statement calls broken into multiple lines to optimise the readablity of these particular lines while making it worse to identify where the IF-statements began and end. Especially the connection of the last VALIDATE statement is not that easy to identify. It would be much clearer if it would be like this:

IF Quantity = 0 THEN BEGIN
  ...
END ELSE BEGIN
  GetPurchHeader;
  IF PurchHeader.Status = PurchHeader.Status::Released THEN
    AmountInclVAT := "Amount Including VAT"
  ELSE BEGIN
    IF PurchHeader."Prices Including VAT" THEN
      AmountInclVAT := "Line Amount" - "Inv. Discount Amount"
    ELSE BEGIN
      IF "VAT Calculation Type" = "VAT Calculation Type"::"Sales Tax" THEN BEGIN
        IF "Use Tax" THEN
          AmountInclVAT := "Line Amount" - "Inv. Discount Amount"
        ELSE BEGIN
          AmountInclVAT :=
            "Line Amount" - "Inv. Discount Amount" +
            ROUND(
              SalesTaxCalculate.CalculateTax(
                "Tax Area Code","Tax Group Code","Tax Liable",PurchHeader."Posting Date",
                "Line Amount" - "Inv. Discount Amount","Quantity (Base)",PurchHeader."Currency Factor"),
              Currency."Amount Rounding Precision")
        END;
      END
      ELSE BEGIN
        AmountInclVAT :=
          ROUND(
            ("Line Amount" - "Inv. Discount Amount") *
            (1 + "VAT %" / 100 * (1 - PurchHeader."VAT Base Discount %" / 100)),
            Currency."Amount Rounding Precision");
      END;
  END;
VALIDATE( "Outstanding Amount", ROUND( AmountInclVAT * "Outstanding Quantity" / Quantity, Currency."Amount Rounding Precision")); ...
END;

What do you think?

Cheers,
Patrik

*This post is locked for comments

  • RE: Coding Guidelines: Single lines, BEGIN/END and Readability

    Hi Patrik,

    I think this code needs refactoring :) It's more than just clarifying where the IF/THEN/ELSE scope starts and ends, it's a whole "forest of IFs" which implements decision-making. Here we would need a behavioural design pattern to fix it in a more fundamental way. I would analyze if the Rules Table design pattern is not a good fit for this. We haven't published it yet on the Wiki, but we did show it at NAV Tech Days in 2014, here is a link: http://mibuso.com/dlinfo.asp?FileID=1615

    If the refactoring couldn't be done, I agree that your suggestion would make it better.

    Bogdana

  • Patrik Nordsiek Profile Picture
    Patrik Nordsiek 220 on at
    RE: Coding Guidelines: Single lines, BEGIN/END and Readability

    Yes and there should be some blank lines as well. I also agree, that too many Begin/Ends are confusing as well but they should only be left out if the following statement can be written in one line.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Coding Guidelines: Single lines, BEGIN/END and Readability

    Either way works...
    But your example is ruined by the lack of Space for indentation.

    Most important is that the code is readably, some times to many Begin/ends actually makes it worse, because the sleek compressed code is easier to get a good overview of.

  • mmv Profile Picture
    mmv 11,465 on at
    RE: Coding Guidelines: Single lines, BEGIN/END and Readability

    Hi Patrik,

    I agree with you, but MS doesn't stop you from using BEGIN and END if the IF statement has just one line.

    Best Regards,

    MMV

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans