PHP, how to write high-quality code

WBOY
Release: 2016-09-27 14:18:11
Original
1217 people have browsed it

How to write excellent PHP code? Now I can basically achieve all my needs, but the code is very messy and the logic is not very good. How can I improve it?

Reply content:

How to write excellent PHP code? Now I can basically achieve all my needs, but the code is very messy and the logic is not very good. How can I improve it?

I think the word high quality should be quantified into indicators such as readability, scalability, security, coupling, operating efficiency, etc. Only in this way can it be followed in an orderly manner, and writing good code will not become a metaphysics.

  1. Readability: Personally, I think this is particularly important. Can your variable names and method names be clearly understood by others? Are the naming consistent and not lax?

  2. Extensibility: The vitality of an extensible program is powerful. High-quality code must have high scalability characteristics. PHP itself can be expanded. It has very good scalability and is very convenient. This is well known . The best editor vim also has unparalleled scalability.

  3. Security: This importance is self-evident. Without security, no matter how elegant the code is, it has no meaning.

  4. Coupling degree: The logic of the program should not be overly coupled, so that one move affects the whole body. Low coupling degree can make the code reusability very high.

  5. Operating efficiency: The best martial arts in the world can only be fast. An excellent algorithm can make the running efficiency of the program several orders of magnitude faster. Data structure + algorithm = program is still not an empty talk today.

To sum up, writing high-quality code is never easy. You can take off more wheels and see how others build it.

Want to see more excellent codes? I wonder if there are any books on this subject?

http://www.php-fig.org/psr/

  • Passed all tests: and requirements first

  • Eliminate duplication as much as possible: high cohesion and low coupling

  • Express as clearly as possible: readability

  • Fewer code elements: constants, variables, functions, classes, packages... all belong to code elements, reducing complexity

  • The importance of the above four principles decreases in order

The above is quoted from simple design principles

Core: high cohesion and loose coupling (single responsibility), external dependencies, entity versus abstract programming, abstraction is layering

Don’t use too many if statements in one method. If there are too many, just split it into several methods. Dismantle as many methods as possible that can be reused.

The most important prerequisite for writing high-quality code is that you need to know what "high-quality PHP code" is?

High-quality code Many github PHP projects can tell you, you just need to take the time to read. Of course, your reading here is not just to read it line by line, but what you think about this piece of code, whether its structural design is good or not, and whether the internal design ideas can be improved. By slowly thinking about yourself in this way, you will greatly improve yourself.
My recommendation is the yii2 framework, such as this simple logger design yii2/framework/log

You can take a look at <> to optimize code projects and logic
Strictly follow the PHP development coding standards

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template