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! 

  • FINDFIRST OR FINDSET ? - The most common question asked in each and every interview :-D. Always use FINDSET with a REPEAT UNTIL statement because FINDSET fetches a set of records from SQL before the repeat loop starts executing. FINDSET is much faster than FINDFIRST with a reapeat loop.
If you use FINDFIRST with a repeat loop then either you want your code to be slow or some variation of that. Reason behind not FINDFIRST is that it fetches only one record at a time from SQL and executes the code in between the REPEAT UNTIL statement, then it goes back to SQL and fetches the next record and executes the code and so on.

Be very careful. Your style of coding is your mirror 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.

  • 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. Seriously, 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 want to learn more on this topic. So, please share your thoughts in comments. Thanks.

Keep Learning!
Ishwar ;-)