While learning programming and problem-solving, we also, as in many other industries, learn good practices related to our work. This was probably the same in your case. And although you probably know them by heart, let's make an appointment - who doesn’t forget about them sometimes in sheer of tasks and the upcoming deadlines? So, we have prepared 7 best tips for good programming practice that you can refer to from time to time as a reminder. Let’s start!
1. Be DRY or DIE
What does it actually mean? DRY is exactly “Don’t Repeat Yourself”, while DIE - Duplication Is Evil. In both cases, it is about the lack of repetition in the code - if a given part of it is repetitive, automate it! After all, that's what apps are made for - to simplify certain activities. So, why would we waste the time copying and pasting the same fragment of code over and over? Simply summarize them in a procedure or class with specific parameters.
Be DRY and stay safe!
2. Test Driven Development & Bahaviour Driven Development
The pace of life makes us try to improve various ongoing processes and act faster and faster. Unfortunately, often, instead of planning, how to perform tasks more efficiently and precisely, we omit some elements that seem to be less important to us. The same happens in the case of coding and insufficient testing of the created product, due to short lead times. But let's get one thing clear - so what if you write code quickly if it doesn't work properly?
Test Driven Development and Behaviour Driven Development , i.e. in abbreviations TDD and BDD are respectively practices , that require good code design, before the start of the operation and testing its actual behaviour. Acting in this way increases the verifiability of the code and facilitates the creation of accurate documentation.
Insufficient attention, paid to testing , can significantly extend the development process . Adjustments will have to be made over time, which you will either make individually or you will need support in doing so. However, a customer who receives such a product will not be satisfied. As a result, either he will never come back to you, or he will come back only with complaints.
3. Text length matters
Did you notice that long, compact blocks of text seem harder to read from the start and are more reluctant to read than well-formatted ones? The same is true for code . When writing it, try to keep it in an organized, readable form. It is said, that the best art the short lines, with a length up to max. 80 characters.
Just like in articles or books, also make sure that the paragraphs, in this case - sections, are properly divided. Each method or function should have its own block, which will allow you or the person who will work on it at a later time to find yourself in the code .
4. Avoid hard coding
... except for permanent elements, of course. This tip mainly concerns information connected with the configuration, such as passwords, which, for security reasons, should be easy to modify.
5. Single Source of Truth
SSOT is the practice of storing the final version of application data in one place. Specific references to detailed information are made only by adding the corresponding links. It is a principle adapted not only for the field of application development , but also for the creation of documents in other departments of the organization. That confirms its functionality and convenience.
6. Use descriptive names
Returning to the actions we take to reduce the time spent on various activities, let's also look at describing the names of the variables used. Ok, short names look good and more synthetic, but do you always remember what they exactly meant?
Until a few years ago, short names were required, among other things, due to the construction of interfaces. Today, nothing stands in the way of describing the created variables in an extensive and accurate manner. A moment more devoted to describing in detail while creating, is a lot of time saved in the future by you, or by others, on guessing "what the author meant".
7. Version control
All parts of a project that you create, should be under constant version control. Why? Thanks to the use of such a mechanism, cooperation between the involved devs will be facilitated. They will be able to operate on the same sets of files, as well as follow subsequent, updated versions of the code . In this area, your needs can be met by tools such as Git and GitHub, allowing not only the above-mentioned activities, but also regular saving of subsequent versions and creating backups.
Remember, you never know when a glitch will happen to you, and regular recording of your work progress can save you a lot of stress!
8. Maintain consistency
This rule applies to uniform naming, selected functions, and the order of structures. Of course, testing new solutions may turn out to be a bull's eye, but try not to overdo it with their number when working on one project. In this way determine the patterns that you will use or propose to create the same one for the entire team, with which you work. Your code will be more readable, and as a result, it will be easier for you to communicate in terms of advice or product development , and you will easily find any errors that have arisen.
So how many of them do you remember in your daily work?