MQL5 supports object oriented paradigm of programming from very beginning, and MQL4 has been improved to meet MQL5, so basically there is no difference between the languages (only core APIs of MetaTrader 4 and MetaTrader 5 are different).
This series of articles try to answer some of these questions, explain applicational value of OOP, and provide examples of using it in MetaTrader.
OOP is intended to make complex code simplier.
This works in many aspects, such as:
- brevity - lower number of code lines needed to implement a required product via reuse of existing classes (without any changes in the classes);
- reliability - error minimization as a consequence of decomposed code with managable access rules for different contexts (classes, variables);
- readability - short and clear presentation of algorithm in a notation, which is close to natural language and hierarchy of real-life entities;
- maintainability - when it becomes necessary, one need to change as little code lines as possible;
- standartization - best practices of solving common programming tasks are collected and available as compendiums of design patterns;
Main question here is - how can one know whether a specific code is complex enough to make it a good candidate for simplification by using OOP? Or maybe it will only become more complex?
Well, indeed, OOP can seem an overkill for a single buffer indicator, but if you have a bunch of similar indicators it is already an occasion for extracting common structures and behavioral patterns into classes. Anytime you find yourself making "copy&paste" of code you're probably missing the point to get the benefits of OOP world. You may say you could use plain old functions and includes for eliminating "copy&paste", and this works in many cases, but then it usually gets more and more agglomerated over time. And this is when OOP comes in handy. Just a simple example: when you include a library of functions, you have only 2 options - either use it "as is" (if it meets your needs entirely), or customize for specific purpose (with these specific changes affecting every other code relying on the library, which can be error prone). With OOP, you can include a class with base functionality, derive new descendant class from it, and customize it specifically for your code.
Just as a short reminder, let us formulate main principles of OOP. This is the last time we speak about theory, and will from now on concentrate on practice.
In OOP you define a class with an interface, beyond which all implementation details are hidden from the rest of code. This makes it easy to improve or completely change implementation when necessary without affecting other parts of code.
All tasks to be performed by a code are distributed among logically decoupled code parts - classes with clear and unique responsibility (purpose) and minimal dependencies.
An object pointer or reference can hold instance of a derived class. This allows you to call methods declared in the base class and actually invoke an overridden implemenation, thus customize base behavior partially and utilize all the other useful features of the parent.
Below you'll find a list of articles. All parts are not interconnected (unless it's explicitly stated in titles), so you can freely jump to any one of them if it concerns a topic you are interested in.
Table of contents
You can download a single booklet containing all the articles and source codes.