


Programmers Joint Development Network Ten Commandments of Programmers Programming
1.- DRY: Don't repeat yourself.
DRY is the simplest rule and the easiest to understand. But it may also be the most difficult to apply (because to do this, we need to make considerable efforts in generic design, which is not an easy task). It means that when we find some similar code in two or more places, we need to abstract their commonalities to form a unique new method, and change the code in the existing places to make them use some Call this new method with the appropriate parameters.
DRY This rule may be the most common rule in programming. So far, no programmer should have any objection to this rule. However, we can find that some programs forget this rule when writing unit tests: Let us imagine that when you change several interfaces of a class, if you do not use DRY, then those who pass a call The unit test procedures of the interface of the system class need to be changed manually. For example: If the many test cases of your unit test do not use a standard common method of constructing a class, but each test case constructs an instance of the class by itself, then when the constructor of the class is changed, you need to modify it. How many test cases are there? This is the consequences of not using the DRY rule.
2.- Short methods.
At least, we have three good reasons for programmers to write short methods.
The code will become easier to read.
Code will become easier to reuse (short methods can reduce the degree of coupling between codes)
Code will become easier to test.
3.- Good naming convention
Using a good unified naming convention can make your program easier to read and maintain. When the name of a class, a function, or a variable reaches the point where it can be "literally meaningless" If we do this, we can have fewer documents and less communication. The article "Some things about naming design in programming" can give you some tips.
4.- Give each class the correct responsibility
One class, one responsibility. For such rules, you can refer to the SOLID rules of the class. But what we emphasize here is not a single responsibility, but a correct responsibility. If you have a class called Customer, we should not let this class have a sales method. We can only let this class have methods that are most directly related to Customer.
5.- Organize the code
There are two levels of organizing the code.
Physical layer organization: No matter what directory, package or namespace structure you use, you need to organize your classes in a standard way so that they can be easily found. This is a physical code organization.
Logical layer organization: The so-called logical layer mainly means that if we connect and organize two classes or methods with different functions through a certain specification. The main concern here is the interface between program modules. This is the architecture of program modules that we often see.
6.- Create a large number of unit tests
Unit testing is the place closest to BUGs and the place with the lowest cost to modify BUGs. It is also the place that determines the success or failure of the quality of the entire software. Therefore, whenever possible, you should write more and better unit test cases, so that when you make corresponding code changes in the future, you can easily know whether your code changes affect other units.
7.- Refactor your code frequently
Software development is a continuous process of discovery so that your code can keep up with the latest changes in actual requirements. Therefore, we need to frequently refactor our code to keep up with such changes. Of course, refactoring is risky, not all refactorings are successful, and not we can refactor the code at any time. The following are two prerequisites for refactoring your code to avoid introducing more bugs or making already bad code worse.
There are tons of unit tests to test. As mentioned before, refactoring requires a large number of unit tests for assurance and testing.
Every refactoring should not be big, use bits and pieces of small refactorings instead of large refactorings. There are too many times when we initially plan to refactor 2000 lines of code, and after 3 hours, we abandon the plan and revert the code to the original version. Therefore, what we recommend is that refactoring is best accumulated from bit by bit.
8.- Program comments are evil
This one must be full of controversy. Most programmers think that program comments are very good. Yes, that’s right, program comments are very good in theory. However, in actual programming, the comments written by programmers are very poor (the programmer's expression ability is very problematic), which has led to program comments becoming the embodiment of all evil, and also caused us to have trouble reading the program. Most of the time, we don't read the comments but read the code directly. So, here, we are not encouraging not to write comments, but - if your comments are not good enough, then you might as well spend more important time refactoring your code to make your code More readable, clearer, and better than comments.
9.- Focus on the interface, not the implementation
This is the most classic rule.The interface focuses on "What" is the abstraction, and the implementation focuses on "How" is the details. The interface is equivalent to a contract, and the actual details are equivalent to the operation and implementation of this contract. Operations can be very flexible, but contracts need to be relatively stable and unchanging. If an interface is not designed well and requires frequent changes, then we can imagine the consequences in this generation. This will definitely be a very costly thing. Therefore, in software development and debugging, the interface is the top priority, not the implementation. However, our programmers always focus on implementation details, so their partial code is very good, but the overall software design is relatively poor. This requires our more attention.
10.- Code Review Mechanism
Everyone will make mistakes. The probability of one person making a mistake is very high. The probability of two people making a mistake will be smaller. With more people, the probability of making a mistake will become smaller and smaller. Because when there are more people, they can look at a thing from different perspectives. Although this may lead to ineffective arguments, compared with the maintenance cost of problems after the software product is released, this cost is quite worth it. Therefore, this is why we need to let different people reivew the code. The code review mechanism is not only the most effective mechanism for discovering problems, but also a mechanism for knowledge sharing. Of course, for Code Review, there are several basic principles below:
The reviewer’s ability must be greater than or equal to the code author’s ability, otherwise, code review becomes a kind of training for novices.
Also, in order for reviewers to be truly responsible, rather than just doing a perfunctory review, we need to make reviewers primarily responsible for the code they reviewed, rather than the authors of the code.
In addition, a good code review should not be when the code is completed, but iterative code review during the process of code writing. As a good practice, code reviews need to be done continuously every few days, regardless of whether the code is completed or not.
The above has introduced the Programmers Joint Development Network and the Ten Commandments of Programmer Programming, including the content of the Programmers Joint Development Network. I hope it will be helpful to friends who are interested in PHP tutorials.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.
