Let’s look at 5 awesome programming tips to make our code much better, more readable, and easier to understand to make life easier for our team. 😁
Before getting into the subject… why is it important to apply good practices when programming?
Most of us start in this sector in our homes in a self-taught way or following formal training. We start to write our first lines of code just for ourselves or to be corrected, so… “what does it matter if it is more or less beautiful if it is mine and just for my eyes…”. Careful! 🚩
But when we start programming at a professional level we will usually work in development teams and we will not be alone, so we have to try to follow some standards and good practices for several reasons. First of all, so that you are able in the future to understand your own code. Second, so that your colleagues also understand it, because remember: you are no longer alone in your programmer’s cave!
Let’s get started!
Double-check: What happens if we don’t apply these tips? 🤔
If we do not apply them every time we adapt the project based on the needs of the users, it will be much more difficult to keep adding functionalities and improvements for ourselves as well as for our current and future colleagues.
5 best practices to help you write better code
Tip #1: use indentation
In the following image we can see a code with several IF conditional statements and their corresponding ELSE, but without any indentation applied, which makes it very difficult to understand the logics applied in it.
Below, you can see the same piece of code but with this tip applied, which reads much easier and helps to understand the conditional sets better.
Tip #2: Descriptive variables
In this case a picture is worth a thousand words, let’s try to understand what we see in this code.
Can you tell what is contained in the variables varGen. Var1, 2 and 3? Not really. And now?
The variables that we can see in the first image of this tip do not describe at any point what we are going to use them for. In contrast to the second image in which we can:
- See what we have created them for.
- What we store in them.
- What we want to use them for in the future.
Storing a value Date: 05/03/2023 in a variable called “var1” is not the same as storing it in a variable called “date”.
Tip #3: Avoid code repetition🔁
This practice is more common than we imagine. Logically, the example we are going to show here is very simple and identifiable, but in other cases, it will be a little more difficult.
Do you see any part of the code repeated?
Yes, the two functions we have shown always need, before performing their action, to query the state of the database, but we don’t need to repeat this code as many times as needed.
What needs to be done is to create a common function to unify this repeated code and use it where needed.
Let’s show the improved code:
Tip #4: Comment on your code briefly
A very simple action to improve the understanding of the project code is to add short comments at the beginning of the functions to know what parameters the function will receive, what it will return if anything, and what logic it applies inside.
In this image we can see an example of what it is NOT briefly:
Don’t go crazy adding comments in the code 🤯. We can help the team by adding small comments explaining the functionality of the method, but we don’t need to explain absolutely everything, in this case, I would do the following:
I’ll let you in on a secret: if it’s in English, all the better. You will reach a lot more people.
Tip #5: Try it out
Last but not least, test your code as much as you can and more. You have at your disposal several tools and techniques that can help you to do so.
We have different types of tests:
- Functional Testing: verifies functions by emulating business scenarios, based on functional requirements.
- Integration Testing: ensures that software components or functions operate together.
- Unit testing: validates that each software unit works as expected. A unit is the smallest test component of an application.
- Stress testing: tests how much stress the system can withstand before it fails. Considered as a type of non-functional test.
They may seem very simple or we may think that none of us would fall into misapplying any of these tips we have mentioned, but it is much more common than we think.
Code quality tools 👀
Some tools help us in the challenge of having a clean and maintainable code such as:
- Sonarqube: is the most popular code quality and security analysis tool on the market. With the support of the open-source community, Sonarqube can currently analyze and produce results for more than 25 programming languages.
- CheckStyle: is an extension for static code analysis IDEs that are used in software development to check if Java source code complies with coding rules.
In summary, these 5 programming tips for writing code, apart from helping the whole team of a specific project and creating a good, bomb-proof product, are fundamentally positive for your development as a programmer and for becoming a little bit better every day.
Santander Digital Services is a Santander company, based in Madrid with more than 7,000 employees. We are working to move Santander towards a Digital Bank with branches.
Take a look at the job opportunities to work in tech here and Be Tech! with Santander 🚀
Follow us on LinkedIn and Instagram.