How to write excellent PHP code? Now, I can implement almost all the requirements, but the code is messy and the logic is not very good. how can I improve it? How to write excellent PHP code? Now, I can implement almost all the requirements, but the code is messy and the logic is not very good. how can I improve it?
Reply content:
How to write excellent PHP code? Now, I can implement almost all the requirements, but the code is messy and the logic is not very good. how can I improve it?
I think the word "high quality" should be quantified into readability, scalability, security, coupling, running efficiency, and other indicators. in this way, we can follow the rules in order and will not turn the written code into a mystery.
Readability: I personally think this is especially important. can your variable name and method name be well known.
Scalability: the vitality of scalable programs is powerful, and high-quality code must be highly scalable. php itself can be extended, with excellent scalability and convenience, which is well known. best Editorvim
It also has invincible scalability.
Security: it is self-evident that there is no security and elegant code makes no sense.
Coupling degree: the logic of the program should not be overly coupled, so that the entire body can be moved. The low coupling degree can make the code highly reusable.
Operational efficiency: the world's martial arts, only fast. an excellent algorithm can make the program run more efficiently several orders of magnitude.Data structure + Algorithm = program
Today is not an empty talk.
To sum up, writing high-quality code is never easy. you can remove more wheels and see how others make them.
Look at the good code? Do you have any books in this area?
Http://www.php-fig.org/psr/
Pass All Tests: and the requirement is
Eliminate duplication as much as possible: high cohesion and low coupling
Express as clearly as possible: readability
Less code elements: constants, variables, functions, classes, packages ...... All belong to the code element, reducing complexity
The importance of the above four principles is reduced in turn
The above references are simple design principles.
Core: high cohesion and loose coupling (single responsibility), external dependency, entity-to-abstract programming, abstraction is layered
A method should not have too many if statements. if there are too many if statements, it should be split into several methods for use. Try to remove the reusable methods.
The most important prerequisite for writing high-quality code is that you need to know what is "high-quality php code "?
Many github php projects with high-quality code can tell you that you only need to spend time reading. Of course, your reading on this side is not good as long as you have read this line, but what do you think about this code? its structure design is good and good, how can the internal design idea be improved. In this way, self-thinking will increase greatly for yourself.
My recommendation is the yii2 framework. for example, this simple logger design yii2/framework/log
You can see < <代码大全2> > Optimize code projects and logic
Strictly abide by PHP coding standards