21 of the easiest mistakes to write Java programs

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

1. Duplicatedcode

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

's programming style. Relative to him Oaoo is an important sign of a good system (see my Duplicatedcode: http://www.erptao.org/download.php?op=viewsdownload&sid=6).

2. Longmethod

It is the traditional structured "legacy". A method should have the intention of independence, do not put a few intentions together, my "big class and Long method" in the article is described in detail.

3. Largeclass

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

4. Divergentchange

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. Shotgunsurgery

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. Featureenvy

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. Dataclumps

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

8. Primitiveobsession

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. Switchstatement

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

10. Parallelinheritancehierarchies

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

11. Lazyclass

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.

12. Speculativegenerality

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.

13. Temporaryfield

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.

14. Messagechain

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.

15. Middleman

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 middle

People.

16. Inappropriateintimacy

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.

17. Alternativeclasseswithdifferentinterfaces

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

18. Incompletelibraryclass

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.

19. DataClass

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

20. Refusedbequest

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.

21. 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.