21 Kinds of Errors that Java programs make most easily

Source: Internet
Author: User

1.Duplicated Code

Code duplication is almost the most common odor. He is also one of the main goals of refactoring. Code duplication often comes from Copy-and-paste's programming style. The relative oaoo of him is an important sign of a good system.

2.Long method

It is a traditional structured "legacy". A method should have a self-independent intention, not to put several intentions together.

3.Large Class

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

4.Divergent Change

The content change rate within a class is different. Some states change one hours at a time, and some change only once a few months a year; some states vary for this reason, while others are changed for other reasons. Object-oriented abstraction is the separation of relatively constant and relative change. Isolate the aspect of the problem and the other. This makes these relatively immutable reusable. Each aspect of a problem change can be reused separately. The coexistence of these disparate changes makes reuse very difficult.

5.Shotgun Surgery

This is just the opposite of the above. Changes to one place in the system involve changes in many other places. These changes and changes in the content of similar state and behavior should usually be placed in the same class.

6.Feature Envy

Objects are encapsulated states and behaviors that are closely related to these states. If the methods of a class are frequently evaluated using the Get method to access the state of other classes, you should consider moving the behavior to the class that involves the highest number of states.

7.Data clumps

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

8.Primitive Obsession

An object-oriented novice is often accustomed to using several primitive types of data to represent a concept. For example, they would use two numbers for a range. For money, they are represented by a floating-point number. Because you are not using 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 increased. The good habit is to expand the language to provide the original type, with small objects to represent the 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 one of the classes in a hierarchy, you have to change the parallel subclass of the other level at the same time.

One. Lazy Class

A class that doesn't work much. Class maintenance requires additional overhead, and if a class assumes 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 code will be difficult to understand. Set up an object specifically to hold such an orphan attribute and move only the behavior associated with it to that class. The most common is that a particular algorithm requires certain variables that are only useful for the algorithm.

. Message Chain

The message chain occurs when one customer asks another object to one object, and then the customer asks another object for that other object, and then asks the other object for another object, so. At this point, you need to hide the assignment.

Middle man

One of the basic properties 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 the more than half method of a class interface is being dispatched, you may need to remove the middleman.

Inappropriate intimacy

Some classes are too close to each other, and they spend too much time building the private parts of others. For humanity, we may not be too fake, but we should let our class strictly abide by asceticism.

Alternative Classes with Different Interfaces

The methods of doing the same things have different function signature, and they are uniformly moved up to the class level until the protocol is consistent.

Incomplete Library Class

It's very difficult to build a good class library. Our extensive program work is based on the class library implementation. However, such a wide and different goal puts a demanding demand on the library builders. The Library Builder is not omnipotent either. Sometimes we find that library classes do not implement the functionality we need. It is very difficult to modify the library class directly. At this time it is necessary to use various means to refactoring. Job coordinates www.zhizuobiao.com

Data Class

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

Refused bequest

The superclass passes a lot of behavior and state, and the subclass only uses a small part of it. This usually means there is a problem with your class hierarchy.

Comments.

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

21 Kinds of Errors that Java programs make most easily

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.