Bad taste of code [2]

Source: Internet
Author: User

Bad taste of code [2]

Total returned directory

4. Long Parameter List (too Long Parameter column)

Too long parameter columns are hard to understand. Too many parameters may cause inconsistency and are not easy to use.

1. Generally, if there are more than three parameters, you can consider using objects. Encapsulate the parameters required by the function in a new class and use this class as a parameter.

2. You can also useParamsKeyword to reduce repeated parameters.

You just need to pass the function enough to get what you need from it.

V. Divergent Change)

In layman's terms, a class often changes in different directions for different reasons (a class is affected by multiple changes ).

In this case, we need to find out all the changes caused by a specific reason and then extract them into another class.

All modifications to an external change should only occur in a single class, And all content in this class should reflect this change.

6. Shotgun Surgery (elastic bullet changes)

This is the opposite of Divergent Change. When a change occurs, many small changes must be made within many different classes (one change causes multiple classes to be modified accordingly ).

Put all the code to be modified into the same class. If no suitable class can place the code for the moment, create one.

Shotgun Surgery and Divergent Change you need to organize and refactor the code in a timely manner, so that "external changes" and "classes to be modified" tend to correspond one by one.

VII. Feature Envy (Attachment complex)

The main bad taste is that a function uses more class attributes than its own class attributes. In other words, a function calls almost half a dozen value functions from another object to calculate a value.

The therapy is also obvious, that is, to extract this part of code into a separate function, and then move the function to where it should be.

Of course, not all situations are that simple. A function often uses several classes of functions. So where is it? The principle is to determine which class has the data most used by this function, and then put this function and that data together.

Package data and data operations

8. Data Clumps)

The two classes have the same fields and many function signatures have the same parameters.

Find out where the data appears in the form of fields and extract them into an independent object. If you want to reduce parameters, consider replacing them with an object.

Data that is always tied together should have their own objects

To Be Continued...

 

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.