Ten prohibitions on programmer programming

Source: Internet
Author: User
1.-DRY: Dontrepeatyourself. DRY is the simplest rule and the easiest to understand. But it may also be the most difficult to be applied (because to do so, 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, "> <LINKhref =" http: // w

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 be applied (because to do so, we need to make considerable efforts in generic design, which is not an easy task ). It means that when we discover similar code in two or more places, we need to abstract their commonalities to form a unique new method, and change the existing code so that they can call this new method with some appropriate parameters.

The DRY rule may be the most common rule in programming. So far, no programmer has any objection to this rule. However, we can find that some programs forget this rule when writing unit tests: Let's look at it. When you change several interfaces of a class, if you do not use DRY, the unit test programs that call the interface of a series of instance classes must be manually modified. For example, if many test cases of your unit test do not use a common method to construct classes, but each test case constructs its own class instance, then, how many test cases do you need to modify when the class constructor is changed. This is the consequence of not using the DRY law.

2.-short method.
At least, we have three good reasons to ask programmers to write down 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)
The code will become easier to test.
3.-good naming rules
Using a good unified naming convention can make your program easier to read and maintain. When a class, a function, when the name of a variable reaches the realm of "looking at the meaning of life", we can have less documents and less communication. The article "naming design in programming" can give you some tips.

4.-assign correct responsibilities to each class
For a class and a responsibility, refer to the SOLID rule of the class. However, what we emphasize here is not a single responsibility, but a correct responsibility. If you have a class named Customer, we should not have the sales method for this class. We can only make this class have the most direct relationship with Customer.

5.-Organize the code
The code is organized in two layers.

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.