Home >Backend Development >PHP Tutorial >Five Good Habits of PHP Programming (1)_PHP Tutorial
Depending on the specific situation, average developers are often 10%~20% less efficient than excellent developers. Good developers are more efficient because they have extensive experience and good programming habits. Bad programming habits will affect efficiency. This article helps you become a better programmer by demonstrating some good programming habits.
These good programming habits not only increase efficiency, but also allow you to write code that is easy to maintain throughout the life cycle of your application. The code you write may require a lot of maintenance; application maintenance is a significant expense. Developing good programming habits can improve design quality (such as modularity), making the code easier to understand and therefore easier to maintain, while also reducing maintenance costs.
Bad programming habits will cause code defects, making it difficult to maintain and modify, and are likely to introduce other defects when modifying. Here are 5 good programming habits that can help PHP code avoid these pitfalls:
◆Use good naming.
◆Divide into smaller parts.
◆ Add comments to the code.
◆ Handle error conditions.
◆Do not use copy and paste.
These habits are described in detail below:
Use good naming
Using good naming is the most important programming habit because it is descriptive The names make 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 uncommented, it will greatly facilitate future changes if it is easy to understand. The goal of this habit is to make the code you write as easy to read and understand as a book.
Bad Habit: Vague or Meaningless Names
The code in Listing 1 contains variable names that are too short, illegible abbreviations, and method names that cannot Reflects the functionality of this method. If the method name gives the impression that it is supposed to do one thing, but in fact it does something else, this will cause serious problems because it will be misleading.
List 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 practice. The new method names are highly descriptive and reflect the purpose of the method. Likewise, the changed variable names are more descriptive. The only variable that remains the shortest is $i, which in this listing is a loop variable. Although many people frown upon using names that are too short, it is acceptable (and even beneficial) to use them in loop variables because it clearly indicates the function 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 large conditions into a method, and then name it with a name that describes the condition method. This technique can improve the readability of the code and can make the condition concrete so that it can be extracted and even reused. It's also easy to update methods if conditions change. Because a method has a meaningful name, it reflects the purpose of the code and makes the code easier to read.
1