PHP: how to write high-quality code

Source: Internet
Author: User
Tags coding standards php coding standards
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.

  1. Readability: I personally think this is especially important. can your variable name and method name be well known.

  2. 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 EditorvimIt also has invincible scalability.

  3. Security: it is self-evident that there is no security and elegant code makes no sense.

  4. 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.

  5. 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 = programToday 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.