Outline: How to improve your code
- 1: Use meaningful variable names
- 2: Your code should speak for itself
- 3: Write independent components that could be used elsewhere
- 4: Single Responsibility principle
- 5: Use the ternary operator wisely
- 6: Use DRY: “Don’t repeat yourself”
- 7: Use separate files for separate pieces of code
We all make mistakes all the time. From complete beginners to senior engineers with years of experience, everybody makes mistakes on a daily basis. The key difference between beginners and veterans is the type of mistakes they make. Experience is a driving factor in this. While senior developers also make mistakes, beginners tend to make a lot more, especially on things that can be learned over time, which experienced developers don’t make anymore.
In today’s post, we want to give you 7 tips to improve your code. Those tips might also be considered as programming best practices and are things that are especially helpful for newer developers, as most veterans already know and apply the things we will teach you in this article.
Make sure to subscribe to our Youtube-channel aswell! If you rather watch than read, check out our video about this topic:
Let’s get started!
Tip 1: Use meaningful variable names
The first tip we want to give you to improve your code is that you should always use meaningful variable names.
We know, everybody says this all the time, but we also know from experience that beginners often can’t see the necessity for using meaningful variable and function names.
There’s a really obvious reason for why you would want to always use meaningful names in your code. This reason is called readability and maintainability.
Readability means that your code can be easily understood and read by others and most importantly by yourself or your team that’s working on the project. When I first started learning I used to just give my variables and functions random names like food, animals or just random letters from the keyboard.
I also know many people that used to do this as a beginner as well and I also know many beginners that do this quite often.
In small projects it might not seem necessary to give each variable a well though of name and the temptation is great to just smash some keys and go with it.
Code readability is one of the most important skills to learn as a developer and the sooner you start doing this regularly, the faster you will improve as a programmer in general.
Code readability is also tightly tied with the maintainability of the code base. Imagine a large project, possibly in a big company like facebook or google. Such projects can easily have 10s of thousands of lines of code written by hundreds of people. Now imagine that every developer used their own hard to understand naming patterns and used random names for most of the variables.
This would be an absolute nightmare for everyone involved and the code would be almost impossible to maintain.
So, to prevent such things from happening, always use meaningful variable names and do yourself and all the other individuals on your team a big favor.
Tip 2: Your code should speak for itself
The next tip is deeply connected to code readability and maintainability. Your code should always speak for itself.
This means that anyone working on the project now or in the future can understand the code you wrote with ease and can make improvements to it.
To achieve this, you should avoid working with crazy nested structures, cramming multiple conditions into one line and much more.
The ideal code should speak for itself and won’t need any comments at all. Of course, this is an ideal and almost impossible to achieve for us, but clean and readable code comes with a nice side-effect.
It eliminates the need for excessive use of comments to describe the code structure and how it works. If every developer can understand what’s happening just by looking at the code itself, there is absolutely no need for massive amounts of comments in it.
We know that many programmers hate writing comments as it can be a daunting task, so by focusing on writing clean and readable code that speaks for itself, you can get rid of this exhausting activity.
Tip 3: Write independent components that could be used elsewhere
Another tip to improve your code is to write independent components that could be used elsewhere.
This means that you want to keep your functions, classes, components or whatever as generic as possible to enable their usage in other locations as well. For example, imagine that you need to fetch some information from a database.
Instead of just hardcoding everything into one function that solves your problem in this one location and then writing it all again in a different location, try to create a general-purpose function that deals with fetching all kinds of data from the database.
For example, you could pass the search parameters and the table name as parameters to the function instead of just hardcoding it. By doing so, you enable your function to do all kinds of different requests.
This was just a small example but imagine how powerful this technique could be with larger components. So, remember, one key aspect to improve your coding skills is to always try to write independent components that could be used elsewhere.
What tips help you the most to write clean code? Do you have any additions to our list? We’d love to read your comments about this.
Tip 4: Single responsibility principle
Tip number 4 on our list for writing better code is to always apply the single responsibility principle.
Every function you write should only have a single responsibility in your program. This not only enables better testing and prevents silly bugs, it also guarantees predictable outcomes.
Many beginners tend to write a function that handles all kinds of stuff. For example, they write a function which first makes a database request. Then takes that data and stores it somewhere else. It then also manipulates user data and changes the user interface.
Such functions are really common amongst unexperienced developers and can be really dangerous. They can cause all kinds of errors and bugs and make your program unpredictable. They also hinder readability and maintainability of your code, as it can be really difficult to work with such massive functions.
Therefore, our fourth tip for writing better code: always apply the single responsibility principle.
Tip 5: Use the ternary operator wisely
Coming up at spot number 5 is our tip to use the ternary operator wisely.
We know, the ternary operator is really cool and allows for fancy usage of conditions without having to rely on if statements. But a ternary is not always the best choice in terms of code readability and maintainability as it makes your whole code really messy and hard to understand.
Many beginners, including the younger version of myself, seem to use ternaries everywhere once they learn about it. Like already said, a ternary can be a cool thing but you should always use it wisely.
There is no point in using a ternary when you have to check for multiple conditions at the same time. Instead you should only use ternaries, when you only check for a single condition or when absolutely necessary.
For example, when you work with react and write some JSX code, there sometimes is no other way than using a ternary to check for conditions inside your markup. But besides such mandatory applications, only use the ternary operator wisely.
Tip 6: DRY: “Don’t Repeat Yourself”
Our next tip for writing better code is a well-known programming best practice. Of course, we had to include the DRY principle.
DRY stands for “Don’t repeat yourself”. This means that every piece of code must have a single place where it is written, for example in a function, and can then be called somewhere else. You want to use the DRY principle to avoid redundancy and reduce repetition in your code.
By applying this principle you only need to change something in one place and it will work everywhere else automatically.
A good example might be the work on an html document with css. Imagine that you have a page with several headings and paragraphs and you gave every single one of them a style attribute which sets their color to red.
Now imagine that you want to change the color of the text to blue. In this example you would have to change it at many locations manually in your code and if you forget one, your whole design would be messed up.
A better solution would be to apply the DRY principle and use a CSS class. You could create a class and give it to all the text elements in your document instead of directly using the style attribute. If you now want to change the color, you only have to change it once in the CSS file and don’t have the risk of missing something.
This was only a very basic example but you probably can see now why you should always use the DRY principle: “Don’t repeat yourself”.
Tip 7: Use separate files for separate pieces of code
The last tip for writing better code we want to give you today is to always use separate files for code that can be separated.
There is nothing worse than having to maintain massive code files with thousands of lines of code. It can be an absolute nightmare working on such files and unfortunately, many older projects, even in bigger companies have such terrifying files.
Much of the work then has to go into refactoring all the heavy files into smaller, better to maintain, separate files to allow for future enhancements.
To get rid of all this unnecessary daunting work, you should always split up your code into as many separate files as possible to allow for maximum efficiency in readability and maintainability.
These were our 7 tips to improve your code by applying industry best practices and avoiding silly mistakes.
If you apply all of the things we taught in this video, your code will drastically improve and your ability to write clean and readable code will tremendously increase.
We hope you enjoyed today’s video and learned something new. If you did, please leave a like and subscribe to our channel for more weekly videos.
Also make sure to comment down below if you have any questions or critique whatsoever.
We’re excited to see you again next week and until then, stay healthy and keep on learning.
For more tips check out this article.