sequence

Recently, I was arranged by the leader to participate in codeReview for several times and found many problems. Since I was on another team, I didn’t pay much attention to business logic, focusing on code style, abstraction level, etc., and found a lot of problems.

I just happened to be reading a book called Clean Code, so I got some ideas on how to clean this up.

This series will be divided into three parts, from the pure theory, direction, and practice of the code clean way to give some personal insights.

Clean code

One of the last things a programmer wants to do is take over someone else’s code. This is partly because it is someone else’s logic, which takes a lot of work to understand from beginning to end, and partly because it is likely to be heir-code, which is stinky and long.

The first reason is good, with PRD, bite for a period of time or can bite down. But with ancestral code, it’s not so simple. Looking at smelly, long, illogical, abstract, and uncommented lines of code gives everyone a headache.

What is code

Specifying requirements in detail that a machine can perform is what programming does, and that specification is code.

In short, code is a way for us to translate ideas from our minds into machines that can read them through an agreed specification.

Just like a translator, good code can make the machine read better, with better performance, higher abstraction, and less repetition, while bad code, like machine translation, not only has a greater burden on the implementation of the code, but also has a great mental burden for the code to take over.

The importance of clean code

Lebron’s law: Later is never

In the beginning, every project is clean and tidy.

But as code complexity increases, so does code clutter. As the level of confusion increases, team productivity will continue to decline. When productivity declines, more people will be added to the project in the hope of improving productivity, but new people are not familiar with the whole, so the confusion will become more and more.

So to avoid this, we need to keep the code as clean as possible at all times.

But many times, project requirements are urgent, and completion may be the first goal, rather than clean and maintainable code. Then, as mentioned above, the code gets messier and more unmaintainable.

Creating chaos doesn’t help development, it just slows you down. To speed things up, you need to keep your code as clean as possible.

Therefore, it is necessary to maintain clean and uniform code at all times.

Let’s say you’re a doctor, and a patient asks you not to wash your hands before an operation, because it’s a waste of time, and obviously, that’s not reasonable.

How do we keep our code clean

The broken Window theory: undesirable phenomena in the environment, if allowed to exist, will induce people to imitate and even intensify

In order to keep our code clean, we need to spend some time optimizing for it.

Clean code is code that the author takes care of. Someone took the time to keep it simple and organized. They paid proper attention to the details. They cared.

In general, in addition to tools such as Linter for formalizing code, there are a few areas you need to focus on yourself:

  • The code logic is straightforward
  • Reduce dependencies for easy maintenance
  • Refine error handling according to layered strategies
  • Unit tests cover everything
  • Name it meaningfully (live up to the name)
  • Provide one way rather than multiple ways to do one thing (no duplicate code)

summary

In this article, from the importance of code cleanliness, how to keep code clean and other macro aspects, about some of the reasons why to keep code clean, next, we will from

  • named
  • function
  • annotation
  • Code format
  • Error handling
  • The border
  • Unit testing
  • Idea into the design
  • Gradually improve

And so on, specifically to introduce some key points of clean code.

Hope to read these three, to help you.