Condition basics in AL

Recently I posted a “Did you know that …” post on Twitter showing an alternative way to write an if-condition. Today I thought: Hey, I might have some more examples for different ways to write conditions and here we are 😉

First start with the most simple if statement there is:

It just checks if the text variable is empty and if this is the case, some code is executed. Note that in AL the parentheses are not needed if you have only one condition or if you compare booleans.

This was easy. Now an example about how to check if two text variables are empty:

In this example parentheses are mandatory because in AL the “and” would try to evaluate this:

'' and MyTextVar2

Okay so far so simple. If you need to evaluate more than two conditions and maybe even nest conditions this can get messy.

I think you get the point.
If this happens I tend to write the conditions this way:

It looks strange at the beginning but on the second glance it is more clear to read. (At least in my opinion 😉 )

This is even easier to do for an “or” statement:

The IN statement checks if the value before IN is at least once within the list of values which follows.

Now, how about this example:

You can shorten this up with a simple case true of:

If nothing helps and your condition is not clear to understand, put it into a function with a name explaining what your condition is:

And don’t be afraid of long function names. Best is, if you can read the if statement like plain english.

And of course: Go to my GitHub and check it for yourself 😉

2 thoughts on “Condition basics in AL

  1. Hi,

    Me again with the public review 🙂
    1) using ‘in’ – to me the approach does not contribute to readibility and personally I wouldn’t do it in that way.
    2) using case instead of nested ‘if’s – looks nice, that is also how I would do it.
    3) long functions names: I would always suggest using the least amount of necessary words to describe the behaviour. If that can be achieved only with many words that sounds fine but I would not encourage writing long function names without the need.


    1. Thanks again for the feedback 😉
      1) I think this is a personal taste. I would not force “in” either. The examples here are to show what is possible. My last example on twitter though, is something I would totally recommend to anyone!
      3) Again, this is an example to show the concept. Rule is as short as possible but as long as needed to make clear what the purpose of the function is. If the name gets too long, maybe the function does too many things. But I think a function name which is too short is worse than one that is too long 😉


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: