Contents
How to Write Better Code (9 Easy Steps)
In this blog, I’m going to share with you some 9 steps to write a better code that I have learned with my own experiences. Since these tips are all based on my personal experiences, this article may solve your problem on how to become a good programmer for beginners. Before moving forward to the tips to code better it is important to understand “what is a good code?” When any person starts their journey in coding and programming, believe it or not, everyone starts with a code block that looks really horrible. The code looks scattered, out of place, and difficult to understand. It’s not a really big problem, every beginner starts with this kind of thing, but important is to improve every time and keep on improving every time we code anything. A good code is something that looks neat and clean, into the place, on the point, no any additional trash, and easily understandable. Not everyone can write good code. A good code only comes up with dedication towards coding and programming and after following the steps that are mentioned in this article. So, First of all, let’s see an example of good, neat, and clean code.
const game = () => {let pScore = 0;let cScore = 0;//Start the Gameconst startGame = () => {const playBtn = document.querySelector(“.intro button”);const introScreen = document.querySelector(“.intro”);const match = document.querySelector(“.match”);playBtn.addEventListener(“click”, () => {introScreen.classList.add(“fadeOut”);match.classList.add(“fadeIn”);});};//Play Matchconst playMatch = () => {const options = document.querySelectorAll(“.options button”);const playerHand = document.querySelector(“.player-hand”);const computerHand = document.querySelector(“.computer-hand”);const hands = document.querySelectorAll(“.hands img”);hands.forEach(hand => {hand.addEventListener(“animationend”, function() {this.style.animation = “”;});});
As you can see, this piece of code is of a simple rock-paper-scissors game coded out of javaScript. In this piece of code, you can see, comments are used, variables are also easy to understand, making this code easily understandable for everyone that reads the code and makes it neat and clean. Now let’s have a look at the 9 Steps to code better (effectively, easily, and beautifully).
1. Finalize your Idea.
It may sound a little stupid, but trust me, if you don’t have a clear idea of what exactly you want to make then you can never code better, and your code will be scattered, and it would be really bad. One of the biggest mistakes that many beginners make while coding a project is that they don’t have a clear idea of what exactly they want to make. They come up with an idea but don’t have a clear idea of how the final product should look like. Hence, resulting in a code that looks really bad and a program that really looks out of place and it can be easily be distinguish that it has been coded by a newbie. So, it is very much important that you finalize your project before starting to code. Now onwards, always remember that there should be a clear image of the final product in your mind.
2. Visualize your Project.
After you finalize your idea, it is very much important to visualize your project into paper or any computer program like Figma, Adobe XD, etc. so that you can start working on the project flawlessly and don’t get stuck at any point while working in a project. Since you visualize your project into paper or computer programs, you can have an idea that how your project’s final result will look and you can code effectively and also your code will be neat and clean you you follow according to your visualization of the project. So, always remember to visualize your project so as to write a code that’s better and easy to understand.
3. Take Inspiration from Others.
As a programmer and a coder if you think of building everything from scratch and not take inspiration from anyone’s design and idea then you can never progress in this field. The field of coding is all like this. If you don’t take inspiration from others and don’t follow the latest trend then you can’t sustain. And also to write a code better you should take inspiration from others. Taking inspiration isn’t in the sense that you can copy the code fully, it means to take their idea and add your own touch to it so it comes out unique and can be sold in the market easily.
4. Comfortable Coding Environment.
If you want to code better and you are coding in an environment that is full of disturbances and distractions, then I can guarantee you that you can’t write good code. If you wish of writing good code and you are coding while being in the bed, being in a noisy area then I bet you can’t code effectively. Hence, It is very much important to choose a coding place that has no distractions and disturbances. If you want to improve your productivity in coding and also any other field, you should do your work in a comfortable and peaceful environment so that you can be with yourself only and be creative and productive at the same time.
5. Choose your IDE wisely.
IDE is a computer program in which you develop your code hence it is called IDE (Integrated Development Environment). There are many IDEs out there in the market but it is very much important to choose an IDE wisely. While choosing your IDE you should choose the one which has maximum features and also has enough customization options. Because the IDE with a lot of features helps you improve productivity and customized environment help you work effectively. Hence, you can write good code. One of the best IDE for both beginners and professionals is VS Code, VS Code is developed by Microsoft and has many features and has a lot of customization features. If you want to extend features you can even get extensions in this IDE.
6. Others should understand your code.
One of the identities of a good and better code is that others reading your code should understand your logic and program without much difficulty. When your code is understandable to others, it is a good signal that you are a good programmer and coder. Hence, for beginners, it is very much important to write a code better that is easily understandable. Now let’s see some samples of good and bad code.
#include #include void main(){int a, b, c;scanf(“%d”, &a);scanf(“%d”, &b);c = a * b;printf(“%d”, c);getch();}#include #include void main(){int l, b, a;printf(“Input length “);scanf(“%d”, &l);printf(“Input Breadth “);scanf(“%d”, &b);a = l * b;printf(“Area of rectangle is %d”, a);getch();}Both of these code blocks are to find the area of a rectangle, but you can judge yourself which code is better. You can see that the first code is a little hard to understand what is the code about. Whereas in the second code there is the use of simple English language as well and variables are also relatable, which makes the bode easily understandable. Hence, if you want to write code better, you should make your code better understandable to others.7. Use Comments Properly.
While talking about writing code better and becoming a good programmer for beginners, we can never forget the point to use comments. Also, using comments doesn’t means using comment everywhere, even where it is not of use. A good programmer always uses comments properly, which is the distinguishing factor between a good and a bad programmer. Now let’s see some examples of good and bad programs.mybutton = document.getElementById(“myBtn”);window.onscroll = function() {scrollFunction()};function scrollFunction() {if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {mybutton.style.display = “block”;} else {mybutton.style.display = “none”;}}function topFunction() {document.body.scrollTop = 0;document.documentElement.scrollTop = 0;}//Get the button:mybutton = document.getElementById(“myBtn”);// When the user scrolls down 20px from the top of the document, show the buttonwindow.onscroll = function() {scrollFunction()};function scrollFunction() {if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {mybutton.style.display = “block”;} else {mybutton.style.display = “none”;}}// When the user clicks on the button, scroll to the top of the documentfunction topFunction() {document.body.scrollTop = 0; // For Safaridocument.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera}Now you can judge yourself which code is better. Here first code is without comments and the second code is using comments making it a better code. When you write code without comments, there may be a problem that even you also can’t understand your own code if you look at it after a while. So, to become a good programmer, you should always use comments.8. Format your code.
As a part of writing good code, I have added this point because after formatting your code, your code looks neat and clean, as proper spacing and format are maintained after formatting. It is not hard work to do. A formatted code is far better than a code that looks horrible. For formatting, you can use extensions like Beautify in VS Code to format your code. So, always remember to format your code the next time.9. Take Help from Others.
In the process of being a good programmer, you can never work without taking help from others. If you ever get stuck at any point while writing a code, don’t ever hesitate to ask for help from others. You can take help from youtube, stack overflow, and Github, etc. to increase productivity. This will help you code without stopping at any point, leading to an increase in productivity. Hence, it is necessary to take help from others if you ever get stuck at any point.These are some 9 steps to write code better than I have learned from my own personal experiences. If you follow these steps, you will definitely improve your skill, increase productivity, etc. Thanks for making it to the end.