What are the basic rules you need to know when coding?

  • LELEU Guillaume
  • 09 Mar 2023

In this article, we will explore some of the main rules to follow when writing code. Whether you are a novice or an experienced developer, it is important to follow certain practices so that your code is readable, efficient and easy to maintain. We will cover topics such as naming variables, structuring your code, using comments, and handling errors. By following these rules, you can increase your productivity by avoiding costly errors and allowing other developers to quickly understand your code.

Why comment on your code ?

Commenting on your code is not to be neglected, in fact it is crucial for the future and your collaboration with other developers. Comments are a kind of notes that provide additional information about how your code works. They help to determine why you wrote certain parts of your code, how they are connected to other parts of your program and most importantly how they work. 

Without your comments, your code can become complicated or impossible for other developers to understand, as they may have a different way of coding. Comments can also be used to challenge a developer on a common project. Let's say you want to update a function later on you can very well indicate this in your code so that other developers don't touch it.

 

Why name your variables properly ?

Naming your variables well is crucial for your program, especially if it is large. It makes your code more readable, understandable and easy to update. When you create variables, think of them as descriptive labels that explain their function and use in the program. 

By using this method, you can quickly understand the purpose of each of your variables. This makes it easier, as with comments, for other developers working with you to understand your code.

 

Why is indentation important ?

Indentation consists of visually shifting lines of code to the right. This improves the readability and understanding of your code. By using it consistently you can easily visualize the structure of your code and determine where each block of your code begins and ends. 

Beware, indentation is sometimes mandatory in some languages like Python, if you don't do it you will have an error in your program. Indentation standards are often used in collaborative projects to ensure that all developers work to the same standards.

 

How to avoid repetition in your code ?

Firstly we can use functions which are sequences of codes that can be called as many times as we want in our program. 

Secondly we can use loops which are very useful in case you need to perform the same operation on an array of data or a list.  

Finally, use inheritance and polymorphism because if you have classes that share common features, you can create a parent class that contains these features and inherit them into the child classes. This avoids duplication of code and makes maintenance easier.

 

Why is testing your code important ?

Testing your code is the basis of programming. First of all, the most important thing is not to provide a site full of bugs to your customer, it would be a big loss of confidence and money for you. 

However, it also allows you to improve the quality of your code, indeed, the tests allow to detect errors in the code (syntax, logic, programming errors) and can be corrected before it becomes a serious problem. Imagine if you don't test your payment system on your website? The user could find a flaw and get your goods for 0€ !

 

That's what we could say about the basic rules when you do code. Of course there can be more! Don't hesitate to tell us in comments

Comments

No comments yet! Why don't you be the first?
Add a comment