[Reprinted] What is the code smell?

Source: Internet
Author: User
The Chinese Translation of code smell is generally" Code "Odor", or "code smell", is a hint of an error in the Code. developers can find the problem in the Code through this smell. Programming on the computer Community , Code smell represents any sign of bad things. It often indicates that the Code should be refactored or all the designs should be reviewed. This phrase appeared on wardswiki and was fabricated by Kent Beck. After the rise of refactoring, the usage of this phrase suddenly increased. It is often subjective to determine whether code smell exists, and there are differences with different languages, developers, and development theories. Experienced and knowledgeable developers have a "feeling" about excellent design, and they have reached a state called "unconsciouscompetence. That is to say, they don't need to think about it. By looking at the code or a piece of design, they can immediately have a "feeling" on the code quality of the project, so that they can have a rough judgment on the merits of the code design. However, you should note that code smell is just a "suggestion", not a "confirmation ". Calling something "code smell" is not an attack; it is just a reminder that developers need to further view the project design. Therefore, code smell is more intuitive and instinctive ". Code smell generally includes: 1. Repeated duplicated code is almost the most common odor. He is also one of the main goals of refactoring. Code duplication often comes from the copy-and-paste programming style. Oaoo is an important indicator of a good system. 2. Long method: it is a traditional structured "Legacy drug ". A method should have self-independent intentions. Do not put together a few intentions. I will describe in detail in my main category and long method article. 3. Large Class class is a class that you assign too many responsibilities. The rule here is one class one. 4. Divergent change the content change rate in a class is different. Some statuses change once an hour, while some changes only once a few months a year. Some statuses change for this reason, while others change for other reasons. Object-oriented abstraction is to isolate relatively unchanged and relative changes. Isolate one aspect of the problem change from the other. This makes these relatively unchanged items reusable. Each aspect of the problem change can be reused independently. The coexistence of such different changes makes reuse very difficult. 5. Shotgun surgery. Changes in one place of the system involve changes in many other places. These States and actions with similar change rates and changes should generally be placed in the same class. 6. The purpose of the feature envy object is to encapsulate the state and the behavior closely related to these States. If a class method frequently uses the get method to access the states of other classes for calculation, you should consider moving the behavior to the class with the largest number of States involved. 7. Data clumps some data is usually played in groups like a child: it appears together in many class member variables and in many method parameters ......, These data may be independent objects. 8. Beginner primitive obsession objects are usually used to representing a concept using several primitive types of data. For example, for a range, they use two numbers. For money, they are represented by a floating point number. Because you do not use objects to express the concepts that exist in the problem, this makes the code difficult to understand and the difficulty of solving the problem greatly increases. The good habit is that the Extended Language can provide the original type, and small objects are used to represent the range, amount, conversion rate, zip code, and so on. 9. Switch statement the constant-based switch statement is the enemy of OO. You should change it to a subclass, state, or strategy. 10. Parallel inheritance hierarchies the parallel inheritance level is a special case of shotgun surgery. Because when you change a class in a hierarchy, you must change the parallel subclass of another hierarchy at the same time. 11. Lazy class is a class that does not work much. Class maintenance requires additional overhead. If a class bears too few responsibilities, it should be eliminated. 12. Speculative generality a class implements functions and versatility that have never been used. Generally, the only user of such a class or method is test case. Do not hesitate to delete it. 13. The properties of an object in temporary field may only make sense in some cases. Such code is hard to understand. Create an object to hold such orphan attributes, and move the behavior associated with the object to this class. The most common is a specific Algorithm Some variables that are useful only for this algorithm are required. 14. message chain occurs when a customer requests another object to one object, then the customer requests another object to the other object, and then requests another object to the other object. In this case, you need to hide the assignment. 15. One of the basic features of the middle man object is encapsulation, which is often encapsulated by dispatching. However, this step cannot go too far. If you find that most of the Methods of A Class interface are being distributed, you may need to move to this intermediary. 16. Inappropriate intimacy some classes are too close to each other, and they spend too much time researching the private part of others. For humans, we may not be too serious, but we should keep our classes strictly under assilence. 17. Alternative classes with different interfaces there are different signature functions for methods that do the same thing, and they are moved to the class hierarchy until the protocols are consistent. 18. It is very difficult for incomplete library class to establish a good class library. A large number Program All work is implemented based on the class library. However, such broad and distinct goals impose demanding requirements on Database builders. Library builders are not omnipotent. Sometimes we will find that the library class cannot implement the functions we need. However, it is very difficult to directly modify the library class. At this time, we need to use various means for refactoring. 19. Data class objects include statuses and behaviors. If a class has only the status and no behavior, there must be something wrong. 20. The refused bequest superclass transmits many behaviors and States, and the subclass only uses a small part of them. This usually means that there is a problem with your class hierarchy. 21. Comments often think that writing a lot of comments is hard to understand your code. If it feels too much, you need refactoring.

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.