Five good habits of PHP programming (1 ). Generally, developers are 10% less efficient than excellent developers ~ 20%. Excellent developers are more efficient because they have rich experience and good performance. generally, developers are 10% less efficient than excellent developers ~ 20%. Excellent developers are more efficient because they have rich experience and good programming habits. Poor programming habits will affect efficiency. This article demonstrates some good programming habits to help you become a better programmer.
These good programming habits not only improve efficiency, but also allow you to write code that is easy to maintain throughout the application lifecycle. The compiled code may require a lot of maintenance; application maintenance is a huge expense. Developing good programming habits can improve the design quality (such as Modularization), so that the code is easier to understand. Therefore, maintenance is easier and maintenance costs are also reduced.
Poor programming habits may cause code defects, making it difficult to maintain and modify, and may introduce other defects during modification. Five good programming habits can help PHP code avoid these defects:
◆ Use a good name.
◆ Smaller part.
◆ Add comments to the code.
◆ Handle error conditions.
◆ Do not use copy and paste.
The following describes these habits in detail:
Use good naming
Good naming is the most important programming habit, because a descriptive name makes the code easier to read and understand. Whether the code is easy to understand depends on whether it can be maintained in the future. Even if the code is not annotated, it is easy to understand and can be easily changed in the future. The goal of this habit is to make your code as easy to read and understand as a book.
Bad habits: vague or meaningless names
The code in listing 1 contains too short variable names and unidentifiable acronyms, and the method name cannot reflect the function of the method. If the method name gives people the feeling that it should do this thing, but in reality it does another thing, it will bring serious problems because it will mislead people.
Listing 1. bad habits: vague or meaningless names
<?phpfunction getNBDay($d){ switch($d) { case 5: case 6: case 7: return 1; default: return ($d + 1); }}$day = 5;$nextDay = getNBDay($day);echo ("Next day is: " . $nextDay . "n");?> |
Good habits: descriptive and concise names
The code in listing 2 demonstrates good programming habits. The new method name is descriptive and reflects the purpose of the method. Similarly, the changed variable name is more descriptive. The only variable to keep the shortest is $ I. in this list, it is a circular variable. Although many people do not agree to use too short names, it is acceptable (or even good) to use them in loop variables because it clearly indicates the functions of the code.
Listing 2. good habits: descriptive and concise names
<?phpdefine ('MONDAY', 1);define ('TUESDAY', 2);define ('WEDNESDAY', 3);define ('THURSDAY', 4);define ('FRIDAY', 5);define ('SATURDAY', 6);define ('SUNDAY', 7);/* * * @param $dayOfWeek * @return int Day of week, with 1 being Monday and so on. */function findNextBusinessDay($dayOfWeek){ $nextBusinessDay = $dayOfWeek; switch($dayOfWeek) { case FRIDAY: case SATURDAY: case SUNDAY: $nextBusinessDay = MONDAY; break; default: $nextBusinessDay += 1; break; } return $nextBusinessDay;}$day = FRIDAY;$nextBusDay = findNextBusinessDay($day);echo ("Next day is:" . $nextBusDay . "n");?> |
We encourage you to split a large condition into a method and use the naming method that can describe the condition. This technique can improve the readability of the code and embody conditions so that the code can be extracted or reused. If conditions change, the update method is also easy. Because a method has a meaningful name, it can reflect the purpose of the code and make the code easier to read.
1
Listen 10% ~ 20%. Excellent developers are more efficient, because they have rich experience and good...