Loading…

Design By Contract Helpers

Design by contract is a documentation-and-verification style: a function states what it requires of its caller and what it guarantees in return, and those statements are checked, not just written in a comment that can quietly go stale. Neither MQL4 nor MQL5 can throw or catch, so a broken contract here can never unwind execution the way an exception would elsewhere - what this library does instead is refuse to stay quiet about it. Every failed DBC_REQUIRE, DBC_ENSURE, DBC_INVARIANT or DBC_ASSERT Prints exactly which function and line it happened at and increments a global counter, so an OnInit that calls DbcSummary() before starting to trade gets a real stop condition out of what would otherwise be just a log line nobody reads until after something breaks. Every entry point is a macro, not a wrapper function, and that is not a style choice - it is required for the location reporting to work at all. __FUNCTION__ and __LINE__ are preprocessor tokens that expand where they appear in the source; routing the call through an ordinary function first would make every violation report the inside of this header instead of the line in the caller's own code that actually broke. Both platforms expand these tokens identically at the call site, which is why the macro plumbing needed no changes during the port. The feature that separates this from a plain assertion helper is the release switch. Writing `#define DBC_DISABLE_CONTRACTS` on its own line before including this header switches every macro to a trivial function call that never evaluates its condition or message argument at all - so a contract that is expensive to check, a loop over open positions or a file read, costs nothing once contracts are switched off for a release build that already went through development with them on. The macros were deliberately not written as a bare `true` literal: strict diagnostics on both platforms flag a discarded constant expression as "expression has no effect", which this catalogue's house style forbids, so the disabled path still makes one real (empty) function call instead - the honest zero-warning way to get the same effect on either platform. Neither platform's preprocessor understands a real #if with an expression, only #ifdef/#ifndef, which is why the switch above is presence-based rather than a 0/1 flag: DBC_DISABLE_CONTRACTS being defined at all is what flips it, regardless of any value assigned to it. Notes and limitations - A failed contract can only Print and count on either platform - it cannot stop the function it was written in from continuing to execute and returning a value, because neither MQL4 nor MQL5 has exceptions to unwind with. Always check DbcSummary() (or DbcViolationCount()) explicitly at a point where you can actually act on it, rather than assuming a violation halted anything. - The message argument is a plain string, not a format string - build any dynamic detail into it with StringFormat() before passing it in. - Defining DBC_DISABLE_CONTRACTS removes all checking; use it only after the code has been exercised with contracts on, the same way you would only strip C's assert() for a build that already passed testing. - Every macro takes exactly a condition and a message; there is no variant that logs extra context automatically, by design, to keep the macro expansion small enough that the disabled path stays a single no-op call.

Script · MT5 · v1.0 · Free

About Design By Contract Helpers

Design By Contract Helpers is a script for MetaTrader 5. MQL source code included.

Files in the download: DesignByContract.mq5.mqh, DesignByContract.txt, DesignByContractDemo.ex5, DesignByContractDemo.mq5.

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria