Senior PHP Engineer thinking and coding skills necessary

Good developers often defined in terms of code quality. In the software industry, written in the code means test, update, expand or repair vulnerabilities save money. In this paper, I will show you some examples of real-life tips and ideas to help you clean up your logic code, refactor it, make it more robust and modular. These tips will not only help you reconstruct your old code, and give you some good advice on how to write simple code from now on.

 

What is reconstruction, why do we need it?

Reconstruction means to help us write the code simple methods and procedures. This is important for other possible reading, and do not need to expand how to edit other developers reuse our code for the.

 

The following content will show you some of reconfigurable logic code examples make it get better.

 

Do not refactor the code in a production environment without unit testing

My first piece of advice is never complete without unit testing began reconfigurable logic code. My reason is: you will have to function very difficult to repair the damage to the ending, because you can hardly point out that where damaged. So, if you want to reconstruct it, starting from the test. To ensure that you are ready to test the reconstructed part is covered. PHPUnit code coverage analysis.

 

You start from the bottom of the code refactoring

Look at the picture below. This is one I found from the Github real hotel management system project. This is an open source project, may want to closed-source project will be worse.

Example: Starting from the bottom Reconstruction

You see this code, where the three levels marked with red color. The bottom should be if / else enclosed, stating that in the first if conditions. Typically, the bottom is concentrated in a single logic processing, reconstruction easier.

 

Make your way shorter, break them down into smaller methods or profile / DB table

Perhaps here we can refine it to below as a private method:

Make your way shorter

 

The next point is to upload depth parameters and load view. Now, let's look at the rest of the reconstruction after the add () method. It has become more concise, easy to read, easy to test.

Example: First, the bottom Reconstruction

 

if affirmed stick with braces

Most programming languages ​​support a single line if affirmed, because it is relatively simple, so some developers on such use, but not so easy to read, and likely to cause a problem, because you can break a blank line conditions caused a crash. The following two examples of different look:

Example: Using braces

 

Do not use magic magic number or string:

The next example, if you notice more than 250 rooms, will return an error message. Here, 250 is considered to be a magic number. If you do not write this developer, it is difficult to point out what the number represents.

Example: Magic numbers

 

To reconstruct this method, we can point out the number 250 represents the largest room. To replace the hard-coded, we can extract it to a variable $ maxAvailableRooms. Now other developers, it becomes more easily understood.

Example: Repair magic number

 

Do not use else stated, if you do not really need:

In the same availablerooms () function, if you notice that affirm where we can easily get rid else part, and logical consistency.

Example: Ignore else stated

 

Your name can be represented using methods, variables, and tested

In the latter example, you will find the hotel management system has two methods are "index ()" and "room_m ()". For me, I'm not quite sure what their purpose yes. I think it should be easy to understand if their names are able to describe themselves.

Example: a bad method name

 

Make full use of your programming language functions

Many developers will not use the full functionality of the programming language they use. Many features can save you time, but also can make your code more robust. Look at the following example, note how much easier to achieve the same results in less code that case, by using the type of tips.

 

Finally, I would like to provide some quick tips on better coding:

 

  • An array using the new [] replaces the old array ().

  • Unless we do not check the data type is very important, otherwise the alternative == === operator.

  • Public method to provide a short descriptive name is always a good idea. Private methods may use longer names, because they are more limited in scope.

  • The method of implementation of the interface only, for example, generic names add (), and methods of using a single class, for example, the addUser descriptive name () or addDocument ().

  • Delete unused methods from the class.

  • Use is / has prefix like boolean function return value: isAdmin ($ user), hasPermission ($ user).

  • Always use access modifiers in class methods and properties.

  • Interface contamination Note: The user can use only publicly available.

  • Public class method in position of the tissue at the top of the method.

  • Always apply the concept of responsibility in a single class.

Released 2395 original articles · won praise 53 · views 440 000 +

Guess you like

Origin blog.csdn.net/lxw1844912514/article/details/105084388