In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
I believe many inexperienced people are at a loss about the 21 kinds of mistakes that are most easy to make in writing Java programs. Therefore, this paper summarizes the causes and solutions of the problems. Through this article, I hope you can solve this problem.
An example Analysis of 21 kinds of mistakes most easily made in writing Java programs
1.Duplicated Code
Code repetition is almost the most common odor. He is also one of the main targets of Refactoring. Code duplication often comes from the programming style of copy-and-paste. OAOO corresponding to him is an important symbol of a good system.
2.Long method
It is the legacy of traditional structure. A method should have the intention of being independent and should not put several intentions together.
3.Large Class
A big category is that you give too much responsibility to one class. The rule here is One Class One Responsibility.
4.Divergent Change
The rate of change of content in a class is different. Some states change once an hour, some change only once a few months, some states change for this reason, while others change for other reasons. Object-oriented abstraction is to separate the relatively constant from the relative change. Isolate one side of the problem from the other. This makes these relatively constant can be reused. Each aspect of the problem change can be reused separately. The coexistence of different changes makes reuse very difficult.
5.Shotgun Surgery
This is just the opposite of the above. Changes to one part of the system involve related changes in many other places. These states and behaviors with similar rates of change and content should usually be placed in the same class.
6.Feature Envy
The purpose of an object is to encapsulate states and behaviors that are closely related to those states. If the methods of one class frequently use the get method to access the state of other classes for calculation, then you should consider moving the behavior to the class with the largest number of states involved.
7.Data Clumps
Some data usually play in groups like children: they appear together in member variables of many classes, in the parameters of many methods, and perhaps they should form objects on their own.
8.Primitive Obsession
Object-oriented novices are usually used to using several primitive types of data to represent a concept. For ranges, for example, they use two numbers. For Money, they represent it as a floating point number. Because you do not use objects to express concepts that exist in the problem, this makes the code difficult to understand and makes it much more difficult to solve the problem. A good habit is to extend the primitive types that the language can provide, using small objects to represent scope, amount, conversion rate, zip code, and so on.
9.Switch Statement
Constant-based switch statements are the enemy of OO, and you should turn it into a subclass, state, or strategy.
10. Parallel Inheritance Hierarchies
The parallel inheritance hierarchy is a special case of shotgun surgery. Because when you change a class in one level, you have to change the parallel subclasses of another level at the same time.
11. Lazy Class
A class that doesn't work much. Class maintenance requires additional overhead, and if a class takes on too little responsibility, it should be eliminated.
12. 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.
13. Temporary Field
The properties of an object may only be meaningful in some cases. Such code will be difficult to understand. Specifically create an object to hold such an orphan property and move behaviors that are only related to him to the class. The most common is that a particular algorithm requires variables that are only useful for that algorithm.
14. Message Chain
The message chain occurs when a customer requests another object from one object, and then the customer requests another object from this other object, and then another object from this other object, and so on. At this point, you need to hide the assignment.
15. Middle Man
One of the basic features of an object is encapsulation, which you often do through dispatch. But you can't go too far, and if you find that more than half of the methods of a class interface are dispatching, you may need to remove the middleman.
16. Inappropriate Intimacy
Some classes are so close to each other that they spend too much time researching other people's private parts. For human beings, we may not be too prude, but we should let our class strictly abide by asceticism.
17. Alternative Classes with Different Interfaces
The method of doing the same thing has different functions signature, which are moved up the class level consistently until the protocol is consistent.
18. Incomplete Library Class
It is very difficult to build a good class library. A lot of our program work is based on class libraries. However, such a wide range of different goals put forward stringent requirements for library builders. Library builders are not everything. Sometimes we find that library classes cannot achieve 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 carry out Refactoring.
19. Data Class
Objects include status and behavior. If a class has only state and no behavior, then something must be wrong.
20. Refused Bequest
Superclasses pass down a lot of behaviors and states, and subclasses use only a small portion of them. This usually means that there is something wrong with your class level.
21. Comments
I 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 Refactoring.
After reading the above, have you mastered the methods of writing the 21 most common mistakes in Java programs? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.