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:

Trim Prefix

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 Prefix.

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…

TrimPrefix in C  Wrong

…would lead the compiler to complain about this…

Error Message in C

…which in turn would tell me to make this correction:

TrimPrefix in C  Right

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!

3 Responses to “IWCWMLC#: Not all code paths return a value”

  1. Peter Says:

    Huh, found the same thing week ago. But, I don’t MS will implement something like this in C\AL. Finally, C# is a bit different mindset, yes, compiler will care about everything, but we all are paid to brain-compile our C/AL code :)

  2. jhoek Says:

    Agreed, Peter. I guess one could argue that C# was conceived with different mindset: intended for larger and more complex projects, and helping developers find and fix issues sooner. The new unit testing abilities in NAV may be a first step away towards software engineering (as opposed to software developing). :-)

  3. kriki Says:

    And initially, NAV was implemented with the idea that very little code should be added.
    The idea was to only add some fields in tables and forms and that was it.
    Programmers were a not-desired-option in the old days.

    That explains the still feature(-very)-poor C/AL editor.

    C# was created for programmers, so it is completely different.

Leave a Reply

To prove you're a person (not a spam script), type the security text shown in the picture. Click here to regenerate some new text.
Click to hear an audio file of the anti-spam word