Put it in PHP, there are some additional places, such as keyword case sensitivity and syntax sugar usage (array () and. I have previously compiled the SRS and found tools such as php-cs-fixer. 1. unified coding specifications.
The encoding specification is simply described as follows:
Line Feed
Space
Variable name
Put it in PHP, there are some additional places, such as keyword case sensitivity and syntax sugar usage (array () and. I have previously compiled the DSRs and found tools such as php-cs-fixer. These are important methods to standardize the code. There are unified standards, and it is not difficult to form unified coding constraints in concert with the inspection of tools.
There is no specification. different people, even the same person, may be very casual in terms of spaces, line breaks, and naming. Once the code is long, the entire file looks messy.
Typical examples include:The combination of if and else can write countless styles, such:
For example, the naming of variable functions leads to an endless stream of mashups:
The advantages and disadvantages of various writing methods are not discussed here, but the style must be consistent and not mixed.
2. good coding habitsIf you have carefully read the SRS standard, you may also notice that some areas are actually not covered by the standard. For example, when a super-long expression line breaks and how to indent it.
The constraints of coding habits are involved here.
For example, the problem of method chain calling, such as the encapsulation of some database queries:
Db-> select ('id')-> where ('A', 1)-> groupBy ('A')-> orderBy ('id', 'desc ') -> result (); # In this case, we recommend that you set a condition row and keep it indented. $ result = $ this-> db-> select ('id ') -> where ('A', 1)-> groupBy ('A')-> orderBy ('id', 'desc')-> result ();There is also an array definition, which is written when some array member strings are long:
3. find the optimal writing methodIn the process of writing code, the best writing method and coding habits are not the same thing. What we will talk about here is how to follow PHP's language features or framework features and give full play to the language and framework capabilities to reduce redundancy.
For example, when obtaining parameters passed by the front-end, you can often see the following code:
What's more, some frameworks encapsulate the parameters passed by the front end, such as $ this-> request-> data ['param']. if you use isset or array_key_exists to judge, the entire statement for obtaining parameters will become very long.
In some cases, when using the ternary operator, do you need to pay attention to it? : It can be used in combination.
In fact, we should try to encapsulate this method to prevent the same variable from appearing in a statement multiple times. To assign the default value, you can check whether the framework is provided by encapsulation or by force type conversion.
Another case is nested conditions and loops. For example, when extracting a field from an array or processing the value of a field, using array_map and reference (&) can save a lot of work. However, pay attention to the last position of the array pointer when using it.
When returning results based on conditions, return should be used properly. There is a reasonable abstraction and encapsulation.
4. review your own codeIn addition to the issues mentioned above in daily development. There is also post-event work.
I believe that many people may have a better way of writing their previous code. Over time, you will always have more experience and ideas. Occasionally review your code is also a summary of the past, and may have a new perception.
5. promote it to your teammatesIn team projects, the cooperation of teammates plays a decisive role in the entire code specification. When one person in a team does not comply with the rules, and the code needs to be modified everywhere, all constraints will soon be broken.
Only with unified standards and good execution can this work be completed.