Refactoring to improve the design of existing code

Source: Internet
Author: User

Recently took over a project, the source code of the architecture and many designs have a bad taste. You want to refactor, but you don't have enough clout.

First, the restructuring of the tangle:

(1) If the existing code is available, will your refactoring be more efficient than it is now?

(2) The project progress is relatively tight, do you want to spare time to do such a job without KPIs;

(3) After you re-construct, others need to re-read your source code, to give colleagues to re-learn the code of the workload;

(4) If the project is sustainable, if there is no need, what else do you refactor?

(5) If you want to stay in this company for a long time, after the reconstruction, you may not be.

Second, if not re-construction, it will certainly bring great difficulties to development:

The original code is like a padded jacket full of patches, it can keep warm, but it is difficult to locate where the air leakage, even if the location, but a new patch, but mend work may not end.

(1) The change is very energy-consuming;

(2) Bug Pattern Bursting

(3) Poor expansibility, continuous development of follow-up requirements is difficult;

(4) Poor adaptability, lead a whole body.

Refactoring or not refactoring, this is a problem, until the "refactoring to improve the existing code design", gave me a lot of inspiration. After a cursory look at it, the truth is clear, my previous ideas should be called rewriting rather than refactoring.

Refactoring is the process of gradual improvement and is an intermediate option for rewriting and patching.

Of course, after reading the book, I chose to rewrite, because the project is not very large, rewrite to do more work less.

Refactoring uses methods that are also good references in the process of writing code. Methods are written in the 2-13 chapters of the catalogue, the meaning is more obvious, idle time to see, but also helpful.

The 2nd Chapter Reconstruction Principle2.1 What is refactoring2.2Why Rebuild2.3when to refactor2.4how to say to the manager2.5the challenge of refactoring2.6Reconstruction and Design2.7Refactoring and Performance2.8Refactoring Origins where 71 the 3rd chapter code bad taste3.1Duplicatedcode (Duplicate code) the3.2Longmethod (Over-length function) the3.3Largeclass (over-large class) +3.4Longparameterlist (over-length parameter column) +3.5Divergentchange (Divergent Variation) -3.6Shotgunsurgery (Shotgun-Modified) the3.7Featureenvy (Attachment Complex) the3.8Dataclumps (Data Mud Regiment)Bayi3.9Primitiveobsession (basic type Paranoia)Bayi3.10Switchstatements (switch thriller appearances) the3.11Parallelinheritancehierarchies (parallel succession system) the3.12Lazyclass (redundant Class) the3.13Speculativegenerality (Rhetoric of the future) the3.14Temporaryfield (confusing temporary field) -3.15Messagechains (over-coupled message chain) -3.16Middleman (Middleman) -3.17Inappropriateintimacy (Contempt relationship) -3.18Alternativeclasseswithdifferentinterfaces (analogous Class) -3.19Incompletelibraryclass (Imperfect library Class) the3.20DataClass (Childlike data Class) the3.21Refusedbequest (rejected bequest) the3.22Comments (Too many comments) theThe 4th Chapter constructs the test system4.1value of self-test code4.2junit Test Framework4.3Add more Tests 97 5th refactoring list5.1Refactoring the record format 1035.2Find Quote Points5.3How mature are these refactoring methods 106 the 6th chapter re-organization function6.1ExtractMethod (Refinement function) the6.2InlineMethod (inline function)1176.3Inlinetemp (Inline temp variable)1196.4Replacetempwithquery (replace temporary variable with query) -6.5Introduceexplainingvariable (introduction of explanatory variables)1246.6Splittemporaryvariable (break temporary variable) -6.7Removeassignmentstoparameters (Remove the assignment to the parameter)1316.8Replacemethodwithmethodobject (Replace function with function object)1356.9Substitutealgorithm (Replacement algorithm)139The 7th chapter moves the characteristics between objects7.1Movemethod (Move function)1427.2Movefield (move field)1467.3Extractclass (Refining Class)1497.4Inlineclass (Inline the Class)1547.5Hidedelegate (hide "delegate relationship")1577.6Removemiddleman (removal of the middleman) the7.7Introduceforeignmethod (Introducing an additional function)1627.8Introducelocalextension (introducing local extensions)1648th Re-organization of data8.1Selfencapsulatefield (self-encapsulated field)1718.2Replacedatavaluewithobject (replace data values with objects)1758.3Changevaluetoreference (change value object to reference object)1798.4Changereferencetovalue (changes the Reference object to a value object)1838.5Replacearraywithobject (replaces an array with an object)1868.6Duplicateobserveddata (copy "monitored data")1898.7Changeunidirectionalassociationtobidirectional (Change one-way Association to Bidirectional Association)1978.8Changebidirectionalassociationtounidirectional (change bidirectional Association to one-way association) $8.9Replacemagicnumberwithsymbolicconstant (replacing magic numbers with literal constants)2048.10Encapsulatefield (Encapsulate Field)2068.11Encapsulatecollection (Package collection)2088.12Replacerecordwithdataclass (replacing records with data classes)2178.13Replacetypecodewithclass (replace type Code with Class)2188.14Replacetypecodewithsubclasses (replace type code with subclass)2238.15Replacetypecodewithstate/strategy (replace type code with State/strategy)2278.16Replacesubclasswithfields (replace subclass with Field)2329th. Simplifying Conditional Expressions9.1Decomposeconditional (decomposition condition expression)2389.2Consolidateconditionalexpression (merge conditional expression) -9.3Consolidateduplicateconditionalfragments (merge duplicate conditional fragments)2439.4Removecontrolflag (remove control tag)2459.5Replacenestedconditionalwithguardclauses (replace nested conditional expressions with a Guardian statement) -9.6Replaceconditionalwithpolymorphism (replacing conditional expressions with polymorphism)2559.7Introducenullobject (Introducing a Null object)2609.8Introduceassertion (Introducing assertions)26710th Chapter Simplified function Invocation10.1Renamemethod (function renaming)27310.2Addparameter (add parameter)27510.3RemoveParameter (Remove parameters)27710.4Separatequeryfrommodifier (separating the query function from the modified function)27910.5Parameterizemethod (make function carry parameters)28310.6Replaceparameterwithexplicitmethods (replace parameter with explicit function)28510.7Preservewholeobject (keep the object intact)28810.8Replaceparameterwithmethods (replace parameter with function)29210.9Introduceparameterobject (Introduction of Parameter objects)29510.10Removesettingmethod (remove SetPoint function) -10.11Hidemethod (hidden function)30310.12Replaceconstructorwithfactorymethod (replace constructor with factory function)30410.13Encapsulatedowncast (Package down transition)30810.14Replaceerrorcodewithexception (replace the error code with an exception)31010.15Replaceexceptionwithtest (replace exception with test)315The 11th chapter deals with the generalization relationship11.1Pullupfield (field Move up) the11.2Pullupmethod (function Move up)32211.3Pullupconstructorbody (the constructor body moves up)32511.4Pushdownmethod (function Move Down)32811.5Pushdownfield (field Move Down)32911.6Extractsubclass (Refinement sub-Class) the11.7Extract Superclass (Refined Super Class)33611.8Extract Interface (Refining interface)34111.9Collapse Hierarchy (Folding inheritance system)34411.10Formtemplatemethod (shaping template functions)34511.11Replace inheritance with delegation (replace inheritance with delegate)35211.12Replace delegation with inheritance (replace delegate with inheritance)355The 12th chapter of large-scale reconstruction12.1Tease Apart inheritance (combing and decomposing the inheritance system)36212.2Convert procedural design to Objects (convert process to object design)36812.3Separate Domain fromPresentation (separating the field and presentation/display)37012.4Extract Hierarchy (Refining inheritance system)375

Refactoring to improve the design of existing code

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.