Home Backend Development PHP Tutorial Programmers Joint Development Network Ten Commandments of Programmers Programming

Programmers Joint Development Network Ten Commandments of Programmers Programming

Jul 29, 2016 am 08:40 AM

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.

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

See all articles