March 22nd, 2013
Today, the first post in a (potentially long ;-)) series of posts filed under “I Wish C/AL Was More Like C#”.
I like how C# prevents me from making the kind of mistakes that my - slightly chaotic - mind frequently makes: the kind of mistake that’s a lot more difficult to find at run-time than it is at “code-time”. Here’s an example:
The TrimPrefix function can be used to trim off a string from another one, if the latter starts with the former. A simple function that I wrote almost without thinking, while my brain was working on the proverbial bigger picture ;-). My first tests succeeded nicely, but after a while I started to notice blank values in some records. Of course, I had failed to
EXIT(Text) in case the prefix is not found, i.e.
Text starts with something other than
C# is a lot more strict about these things, and would have told me straight away that some of the code paths in my function do not result in a proper return value. This function…
…would lead the compiler to complain about this…
…which in turn would tell me to make this correction:
I wish this kind of check could be added to C/AL, but I realise doing so would break a lot of existing applications. Perhaps a compiler warning would be nice? If you tell the compiler that your function has a return value, you’d better make sure it does on all code paths!