Logo

Computer Arena

8 Code Tips

8 tips for more readable code!
8 Code Tips

#software

#code

Legibility of code is one of the basic principles of programming that developers need to follow in order to be more professional. Apart from that, more readable codes make it easier to maintain and modify the project, as well as make the teamwork process easier.

In this article from Computer Arena Training website, we are going to talk about 2 training tips that will help you to have more readable codes.

1. Commenting and documenting

As you know, commenting does not cause any problems or changes in the final execution process of your project. As a result, in order for other developers who have access to your code to better understand it, it is best to follow the commenting process and write meaningful comments for each piece of code.

Of course, keep in mind that there is no need to write comments everywhere. For example, when you write a very simple block of code, you do not need to explain the writing because its improper use creates cluttered code and the so-called "crowded alki".

Apart from that, providing documentation for the code and the specific causes of the functions you create will have a huge impact on a better understanding of the code. A simple document can include a general description of the project, the tools used, the custom functions and commands used, and so on.

2. Persistent dents

Of course, you can write your code in non-linguistic languages ​​like Python without indentation or Indetetaion, and in most cases you can get the output without any problems. So why do you think indentations are so important?

Indentation allows you to create more readable code for yourself and others. By using this feature, you can define the boundaries of blocks and functions much more clearly and do not confuse yourself or your colleagues in the future.

It is also important to note that you should never try to use deep indentations. This will make the code illegible and frighten your programmer or audience. For example, pay attention to the following code and find out how sloppy and illegible it is:

function do_stuff() {
 
// ...
 
    if (is_writable($folder)) {
 
        if ($fp = fopen($file_path,'w')) {
 
            if ($stuff = get_some_stuff()) {
 
                if (fwrite($fp,$stuff)) {
 
                    // ...
 
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }
    } else {
        return false;
    }
}

Eliminating such a situation will certainly not be an easy task, but it is at this point that the line between a professional programmer and a novice programmer is drawn.

3. Block spacing

Always try to put a space between any blocks or pieces of code that you write that are not necessarily related to each other. You can do your job with a simple Enter.

4. Fixed registration

Some programming languages ​​follow a specific naming principle, but others do not. In this case, it is better to create a special method yourself and carry out your whole project accordingly. Be sure to keep in mind that you need to name and explain the method you used in the initial project documentation or comment.

There are currently only two very common methods for naming, one is camel-case mode and the other is the use of _ between different words of variables and functions. For example: mysql_string_escape

Another way to nominate is to use a combination of the two. In this way, you name your functions based on camleCase and use normal variables based on the same simple mode (Underscore).

5. Do not repeat yourself

The Don’t Repeat Yourself rule is a well-known rule in the programming world that is used to reduce duplicate code. When you have a function with a specific name and function, you do not need to define it again, the only thing that needs to be done in this case is to call it again and there is no need to repeat it.

So be sure to note that you have never used duplicate code snippets and learn the philosophy of reusing well.

6. Limit the number of characters in a line

An easy way to improve the process of reading a text (whether code or plain text) is to limit the number of characters or letters you put in a line to specific values. This happens most often when you are writing database queries. For example, look at the following code:

$query = "SELECT id, username, first_name, last_name, status FROM users LEFT JOIN user_posts USING(users.id, user_posts.user_id) WHERE post_id = '123'";

Using the above method, you can write the above code snippet as follows, which is much more readable and better:

$query = "SELECT id, username, first_name, last_name, status FROM users LEFT JOIN user_posts USING(users.id, user_posts.user_id) WHERE post_id = '123'";

7. Manage files and directories

If we look at it from a technical point of view, most of the time we can write an entire project in a single file. But never do this because it will be like a nightmare for yourself and others.

For example, consider a website whose entire JavaScript styles and code are contained in a single index.html file. it's horrible! No?!

Well, your simple way to do this is to separate each section from the other using different files and folders. By doing this, as your project gets bigger, you can better manage all aspects of it and increase the ability to store code.

8. Use short and even meaningless letters for temporary variables

It will often happen that you use a single variable in a loop block. In this case, you do not need to use a meaningful name because you are sure you will not use this variable later.

Doing so will cause someone later reading your code to see a variable with a meaningless name and realize that it is only a one-time command and belongs to a specific code snippet and range and does not need to be followed elsewhere. To return.

For example, see the variables i and j in the following code snippet:

// $i for loop counters
 
for ($i = 0; $i < 100; $i++) {
    // $j for the nested loop counters
    for ($j = 0; $j < 100; $j++) {
    }
 
}

Conclusion

In this article from Computer Arena website, we have reviewed 8 tips for writing code that are more readable, which can directly affect the optimization of your code. If you have a specific technique in your experience to do this, be sure to share it with us.

Suggested article
  • Go to the next article

Author

Hossein Taghipour

Written on the: October 31, 2021

Comments

All rights to the products and content of this site belong to Computer Arena and any copying of the content and products of the site is unauthorized and without our consent