Hi,
Currently in AL we have the common and / or operators which evaluate all expressions within a single statement. ex:
if FuncA() and FuncB() then;
in the above case, FuncB() will be evaluated regardless of the value of FuncA().
There is a cost associated with running a function needlessly.
Furthermore, an error will be raised if FuncB() depends on the success of FuncA().
Currently, the only way in AL to avoid such problems is to use nested conditional statements.
The same goes for the or operator.
if FuncA() or FuncB() then;
If FuncA() evaluates to true there is no need to call FuncB().
A short-circuit logical operators (like the && and || operators in other languages), therefore, seem quite valuable.
Comments
I support @Moustapha's proposal for introducing AndAlso/OrElse operators. Coming from a .NET background, I've found myself frequently needing short-circuit evaluation patterns in my code. This became particularly evident when working with token validation, where you'd want to check whether a token value is null, before checking if its contents are empty to avoid conversion errors. Adding these operators would significantly improve code scenarios like this.
Category: Development
I still voted for the idea as it would make code more readable.
Category: Development
Or you put this condition into a separate function where you exit when the first condition is not met.The exit if the second condition is not met, There are many ways to achieve this.
Category: Development
Hi @Adnan,Thank you for supporting this idea.The way I am thinking is not to change the behavior of the existing and / or operators we have already in BC.but to introduce new ones.For the and operator, that will be something like: && (as in most languages), AndAlso (as in VB.net) or and_then (as used in PASCAL)For the or operator, that will be something like: || (as in most languages), OrElse (as in VB.net) or or_else (as used in PASCAL).
Category: Development
I agree with this since lazy-evaluation of logical operators is a common thing in majority of programming languages and often is a performance boost. On the other hand this change may cause instability issues within existing codebase.
Category: Development
Business Central Team (administrator)
Thank you for this suggestion! Currently this is not on our roadmap. We are tracking this idea and if it gathers more votes and comments we will consider it in the future. Best regards, Business Central Team