Early Exit

As a developer, whether you’re working with Microsoft Dynamics 365 Business Central or any other system, knowing some good coding practices can make a big difference. One such practice that can significantly improve your code quality is the “Early Exit” approach.

So, what is this “Early Exit” approach?

Early Exit is a programming practice where you leave a procedure immediately as soon as a specific condition is met, rather than waiting for the entire procedure to complete. Let’s talk about why it matters.

Why Use “Early Exit”?

1. Readability and Maintainability

It makes your code easier to understand because you can see the condition immediately. You don’t need to scroll down and search for it. The developer can quickly understand the procedure’s purpose and conditions under which it operates.

2. Performance Optimization

By stopping early, you’re saving precious time and resources. This matters, especially when your system is dealing with a lot of data or when resources are limited and expensive.

3. Faster Debugging

By exiting early, the logic becomes more straightforward and easier to follow and review. If something goes wrong, it’s easier to trace because you don’t have to follow the entire procedure. You just need to look at what happened until the exit point.

Examples

Let’s see how it works with some examples.

Late Exit

We can use Early Exit method to improve the code.

Early Exit before looping

We can improve it a little bit more by filtering the line condition.

Early Exit with Filter

Handling Specific Scenarios

Suppose the ResetPurchaseLineDiscount procedure is called from a method other than ResetPurchaseDiscount, we will need to find another way to develop it. Here are two ways to handle that scenario:

1) Adding an additional Check parameter inside the procedure to indicate whether the condition should be checked.

Early Exit with CheckCondition Parameter

2) Have two procedures: one with the check, one without.

Early Exit with two procedures

Conclusion

“Early Exit” is an excellent way for writing better procedures. Incorporating early exits into your coding practices will result in cleaner, more efficient code that is easier to maintain over time. Try to keep it in mind as you code, but remember, it’s not a one-size-fits-all solution. Use it wisely and happy coding!

Bonus tips: learn about lazy evaluation to squeeze even more performance out of your code.

thatnavguy

Experienced NZ-based NAV Developer and Consultant with 15+ years of experience leading multiple IT projects, performing business analyst, developing, implementing, and upgrading Dynamics NAV and Business Central. Passionate to deliver solution that focuses on user-friendly interface while keeping high standard of compliance with the needs.

You may also like...

1 Response

  1. Nick Goddard says:

    You could make another early exit improvement by doing the following which will mean your repeat is not indented:
    if not PurchaseLine.FindSet() then
    exit;
    repeat
    ….
    until PurchaseLine.Next() = 0;

Leave a Reply

Your email address will not be published. Required fields are marked *