Read with me what the Microsoft Dynamics NAV 2009 Developer and IT Pro Documentation phrases on BEGIN/END, i.e. compound, statement:
In some cases, the C/AL syntax only allows you to use a single statement. If however you have to execute more than one simple statement, the statements can be written as a compound statement by enclosing the statements between the BEGIN and END keywords.
Typically these cases can be:
- C/AL Conditional Statements (IF/ELSE and CASE)
- C/AL Repetitive Statements (FOR, WHILE and REPEAT)
- C/AL WITH Statements
So, as the quoted sentence states, the compound statement is applied when more than one statement is needed where the structure of the conditional, repetitive or WITH statement expects – or should I say: allows – only a single statement. Note that there is no explicit mentioning that you should use the compound statement, i.e. en-wrapping BEGIN/END, only in case of multiple statements. However, looking at the standard application, it has been the intention that this is part of the best practices. In spite of what others might argue – see for example this post: IF Without BEGIN and END Considered Harmful.
Comparing NAV 2013 Beta en NAV 2013 RTM I dare to assert that this best practice has been affirmed as many redundant BEGIN/END wrappers have been cleared out of the code. With many I mean many. I am not sure enough to say all.
See some examples in the screen shot.
Click on the images to get a full view that’s readable.
- Was it coincidence that around the time RTM could be downloaded the blog post IF Without BEGIN and END Considered Harmful was published?
- Which repetitive statement does not need a compound statement?
- Of course it was somewhat strange to see NAV Team developers violate this best practice in their example code at NAV TechDays 2012