Best Coding Practices

DRY, KISS, YAGNI

If these abbreviations (DRY, KISS, YAGNI) tell you nothing – I strongly invite you to read this post. Especially, if you are a programmer or you intend to be.

Don’t Repeat Yourself

Then you will avoid a flood of problems. 🙂

A solid programmer living in the world of object-oriented programming should even subconsciously try to always adapt to this principle. That’s why we have this programming paradigm – not to repeat the code unnecessarily. However, this does not only apply to the writing of the code itself – in the same way we can refer to e.g. the compilation process (if it requires repetitive actions). The rule allows for repetitive actions, as long as they are automated by the computer. However, when it comes to the code itself, the most common method of avoiding repetition is methods – a repeated fragment of the code can be separated into a method, and then call it from anywhere and many times. Similarly, it is recommended to use consts instead of so-called “magic numbers” in the code, which also increases the readability of the code by naming the values ​​used.

The advantages of using DRY are also saving time (we do not have to duplicate the code many times, just call the existing one) and easier handling of errors in the code (in the case of an error in a repeated fragment we have to make changes in each of its occurrences, e.g. in the case of an error in the called method – only once in the body of this method). DRY also forces interface design which makes the code more reusable. So little effort and so many benefits! 🙂

Keep It Simple, Stupid

This rule is rather self-explanatory. It was first created in the context of aircraft design, but it also applies to programming. The easiest way to apply it is to stop your desire to impress others with the complex structures you can use, and you will serve your future when you have to keep the code you write. It also facilitates work in development teams, where the use of the simplest version of working code facilitates its understanding by people other than its creator. The principle is directly related to the other two rules described in this post.

You Ain’t Gonna Need It

Since you don’t need it right now, you probably won’t need it at all. So do not add unnecessary functionalities to your code, until it becomes necessary. You can not only lose your time, but also the time of the programmers who will read this code after you. The use of YAGNI undoubtedly contributes to the maintenance of KISS, which also facilitates the use of DRY, so as you can see again – all principles are directly related.

Simple, but powerful!

All you have to do is remember these 3 simple rules and I’m sure that the quality of your code will improve significantly. They are simple and easy to remember, but the profit from their use is huge.

At the end, I would also add here one less formal rule – always try to leave the code in better condition than you found it. It really makes life easier, especially in the large projects with a complex business domain.

Leave a Reply