Discussing the article: "Developing an MQL5 RL agent with RestAPI integration (Part 4): Organizing functions in classes in MQL5"

 

Check out the new article: Developing an MQL5 RL agent with RestAPI integration (Part 4): Organizing functions in classes in MQL5.

This article discusses the transition from procedural coding to object-oriented programming (OOP) in MQL5 with an emphasis on integration with the REST API. Today we will discuss how to organize HTTP request functions (GET and POST) into classes. We will take a closer look at code refactoring and show how to replace isolated functions with class methods. The article contains practical examples and tests.

In this article, we will take an important step and organize our functions into classes in MQL5. To do this, we will use object-oriented programming (OOP). OOP is a way of writing code that helps keep it organized and easy to understand. This is important because it makes it easier for us to maintain and improve the code. Its code is well organized and is modular, we can use it in different parts of the project or even in future projects.

Also, in this article, we will see how to restructure existing MQL5 functions into classes. We will see how this can make code more readable and efficient. Also, the article contains practical examples of how to do this, showing how the application of the presented ideas can make code easier to maintain and improve.

Object-oriented programming (OOP) is a powerful way of developing software. In MQL5, the use of classes is a great advantage over the procedural code writing method. In this part, we will look at how to improve the quality of our project using this characteristic. Let's look at four important aspects:

  1. Encapsulation and modularity: Classes help organize related functions and variables in one place, making them easier to maintain and reducing errors.

  2. Code reuse: Once you write a class, you can use it in different places, saving time and maintaining code consistency.

  3. Ease of maintenance and improvement: When functions are separated into classes, it is easier to find and fix bugs or make improvements because the clear structure makes the code more accessible.

  4. Abstraction and flexibility: Classes promote abstraction by hiding complexity and revealing only what we need. This makes the code more intuitive and flexible.

We will see that rearranging functions into classes in MQL5 is not just for the sake of beauty, it is a significant change that makes the code more efficient, easier to understand and maintain. The article will show how to transform isolated functions into well-defined class methods, which will provide both immediate and long-term benefits. This will not only improve our current project but will also help us create a solid foundation for future MQL5 projects.

Author: Jonathan Pereira

 

Hi Jonathan,


Great article, just to make you aware.  When you use the keyword "interface" you do not need to use "public:", "virtual" and " = 0;"


Shep 

 
Shephard Mukachi #:

Hello, Jonathan,


Great article, but just to warn you. When you use the "interface" keyword, you don't need to use "public:", "virtual" and " = 0;"


Shep

Thank you for your comment! I'm glad you liked the article. I'd like to clarify why I chose to use public: , virtual and = 0; in the interfaces, even though some of these elements may seem redundant:

  1. Code Clarity and Consistency:

    • In C++ and, by extension, in MQL5, make explicit public: , virtual and = 0; in interfaces improves readability and makes it clear that these methods are part of an interface that needs to be implemented. This is especially useful for developers who are more used to languages like Java, where these declarations are mandatory.
  2. Compatibility with C++:

    • MQL5 is based heavily on C++, where interfaces are defined with pure abstract classes. Explicitly using virtual e = 0; ensures that MQL5 code follows C++ best practices, making life easier for developers working with both languages.
  3. Implicit documentation:

    • Even if the members of an interface are implicitly public, declaring public: acts as a form of internal documentation, making it clear to anyone reading the code that these methods are publicly accessible. Virtual and = 0; reinforce that the class is abstract and the methods need to be implemented by the derived classes.
  4. Extensibility and Maintainability:

    • Keeping these declarations explicit allows for greater flexibility in the evolution of the code. If non-virtual or private methods need to be added in the future, the code structure will already be prepared for these changes, making maintenance easier.
  5. Compliance with Coding Standards:

    • Many C++ coding standards recommend or require these explicit declarations. Following these standards in MQL5 helps maintain a high level of rigour and discipline in coding.

Although it's not strictly necessary to use public: , virtual and = 0; in MQL5 interfaces, choosing to include them brings benefits in terms of code clarity, consistency and maintainability. I hope this explanation helps clarify my design choice.

 
Jonathan Pereira #:

Thank you for your comment! I'm glad you liked the article. I'd like to clarify why I chose to use public: , virtual and = 0; in the interfaces, even though some of these elements may seem redundant:

  1. Code Clarity and Consistency:

    • In C++ and, by extension, in MQL5, make explicit public: , virtual and = 0; in interfaces improves readability and makes it clear that these methods are part of an interface that needs to be implemented. This is especially useful for developers who are more used to languages like Java, where these declarations are mandatory.
  2. Compatibility with C++:

    • MQL5 is based heavily on C++, where interfaces are defined with pure abstract classes. Explicitly using virtual e = 0; ensures that MQL5 code follows C++ best practices, making life easier for developers working with both languages.
  3. Implicit documentation:

    • Even if the members of an interface are implicitly public, declaring public: acts as a form of internal documentation, making it clear to anyone reading the code that these methods are publicly accessible. Virtual and = 0; reinforce that the class is abstract and the methods need to be implemented by the derived classes.
  4. Extensibility and Maintainability:

    • Keeping these declarations explicit allows for greater flexibility in the evolution of the code. If non-virtual or private methods need to be added in the future, the code structure will already be prepared for these changes, making maintenance easier.
  5. Compliance with Coding Standards:

    • Many C++ coding standards recommend or require these explicit declarations. Following these standards in MQL5 helps maintain a high level of rigour and discipline in coding.

Although it's not strictly necessary to use public: , virtual and = 0; in MQL5 interfaces, choosing to include them brings benefits in terms of code clarity, consistency and maintainability. I hope this explanation helps clarify my design choice.

I get you.  I do a lot of work in C++, so I get you.  Great work, great article and great response, thanks.

 

Thank bro so much.

 

Thank you very good article!!!

WebRequest is more powerful than ever. That's why we didn't analyze your code.

You can also do its research, for example, from indicators and not just from experts.