I have been engaged in development for many years and have a feeling that it is a pleasant experience to view the source code of some open source projects, such as Spring, Apache Common, etc. The reasons are as follows: 1) the code quality is very high; 2) the naming is particularly standardized (this may be related to the English level of foreigners). Writing high-quality code is not an easy task. It requires years of practice and is a process of quantitative change to qualitative change. However, good naming only requires a good foundation in English grammar and self-awareness. This blog post will summarize several naming rules based on my development experience. These naming rules are purely personal usage habits and do not represent an ideal rule. They are listed here for everyone to discuss. 1. Avoid using meaningless English letters in naming
This is a common code snippet in many books that teach basic Java syntax. As teaching material, there is nothing wrong with writing it this way, but as real code writing, programmers must develop good habits and not use this meaningless naming method. "index" can be used here. 2. Avoid using pinyin, or even the first letter combination of pinyin
When I was doing code review, I encountered such naming countless times, which made people laugh and cry. 3. Use English, and use accurate English, both in spelling and grammar
4. The naming of the method needs to use "verb-object structure phrase" or "verb + predicative structure phrase" I have seen all kinds of strange method names, some using nouns, some even "noun + verb", and if the object is a collection of objects, it is best to use plural:
5. For common "add, delete, modify and check" methods, be careful when naming them:
6. Prefer lengthy method names to confusing abbreviations I once encountered a method to determine whether the payment account is the same as the receiving account. As a result, I saw a name like this:
7. If you are designing a business system, never use technical terms to name it. The company I used to work for once formulated a naming rule that interfaces must start with "I", data transfer objects must be suffixed with "DTO", data access objects must be suffixed with "DAO", and domain objects must be suffixed with "DO". The reason why I do not recommend this approach is that I hope that designers can guide developers from the beginning to consider issues from the perspective of "business" rather than "technology". Therefore, the interface does not have to start with "I", as long as its implementation class ends with "Impl" (Note: I think the interface is irrelevant to details and technology, but the implementation class is implementation-related, so there is nothing wrong with using technical terms). The data transfer object is nothing more than storing the information of an object, so you can use "**Info", such as CustomerInfo. The domain object itself is the core of the business, so it still appears with its real name, such as Account, Customer. As for "DAO", this word comes from the design pattern of J2ee. The author used "***Repository" in the previous project, which means "*** warehouse", such as AccountRepository. The word "Repository" comes from the concept of warehouse in Eric Evans' book "Domain-Driven Design". Eric Evans defines the concept of Repository as: a conceptual collection of domain objects. I think this name is very appropriate. It allows programmers to completely break away from technical thinking and think about problems from a business perspective. At this point, some people may refute: Don't excellent frameworks like Spring and Hibernate use "I" as the beginning of the interface and "DAO" to name data access objects? That's right! But don't ignore the semantic context. Spring and Hibernate frameworks are pure technical frameworks. The scenario I am talking about here is designing business systems. 8. Do not repeat the class name in member variables For example, many people like to use names such as accountId and accountNumber in the member variables of the Account object. In fact, this is not necessary. Think about it, member variables do not exist in isolation. If you reference accountId, it must be account.accountId. Using account.id is clear enough. "Don't do good things for small reasons, and don't do bad things for small reasons", "Details determine success or failure", there are too many famous sayings telling us to pay attention to details. A good programmer must have a solid foundation, and for the naming rules that are easy to master, why don't we do it now? |
<<: 14 top development communities frequented by foreign programmers
>>: Summary of new technologies in Android 6.0
As the temperature gradually drops, the ginkgo be...
Butterfly is one of the insects that everyone is ...
The operation system is divided into the operatio...
Huangdaozhu's public account traffic matrix m...
It makes sense to integrate IoT and mobile device...
Friends often ask what constitutes a high-quality...
Preface As cross-border e-commerce sellers enter ...
The game of mobile marketing does have many cleve...
[51CTO.com original article] Data Xiaobai started...
There have long been theories about who is more l...
Since its launch, Apple Maps has been criticized ...
Henan server rental cost table, the cost of renti...
As a topic of concern to everyone, height is the ...
“Why has the conversion rate decreased? I can’t f...