How to write high-quality and concise code, these 6 tips to remember

Writing clean code is not an easy task, it requires trying different techniques and practices.

As a developer, it's important to write clean code.

Lao Wang first listed some of the benefits of writing clean code, and then proposed 6 tips for writing clean code for developers to learn from.

The benefits of writing clean code

Let’s start by understanding some of the benefits of writing clean code. One of the main benefits is that clean code reduces the time spent reading and understanding the code. Messy code slows down any developer and makes the developer's job more difficult.

The more cluttered the code, the more time it takes developers to fully understand it so they can use it. And, if the code is too messy, the developer might decide to stop reading it and write it from scratch themselves.

1. Easier to start and continue a project

Let's illustrate this problem with a simple example. Let's say we go back to a previous project after a long time, maybe a client contacted us for another job during this time.

Now, imagine how bad and messy it would have been after seeing the code for the first time if clean code wasn't written. Also, you can see how difficult it is to start coding from where you left off.

So now more time has to be spent on the project because we need to understand the code that was written before. This could have been avoided by writing clean code from the start, but now there is a price to pay for it.

Also, the old code was so messy and bad that we might decide to start from scratch. Customers may not be happy when they hear the news.

Clean code, on the other hand, usually doesn't have this problem. Assuming the previous example is the opposite and the previous code was clean and elegant, how long would it take to understand it?

Maybe it only takes a few minutes of reading the code to understand how everything works, and we've probably started writing it for a while, so the effort spent in this case will be significantly less than the first case, and at the same time, the client won't be too care

This is the first benefit of writing clean code, and it applies not only to your own projects, but to the work of other developers as well. Clean code starts work faster, no one needs to spend hours studying the code, instead we can jump right into work.

2. Conducive to the training of new employees in the team

Another benefit of writing clean code, closely related to the first, is that it makes it easier and faster for new employees to use the code. Suppose we need to hire a developer, how long does it take to understand the code and learn to use it?

Of course it depends on the situation. If our code is messy and poorly written, it takes more time to learn the code. On the other hand, if the code is clean, readable, and simple to understand, she will be able to start her work faster.

Some might say it's not a problem because other developers can help her. Of course this is true, but help should only take a short time, two or three or a day or two, not two or three weeks.

So, the decision to hire another developer was made to speed up our work, not slow it down or spend more time helping her learn to use the code.

When we strive to write clean code, others will learn from us, and it will be easier to follow and write clean code. Of course, still need to set aside some time to help each new developer understand and understand the code.

Of course, I mean days, not weeks. Also, clean code will help the team bring in more developers and at the same time help them understand the code. Simply put, the simpler the code, the easier it is to explain and the fewer misunderstandings.

3. Easier to follow coding patterns

One thing to remember, understanding and learning how to use code is one thing. However, this is just the beginning, and we need to ensure that developers are willing to follow our coding patterns.

Of course, this is easier to achieve with clean code than messy code. This is important because teams not only want to write clean code, but maintain that pattern all the time, which also requires long-term thinking.

Also, what if developers don't follow current coding patterns? This problem usually resolves itself. Suppose there is a group of people working on the same code base and one of them starts to deviate from the standard style. The rest of the team will then push this developer to follow the standard. She'll take advice because she doesn't want to leave the team.

There is also a case where a developer convinces the rest of the team to adopt and follow their own coding patterns. It's certainly a good thing if the coding patterns the developers come up with are cleaner and lead to better results.

It's true that writing and maintaining clean code doesn't mean that any opportunity to improve it should be ignored, I think one should always maintain an improveable attitude to current practices and try to find opportunities for improvement.

So if a developer deviates from the current pattern, and the pattern is better, maybe it would be more appropriate for us to make a change. So one should not ignore other people's coding practices before trying other patterns, and we should continue to look for room for improvement.

Finally, the third case. The developer decided to neither adopt our practices nor convince us to adopt hers. Because she will decide to leave the team.

Skill

In addition to discussing the benefits of writing clean code, now is the time to learn some techniques to help us achieve this goal. As will be seen below, clean code contains and follows a few methods.

These methods make the code cleaner, readable, easier to understand, and simpler. Of course it is not necessary to implement all methods, implementing and following one or two measures is enough to bring about positive results.

1. Write readable code

It is true that the code written will be interpreted by the machine, however this does not mean that the readability and understandability of the code should be ignored, because in the future there will always be another person who will use the code we wrote.

Even if we make our code inaccessible to others, we ourselves may pick it up again in the future. For these reasons, it is in our own interest to make the code easy to read and understand. So how to achieve it?

The easiest way is to use spaces. The code can be minified before it is released, but it is not necessary to make the code look minified.

Instead, use indentation, line breaks, and blank lines to make code structure more readable. When a decision is made to go this way, the readability and understandability of the code is significantly improved. Then, looking at the code makes it easier to understand it

2. Use meaningful names for variables, functions, and methods

Let's take a look at the second tip that will help us write understandable and clean code. This tip is about meaningful names for variables, functions, and methods.

What does "meaningful" mean? Meaningful names are names that are descriptive enough, not just variables, functions, or methods that the writer can understand. In other words, the name itself should be defined in terms of what and how the variable, function or method is used.

Note, however, that using a descriptive name does not imply that you can use as many characters as you like. A good rule of thumb is to limit names to 3 or 4 words. If you need to use more than 4 words, this function or method needs to perform many tasks at the same time, so the code should be simplified and only necessary characters are used.

3. Make a function or method perform only one task

When starting to code, the functions and methods used can look like a swiss army knife that can handle almost anything, but it's hard to find a good name.

Also, hardly anyone other than the writer knows what a function is for and how to use it. Sometimes I run into these problems, and I'm not very good at it.

Then someone came up with a great suggestion: make each function or method perform only one task. This simple suggestion changed everything and helped me write clean code, at least cleaner than before.

Since then, other people can finally understand my code, or they don't need to spend as much time reading it as before, and the functions and methods have become better understandable. With the same input, the same output is always produced, and naming is much easier.

If you're having trouble finding descriptive names for functions and methods, or need to write lengthy instructions so others can use them, consider this suggestion to have each function or method perform only one task.

If your functions and methods look like a swiss army knife, execute this method, trust me, this versatility is not an advantage. This is a rather unfavorable situation that can backfire.

Side note: This practice of having each function or method perform only one task is called keeping functions pure. This coding practice comes from the concepts of functional programming.

4. Easier to follow coding patterns

No matter how hard you try to come up with meaningful names for variables, functions, and methods, the code is still not completely clear and understandable, and there are still some ideas that need to be explained.

The problem may not be that the code is hard to understand or use, rather, others may not understand why the function or method is implemented, or why it was created in a particular way. Meaning, the intent of creating a function or method is unclear.

Sometimes you may have to resort to non-traditional methods to solve a problem because there is not enough time to come up with a better solution, which is also difficult to explain in code.

So, commenting through the code can help with this and also help us explain to others why the method was written and why it was written in this particular way, so that others don't have to guess what those methods or functions are for.

What's more, when we use annotations to explain the code, others may find a better way to solve this problem and improve the code. This is possible because they know what the problem is and what the desired outcome is.

Without this information, it can be difficult for others to create better solutions, or they may not try because they don't see the need to modify the creator's own ideas.

So whenever you decide to use some quick fix or unconventional approach, use a comment explaining why you did it. It's better to explain it with a line or two of comments without anyone else guessing.

That said, we should only use comments when necessary and not explain bad code. Writing endless comments will not help turn bad code into clean code. If the code is bad, it should be fixed by improving the code rather than adding some instructions on how to use it. Writing clean code is more important.

5. Keep the code style consistent

When we have a particular coding style or style that we like, we use it everywhere all the time. But it's not a good idea to use different coding styles in different projects, and it's not possible to go back to the previous code very naturally, so it still takes some time to understand the coding style used in the project.

The best way is to choose a coding style and stick to it in all your projects. This way, it will be easier to go back to the old code from before. Of course, trying out new ways of coding is a good thing, it helps us find better ways to do our work. But it's better to try different coding styles on different experimental projects or exercises than on the main project.

Also, when we decide to do some experiments, we should try and practice many times, and we should take the time to do it thoroughly. It should only be implemented if you are truly convinced that you like it and are comfortable with it. And when you decide to do this, it's best to apply it to all projects. Yes, it will take time and it will also push us to think properly.

6. Check your code

This is the last trick. It's not just about writing clean code, it's about doing the final job, which is the need to maintain clean code. We should regularly review the code and try to improve it. Otherwise, without reviewing and updating our old code, it will quickly become outdated, just like our devices. If you want to keep your code in top shape, you need to update them regularly.

The same is true for codes that are used every day. The code will become more complex and messy, so you should avoid this and keep your code clean. The only way to achieve this is to periodically review our code. In other words, we need to keep it. For projects that no longer care about in the future, this may be unnecessary, but for others, maintaining the code is part of the job.

——————

Original: Lao Wang丨[Public Account: Hongmeng Developer Laowang] Huawei Certified Instructor / Tencent Certified Instructor / Hongmeng Development Pioneer

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324355754&siteId=291194637