Русский 中文 Español Deutsch 日本語 Português 한국어 Français Italiano Türkçe
Better Programmer (Part 01): You must stop doing these 5 things to become a successful MQL5 programmer

Better Programmer (Part 01): You must stop doing these 5 things to become a successful MQL5 programmer

MetaTrader 5Examples | 27 July 2021, 15:55
17 363 3
Omega J Msigwa
Omega J Msigwa

Introduction

"Man's mind, once stretched by a new idea, never regains its original dimensions."
— Oliver Wendell Holmes

In order for us to become Better Programmers we not only need to learn code 24/7 but also we need to learn about the best practices, if not to learn how to learn coding at all. Learning how to learn code along with the best practices to apply is very crucial to everyone who needs to have a successful coding career. By knowing the hows and whys, we become aware of what we are doing—this increases our ability to remember and helps us improve our code in general.

The following ways are the proven methods that can change your programming experience and they way you do it, almost overnight, if you attentively apply everything that you are about to read in this article.


01: Stop the blind copy and paste habits

"The noblest pleasure is the joy of understanding." 
— Leonardo Da Vinci

This is the biggest habit of newbies and sometimes even experienced programmer do that. I remember when I first began programming in 2019, I was most of times on the forum looking for that "Code that works". I did not pay much attention to what was explained whenever I did a search on a certain issue; all I was looking for was that comment when someone replied like "Thank you it now works" or any other comment which people said it was helpful (especially the person who asked the question the first place in the forum)… No need to tell what I did once I found that piece of code that works!!!

Its embarrassing now when I realize that I once declared that Standard Library Chart Does not work

  


Now I know what did not work during that time—it was me because even the code itself was copied from somewhere that I don't remember. My point here is as follows:

Make sure you understand what you are doing. Don't blindly copy and paste code from the forum just to get stuff done and to fix things quickly

Most of times the way a lot of experienced developers answer people in the forum is hard for people with this habit to keep up with because they do answer by only providing a link to MQL5 documentation, about a certain topic, not to mention automatic features on this website that provide related links to a specific issue. The reason behind this is that most of problems are caused by not understanding the fundamentals and basics.

ALWAYS: Search for understanding not a solution!

Once you understand the fundamentals, coding becomes natural and automatic.

Copy and Paste habits not only will hurt your reputation but also cause you more problems than fix.

02: Give up hacking problems

Before you want to fix problem in your code make sure you seek to know why you have the problem. Knowing this will help you ask better questions in the forum rather than dragging down your code waiting for a code in exchange to fix the problem that has been caused by you not fully knowing what and why is that you are coding. For example, me on the image above: first of all I copied the code somewhere else hoping to change the chart template by that, not fully knowing and understanding the standard library chart—that's why I ended up saying the Library is not working.

Once you stumble upon a problem or an error, make sure you fully know the library and the code you have that's not working. There are several ways to do that on MQL5, the two main of them being

  • MQL5 documentation
  • Forum

MQL5 documentation:

Lucky for us we have the whole documentation at our finger tips and there is no need to be online on search engines. Professional Programmers know this but for noobs here is where and how to access it very quickly: Open MetaEditor, then go to Help section and click on the dropdown option MQl5 Reference and there you have it.

DocumentationAccess

MQL5 Forum:

This is the best place to learn and hear what you haven't seen through your own code is by sharing what troubles you have with other developers. But do not make the same mistake I did, DON'T COPY!

 "It is the path of Craftsmanship that leads to true understanding and Mastery."

Noobs are in what I call NOOBS CYCLE

Noobs Cycle

03: Give up perfectionism

"Done is better than perfect."
— Popular idea in Silicon Valley

I can't tell you how much and how long this stopped me from sharing stuff. I was always looking for the holy grail so that I can share with other users on this website. It took me time to realize that if I have something to share then I should share it and not be stopped by my own thinking.

Which is better?

To make one indicator, Expert Advisor, Library or piece of code in the next several years, or to be prolific and to make hundreds of imperfect results in a year?

Write good-enough code for your fellow developers or customers, not perfect code for yourself. You will always have time and opportunity to improve code that needs to be improved; in other cases you will save time by shipping code faster.

You can see this applies in almost everything programmers can think of, every day:

  • Our mobile Apps gets updated 
  • Our computers and phones operating systems and software get updated
  • Even Programming languages such as MQL5, Python and others, get improved day after day 

See, if even our Programming  Languages and Frameworks are getting updated, then you probably know that improving over time isn't a bad thing.

On MQL5.com this applies pretty much everywhere—you can think of updating programs in the Market.

Versions Update

On the Forum, you can change the Forum post, delete or just Update your reply and even your own comment how crazy is that !! , haha

04: Avoid Writing Smart Code

"Debugging is as twice as hard as writing code in the first place. Therefore, if you write code as cleverly as possible, you are by definition not smart enough to debug it." 
— Brian Kernighan

Code that follows best practices learned from the documentation is already smart bro !! 

Write good, clean and simple code that's easy to read and understand. No one will benefit from that smart code including you three months later. It is extremely difficult to work with the so called smart code by a lot of noobs (Freelance developers can tell). Have you ever met code from EA modifying job, or from the CodeBase that was extremely complicated for reasons you don't quite figure out? Don't get me wrong, I'm neither dissing anybody nor encouraging easy code for complex task; all I'm saying is that there is a way to make sure even complicated code can be easy to work with (I think that's what creativity is all about). Clean code can help.

Here are some of the tips for writing clean code:

  • Style it 
  • Make it consistent
  • Comment it
  • Keep in it DRY 

Style it :

I'll just use a function to count position by its type as an example

int CountPosByType(ENUM_POSITION_TYPE type){
 int counter = 0; 
  for (int i=PositionsTotal(); i>=0; i--)
     if (m_position.SelectByIndex(i))
     if (m_position.Magic()==MagicNumber && m_position.Symbol()==Symbol() && m_position.PositionType()==type)
      { counter++; }
  return counter;
}

The function is pretty easy to understand but it doesn't follow the above clean code tips, so, I'll first style it. Doing this is simple—just click on the blue button that looks like hair comb in your MetaEditor

mql5 code syler

This is what our code looks like after we style it:

int CountPosByType(ENUM_POSITION_TYPE type)
  {
   int counter = 0;
   for(int i=PositionsTotal(); i>=0; i--)
      if(m_position.SelectByIndex(i))
         if(m_position.Magic()==MagicNumber && m_position.Symbol()==Symbol() && m_position.PositionType()==type)
            { 
                counter++; 
            }
   return counter;
  }

See, just by applying styling our code has changed from difficult to read to readable. That's not even changing our code, because we have to take the first step to writing clean code and that is making it readable because once you can read it, you can change it --Period.

Make it Consistent:

I can't tell you how much I ended up creating too many variables for pretty much the same work on different function levels, so why not create a global variable? I would also encourage you to use easy to work with and easy to remember, consistent function names that clearly explain what the function is all about, so that you can remember even 6 months from now. This will speed up your work delivery time 3X from where it was since you won't have to start from scratch creating a function that's already in your previous EA, while you can just copy it from your previous works.

For the name of the function let's make it:

int CountPositionsByType(ENUM_POSITION_TYPE type)

Since we are Consistent about our code we can just go to search and set the find in files option       findinFiles


From there you can search for your function inside all the EA's and Indicators and this simplifies the work. Next time we want to create the function, we can just copy it and re-use it again in other EAs.

The best way to write consistent code is to Use OOP (Object Oriented Programming).

Consistent Code is easy to use, while debugging need to be done only in one place

Comment it:

Even things that noobs call small like writing comments in your code can make a huge difference on your productivity. Not only it will make code readable but also comments can be a reminder on what, for example, a variable is all about.

Now let's define the counter again this time with Comment explaining what it's all about:

int counter = 0; //Variable to store the number of positions

The next time we want to access the counter it will be like this:

mql5 code Intellisense

The great thing about commenting code in MetaEditor is that the comment will act as a reminder to what a variable or piece of code is all about. This thing is very useful when having too many variables that maybe easily forgotten on what they are all about.

Keep It dry:

OOP can help a lot when it comes to keeping the code DRY (Don't Repeat Yourself). Also be careful about having different functions that can pretty much do the same work. For example, you don't need to have a function that checks if a position exists by its type when you already have a function to count positions by their type.

05: Give up writing shortest possible code

"Readability first, then everything else."

Now we can see developers think they are writing good code if they are using the fewest possible lines or fewest characters in a line. This is not wise either.

Code written will be read dozens if not hundreds of times so strive to write readable code regardless of its length.

I can relate this to developers being lazy sometimes (I've been that) we feel to implement few things just to get the job done on Freelance or something.

Consider providing easy to use inputs on your EA. Write the code to handle all exceptions and alert the user what's wrong, such as alerting the user if some input is set to zero, in which case the EA may not work properly without it.

Here is what I'm telling you,

   Error codes, trade return codes, compilation errors and exceptions in the system are for you to know, not for users. So, consider writing code to provide alerts, comments on the chart, or whatever; but make sure the user only gets to see what he is supposed to and what action needs to be taken to make things right.

I know it feels boring to implement them all from scratch, but there is always a better way you can find out! You may keep them somewhere in the include files for including it or for copy and paste use—you can find a way.


Conclusion

The good thing about good practices is that they are hard to follow and develop, but once developed they will follow you for the rest of your career.

 Thanks for reading!


Last comments | Go to discussion (3)
Ted Samuel
Ted Samuel | 30 Jul 2021 at 13:03
You're awesome Omega. Thank you for sharing this excellent value freely. This kind of action makes this a better community and I believe what goes around comes around. Wishing you good things!!
Omega J Msigwa
Omega J Msigwa | 30 Jul 2021 at 14:05
Thanks
Raphael Parker
Raphael Parker | 1 Aug 2021 at 23:16

thanks for the valuable pointers.

Graphics in DoEasy library (Part 78): Animation principles in the library. Image slicing Graphics in DoEasy library (Part 78): Animation principles in the library. Image slicing
In this article, I will define the animation principles to be used in some parts of the library. I will also develop a class for copying a part of the image and pasting it to a specified form object location while preserving and restoring the part of the form background the image is to be superimposed on.
Graphics in DoEasy library (Part 77): Shadow object class Graphics in DoEasy library (Part 77): Shadow object class
In this article, I will create a separate class for the shadow object, which is a descendant of the graphical element object, as well as add the ability to fill the object background with a gradient fill.
Better Programmer (Part 02): Stop doing these 5 things to become a successful MQL5 programmer Better Programmer (Part 02): Stop doing these 5 things to become a successful MQL5 programmer
This is the must read article for anyone wanting to improve their programming career. This article series is aimed at making you the best programmer you can possibly be, no matter how experienced you are. The discussed ideas work for MQL5 programming newbies as well as professionals.
Graphics in DoEasy library (Part 76): Form object and predefined color themes Graphics in DoEasy library (Part 76): Form object and predefined color themes
In this article, I will describe the concept of building various library GUI design themes, create the Form object, which is a descendant of the graphical element class object, and prepare data for creating shadows of the library graphical objects, as well as for further development of the functionality.