[Reprinted] Reading Notes on restructuring-improving the design of existing code

Source: Internet
Author: User

Reposted from: http://www.cnblogs.com/karoc/archive/2011/09/16/2179125.html#2497969. Thanks to the original author.

 

Bad taste

Features

Situation and Handling Method

Target

Duplicate code

1. repeated expressions
2. Different algorithms do the same thing
3. Similar code

Two Functions of the same class have the same expression.

Repeated code extraction as a method

The same expression only appears in one method of a class for other methods to call.

Sibling class contains the same expression

Repeated code extraction as a method
Upgrade Method to parent class

Irrelevant classes contain the same code

Extracted as an independent class for calling

Too long Function

1. There are comments before the code
2. conditional expressions
3. Loop

 

Extraction Method

Each method only does one thing. The method must be well defined and named.

Too large class

1. There are too many instance variables in a class
2. There is too much code in a class

High correlation between some fields

Related Fields and methods are extracted as classes.

Each class is responsible for a group of internally associated tasks.

Some fields and methods are only used by some instances.

These fields and methods are moved to the subclass.

Too long parameter Column

1. The parameter column is too long.
2. Frequent parameter column changes

You can use other methods to obtain this parameter.

Allow the parameter recipient to obtain the parameter.

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

Several attributes of the same object as parameters

Use the entire object as a parameter without deteriorating Dependencies

The called function uses many attributes of another object.

Move the method to this object

Some data lacks owner objects

First create an object

Divergent changes

A class is affected by various changes.

Classes often change in different directions for different reasons.

Extract all changes caused by specific reasons into a new class

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

Flexible Modification

One change causes the modification of multiple classes.

Some changes require small modifications in many different classes.

Put all the code to be modified into the same class

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

Attachment Complex

A function uses more class attributes than its own class attributes.

A function calls almost half-dozen value functions from another object.

Extract the attachment code as a separate method and move it to another object.

Package data and data operations

Data dashboard

The data used at the same time is not organized as a class.
1. The same fields in the two classes
2. Same parameters in many functions

 

Extract fields as classes, and then reduce the parameters in the function signature.

Data that is always tied together should have their own objects

Basic Type paranoia

Excessive use of Basic Types

Basic type fields that are always put together

Extraction class

Convert an existing data value to an object

The parameter columns have basic types.

Extract parameter objects

The array contains different objects and data needs to be selected from the array.

Replace arrays with objects

The basic data is the type code.

Use class replacement type code

Type Code with conditional expression

Replace type codes with inheritance classes

Switch statement

The same switch and case statements are distributed in different places.

Switch selected based on the Type Code

Replace switch with Polymorphism

Avoid making the same changes everywhere

Switch exists in a single function

Replace parameters with explicit methods

Parallel Inheritance System

1. When adding a subclass to a class, you must add a subclass to another class.
2. the prefix of an inherited system class name is the same as that of another inherited system class name.

 

Instances in one inheritance system reference instances in another inheritance system, and then migrate members

Avoid making the same changes everywhere

Redundancy

Class idle

There is no big difference between the parent class and the subclass.

Integrate them into one

 

A class does not do much.

Move all the members of this class to another class and delete it.

Talking about the future

 

An abstract class does not have much effect.

Merge Parent and Child classes

 

Unnecessary Delegation

Move all the members of this class to another class and delete it.

Some parameters of the function are not used.

Remove Parameters

Function names have redundant abstraction meanings

Rename function name

The function is called only by the test method.

Delete together with test code

Confusing temporary Fields

1. An Instance field is set only in some situations
2. Some instance fields are set only for complex algorithms that do not pass parameters to a function.

 

Extract separate classes and encapsulate related Code

 

Over-coupling message chain

A long string of getthis or temporary variables

The customer class acquires another object through one delegate class.

Hide Delegation

Eliminate coupling

Intermediary

A class interface has a large number of functions that are delegated to other classes.

Half of the Functions

Remove man-in-the-middle

 

A few functions

Direct call

Man-in-the-middle has other behaviors

Let the delegate class inherit the delegate class

Relationship

A class needs to know the private members of another class.

Sub-classes over-understand superclasses

Change inheritance to delegate and remove sub-classes from the inheritance system

Encapsulation

Bidirectional association between classes

Remove unnecessary associations

Classes have something in common

Extract new classes

Similar class

The two functions do the same thing, but the signatures are different.

 

Merge

 

Imperfect Class Libraries

Class Library functions are not well constructed and cannot be modified.

Want to modify one or two functions

Add a function in the call class

 

Want to add a bunch of additional Behaviors

Use subclass or package class

Naive data

In addition to fields, a class is a field accessor or configurator.

 

1. Replace the public field with an accessors
2. encapsulate the set properly
3. Remove unnecessary seters
4. Move the method called by the accessor and setter to this class.
5. Hide accessors and seters

Encapsulation

Rejected gifts

The derived class only uses a few member functions of the base class.

The subclass rejects the inheritance of the superclass interface.

Use delegation to replace inheritance

 

Too many comments

A piece of code has long comments

 

Eliminate various bad tastes

 

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.