Analysis of 21 error cases most easily made by Java program

Source: Internet
Author: User
Tags comments implement inheritance variables range relative repetition requires
Program | error

  1.Duplicated Code

Code repetition is almost the most common odor. He is also one of the main goals of refactoring. Code repetition often comes from the Copy-and-paste programming style. Relative to him oaoo is an important symbol of a good system.

2.Long method

It is the traditional structured "legacy". A method should have an intent to be independent, not to put together several intentions.

  3.Large Class

The big class is that you give too much responsibility to a class. The rule here is one Class a responsibility.

  4.Divergent Change

There is a different rate of content change within a class. Some states change one hours at a time, some are only once a year, some are changed for this reason, and others are changed for other reasons. Object-oriented abstraction is the isolation of relatively unchanging and relative changes. Isolate the problem on the one hand and the other on the other. This makes these relatively unchanging and reusable. Each aspect of the problem change can be reused separately. This coexistence of differences makes reuse very difficult.

  5.Shotgun Surgery

This is exactly the opposite of the above. Changes to one place in the system involve changes in many other places. The states and behaviors that are similar to the change rates and changes should usually be placed in the same class.

  6.Feature Envy

The purpose of an object is to encapsulate the state and the behavior that is closely related to those States. If the methods of a class frequently use the Get method to access the state of other classes to compute, you should consider moving the behavior to the class that has the highest number of States involved.

7.Data clumps

Some data are often played in groups like children: together in the member variables of many classes, appearing in the parameters of many methods, which should probably be formed independently of the object.

 8.Primitive Obsession

Object-oriented novices are often accustomed to using several raw types of data to represent a concept. For example, for a range, they would use two digits. For money, they are represented by a floating-point number. Because you do not use objects to express the existence of the concept of the problem, which makes the code difficult to understand, the difficulty of solving problems greatly increased. Good habit is to expand the language can provide the original type, with small objects to represent range, amount, conversion rate, postal code and so on.

 9.Switch Statement

A constant-based switch statement is the enemy of OO, and you should turn him into a subclass, state, or strategy.

Parallel Inheritance Hierarchies

The parallel inheritance hierarchy is a special case of shotgun surgery. Because when you change a class in a hierarchy, you have to change the parallel subclass of the other level at the same time.

 Lazy Class.

A class that doesn't work much. The maintenance of a class requires additional overhead, and if a class takes on too little responsibility, it should be eliminated.

Speculative generality

A class implements functionality and versatility that has never been used. Usually the only user of such a class or method is TestCase. Don't hesitate to delete it.

Temporary Field

The properties of an object may only be meaningful in some cases. Such a code would be difficult to understand. Specifically create an object to hold such orphan attributes, and move only the behavior associated with him to that class. The most common is that a particular algorithm requires certain variables that only the algorithm is useful for.

 Message Chain

A message chain occurs when a client requests another object to an object, and then the customer asks another object to the other object, and then requests another object to the other object. At this point, you need to hide the assignment.

  Middle Mans

One of the basic attributes of an object is encapsulation, and you often implement encapsulation by assigning it. But this step cannot go too far, and if you find that a half method of a class interface is being dispatched, you may need to remove the middleman.

  Inappropriate intimacy

Some classes are too intimate with each other, and they spend too much time in the private part of the brick. We may not be too formal for humans, but we should keep our classes strictly in accordance with asceticism.

 Alternative Classes with different interfaces

Methods that do the same thing have different function signature, and they are uniformly shifted to the class level until the agreement is consistent.

 Incomplete Library Class

It's very difficult to build a good class library. A lot of our program work is based on class library implementation. However, such a broad and diverse set of goals has made demanding demands on the library builders. Library builders are not omnipotent. Sometimes we find that the Library class does not implement the functionality we need. and directly to the Library class modification has very difficult. At this time it is necessary to use various means of refactoring.

  Data Class

Objects include state and behavior. If a class has only state and no behavior, then there must be something wrong.

 Refused bequest

A superclass is a lot of behavior and state, and subclasses just use a small part of it. This usually means there is a problem with your class level.

 Comments.

Often feel that to write a lot of comments means that your code is difficult to understand. If this feeling is too much, it means you need to refactoring.



Related Article

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.