This article covers the use of assertions in MQL5 language. It
provides two examples of the assertion mechanism and some general
guidance for implementing assertions.
Assertion is a special construction that
enables checking arbitrary assumptions in the program's arbitrary
places. They are typically embodied in the form of a code (mostly as a
separate function or macro). This code checks a true value of a certain
expression. If it appears to be false, then a relevant message is
displayed, and the program is stopped, given the implementation provides
for it. Accordingly, if the expression is true, it implies that
everything operates as intended - the assumption is met. Otherwise, you
can be certain, that the program has located errors and is clearly
notifying about it.
For example, if it's expected that a certain value X within
the program under no circumstance should be less than zero, then the
following statement can be made: "I confirm that a value of X exceeds or equals zero". If X happens to be less than zero, then a relevant message will be displayed, and a programmer will be able to adjust the program.
Assertions are particularly useful in big projects, where their component parts may be reused or modified with time.
Assertions should cover only those
situations that shouldn't occur during the program's regular operation.
As a rule, assertions can be applied only at the program's development
and debugging stages, i.e. they shouldn't be present in the final
version. All assertions must be removed during the final version's
compilation. This is usually achieved through conditional compilation.
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
New article Using Assertions in MQL5 Programs has been published on mql5.com:
This article covers the use of assertions in MQL5 language. It provides two examples of the assertion mechanism and some general guidance for implementing assertions.
Assertion is a special construction that enables checking arbitrary assumptions in the program's arbitrary places. They are typically embodied in the form of a code (mostly as a separate function or macro). This code checks a true value of a certain expression. If it appears to be false, then a relevant message is displayed, and the program is stopped, given the implementation provides for it. Accordingly, if the expression is true, it implies that everything operates as intended - the assumption is met. Otherwise, you can be certain, that the program has located errors and is clearly notifying about it.
For example, if it's expected that a certain value X within the program under no circumstance should be less than zero, then the following statement can be made: "I confirm that a value of X exceeds or equals zero". If X happens to be less than zero, then a relevant message will be displayed, and a programmer will be able to adjust the program.
Assertions are particularly useful in big projects, where their component parts may be reused or modified with time.
Assertions should cover only those situations that shouldn't occur during the program's regular operation. As a rule, assertions can be applied only at the program's development and debugging stages, i.e. they shouldn't be present in the final version. All assertions must be removed during the final version's compilation. This is usually achieved through conditional compilation.
Fig. 1. Example of an assertion
Author: Sergey Eremin