Sunday, November 13, 2016

Common mistakes we make in C/AL - Vol. 1 - Tips, Tricks & Facts #6

Hi peeps, starting a new series of posts on what are the common mistakes made by developers like us which have bad, sometimes worse, impacts on database transactions. And what the kind of coding styles we must avoid to keep the code neat and clean. And what are the DO's and DON'T's for writing C/AL code.

We gotta remember that we are not the last developers who will write a particular code somewhere in the NAV objects. There will be more developers after us who will be carrying the torch and some of them might run into some code which was written by us.

Therefore, your code must better be understandable and optimized!

For this post, I will talk about one of the most common C/AL functions which are used in everyday coding.

FIND('-'), FINDFIRST, ISEMPTY and FINDSET - When to use what and when not to use what out of these functions. I will be sharing thoughts based on my own experience and knowledge. Please share your thoughts in comments and update me and others with your thoughts on this topic.

Some posts already exist on the web comparing these 4 functions. But, there is still some space for knowledge left to share about these. I will answer common questions related to these functions as the definition of these functions can be found on MSDN. Therefore, here goes,

  • Using FIND('-') ? - See, using -,+ etc. with FIND is outdated. Using these parameters with FIND isn't what I recommend because we have advanced functions like FINDFIRST and FINDSET. We must use these two instead of using FIND('-') in our code.
  • FINDFIRST OR ISEMPTY ? - Always use ISEMPTY when you just want to check if some records exist in a particular table (under particular filters, the filters depend on the SETRANGE and SETFILTER used on the record variable). 

Do not use the following functions just to check if a table has (particular) records,
> FIND('-')

  • Using ISEMPTY ? - This function is the way to go for scenarios where you just want to check and you do not want to do anything (like modify, delete etc.) with the records existing in the table.
This function is very useful but the irony is we do not use it much. I didn't use it till the time I didn't know its importance. Read more about this function, start using it!

Checkout the screenshot below,

  • FINDFIRST OR FINDSET ? - Check this MSDN link in which it is mentioned that "You should only use FINDFIRST function when you explicitly want to find the first record in a table or set. Do not use this function in combination with REPEAT.. UNTIL."

There are conflicting views with using FINDFIRST with REPEAT.. UNTIL and I have gathered some valuable knowledge from a session from NAVTechDays 2016 and I have added a screenshot below to resolve these conflicts,

Therefore, according to the experts - we must use FINDSET to get the fastest results when we want to traverse thru all the records in the filters set by us on a record variable.

  • Using FINDFIRST ? - To be very true, this function is the least used function by me. I use either ISEMPTY or FINDSET depending upon the situation. There are not much situations (at least in my experience) when you just need the first record and you want to do something with it. But yes there are some scenarios where this function suits the best.

I have added some more screenshots below to add more info to this post. These screenshots are taken (again) from the NAVTechDays 2016 session on Bad Habits of NAV Developers. This session was given by Waldo, Vjeko and Gary Winters.

The 'first batch of records' mentioned in the screenshot above is the no. of records which we used to specify (if you remember) in the Record Set in Advanced tab in Alter Database options until one of the cumulative update of NAV 2015.

Be very careful. Your style of coding is your image as a developer. Don't take it for granted. If you do not know, ask or find about it and read and learn. Don't just write code for the sake of coding or completing your task.

Please share your thoughts in comments. Thanks.

Keep Learning!
Ishwar ;-)