MQL5 Comments… Improve Your Coding

In this article you will learn the benefits of using MQL5 Comments. Comments are not necessary to develop tools, however they can provide significant improvements.

Learning MetaTrader MQL5 Comments

What are MQL5 Comments?

When working coding and software development in general, writing clean and efficient code is essential.

One of the best ways to have clear source code is to use comments.

MQL5 Comments are non-executable lines of text that help explain your code’s logic.

Comments for MQL5 are also explained in the official mql5.com website, briefly in this page.

Comments are ignored by the compiler, meaning the will not be part of the final “executable” file. However, they provide valuable context for you and other developers who may work on the code later.

MQL5 supports two types of comments:

  • Single-line comments: Use // to comment everything following on the same line.
  • Multi-line comments: Use /* */ to comment across multiple lines.

Here some examples

Understanding Comments Through Analogy

If you want to use some analogy to better understand the concept of comments, these might give you an idea

Post-it Notes on a Document: Just like you might stick a Post-it on a printed report saying, “This section explains the budget breakdown,” comments stick helpful notes on your code without changing the content itself.

Recipe Tips in a Cookbook: A recipe might tell you to “let the dough rest for 30 minutes,” and a note next to it might say, “This makes the bread fluffier.” The recipe works without the tip, but the note helps you understand why that step matters — just like comments explain why certain code exists.

Labels on Moving Boxes: Imagine packing up for a move. A box labeled “Kitchen – Fragile Plates” helps you know what’s inside without opening it. Comments do the same — they tell you what a piece of code does without running it.

Why Are MQL5 Comments Important?

Despite not being a requirement for a program to run correctly, comments in MQL5 coding provide several benefits:

  1. Improved Readability: Comments make your code easier to understand, especially when working on complex algorithms or indicators.
  2. Easier Maintenance: Clear comments help you and others revisit and update code without confusion.
  3. Better Collaboration: When working in teams, comments provide crucial context and minimize the learning curve for new developers.

Best Practices for Using Comments in MQL5

There is no right or wrong way of using comments, but definitely there are better ways than others.

These are just some simple recommended guidelines:

  • Be Clear and Concise: Explain why you’re doing something, not just what you’re doing.
  • Avoid Obvious Comments: Don’t state what’s apparent from the code itself.
  • Keep Comments Updated: Outdated comments can be misleading.
  • Use Consistent Style: Stick to a format that’s easy to follow.

Real Example of Comments in MQL5

Earlier you saw an example to understand the syntax of comments.

Here you can see a snippet of code that is more realistic, showing how comments can be used to make the code more understandable.

Personal Reflection

Commenting code can be very boring and time consuming at times.

Many developers don’t spend much time, if any at all, on commenting code, I myself am guilty of that at times.

However, I also recognize that there is great value in integrating comments into your code, especially when complex and or long.

If you are serious about developing your own tools, I suggest to get into the habit of adding comments into your code, you’ll thank yourself later.

Conclusion

In this quick article you learned

  • what are comments in MQL5
  • how to add them into your code
  • the benefits of having comments
  • good practices

Whether you are building a personal indicator or expert advisors, or any other program, I recommend you to use comments during the development in MQL5 language.

I hope this was useful to you.

Check our tools if anything that can help you further, or learn more about MQL5 Basics.

For feedback or support please Contact Us.

Donchian Channel Indicator For MT4 And MT5

Metatrader Program Types

Leave a Comment