The "scheming" design in the code

Junior engineers are often derided as code-movers, copying chunks of code and making minor business adjustments themselves. This is a phase that every programmer has to go through, so there's nothing uncomfortable about it. Even if you just enter the line, you can't write the code, but you can write the code.

Writing code on a whim is a senior engineer's job, and we won't discuss it here. As an ideal code farmers, we must be able to write flowing code. Here are a few tips for writing quality code.

1. One thing rule

The One Thing Principle, where a file only does one thing, a class only does one thing, a function only does one thing, or even a line of code only does one thing.

A case to share: I used to make a catering system, the business process is very complex, we use a small front end (service number)+ large background structure to achieve, the front end with VUE framework. The application has been online operation, one day the customer feed to change the demand, order function to add content, business content I will not talk about, very complex, the use of intelligent analysis, application restaurant past business data automatically calculate the content of the order. To change the content, originally very simple, I estimate also an hour to fix, can send a small version, the result has not been done for a long time.

I was very strange, went to the engineer to learn about the situation, the engineer Rory told me a lot of verbdy, I didn't know what he was talking about, directly looked at the code, found that he generated the order and view the order details in the same VUE, editing and viewing judgment everywhere, I confused myself.

This is obviously a problem, a file to do two functions, at the beginning may save trouble, but you have to pray that the customer does not change the demand, or you will even want to die behind the heart have.

2. Coding specification

Now a lot of IDE come with code specifications, there are also a lot of large enterprises on the net code specifications, Ali, Huawei. If the company has a code specification, it is best to code according to the company's code specification. If the company does not, it is recommended to find a coding specification, do not need to implement all, all are not carried out.

Here's a tip. Pick ten or twenty of the most important coding conventions you can find and implement them. Comments, naming conventions, and case conventions are essential so that you can write consistent code. Otherwise, the code copied from Zhang San uses the Hungarian nomenclatura printEmployeePaychecks(); The code copied from Li Si uses the underscore naming method print_employee_paychecks() and your code won't look good.

3. Extract tool class library

Some commonly used, such as string processing, date and time processing, graphics image processing, file processing, etc., extracted into a tool class library, with your tool class library rich, you write code will be more and more simple, the development speed will be faster and faster. And the code is pretty clean. It could have been a dozen lines of code, but now it's just a function call. The code is also easy to maintain.

4. Don't duplicate code

We directly copy the code is the fastest, so a lot of engineers have written a piece of code, and then when they use it, they are used to copy this section, change the value, the function becomes.

This is a very bad habit, if this code is the business code, it is more trouble, the need is to change from time to time, adjust business is normal, three changes have a day, even if the business code copied everywhere, at first you remember a few places have changed, changed much, behind may start a less, less back again to buy more and more. This is the rumored area where fixing a bug creates more bugs.

So when you press Ctrl +C, think, is it possible to make a function? Normal is fine, and your code will be of much higher quality.