id=”js_article-tag-card__right” class=”article-tag-card__right”> 1


Recently, when I was expanding an existing project, I found that a class to be changed was 900 lines long and opened nearly 40 public interfaces, and I changed it in tears.

In order to prevent such a tragedy from happening to me again, I felt the need to write a blog to let fellow programmers know the importance of code refactoring.

If you have an ape next to you with a class with thousands of lines written on it, be sure to transfer this article to ta

Why can’t the class be too long?

The class is too long –

can’t read, can’t be expanded< ul class=”list-paddingleft-2″> can’t be

  • read – it takes several seconds to roll directly with a wheel, even if it is the original author, I am afraid it will be difficult to sort out the entire class after a long time, let alone other readers

  • Scaling is not possible – a class has too many interfaces, which makes it extremely difficult to extend the class, and a thousand lines

  • of class are too long – there may be redundant code

    At this time, the CV engineer played a cold war

    redundant code,

    that is, repeated code, usually from the hands of CV engineers who use Ctrl+C, Ctrl+V to produce code, redundant code is very harmful:

    1. redundant code makes methods, Classes that are too long, redundant code that is not concise

    2. will lead to divergent modifications (Ctrl+V needs to be changed everywhere when redundant code needs to be changed) and

    classes that are too long – mostly too many responsibilities

    A class opens dozens of interfaces, there is definitely a problem of too many responsibilities, just like the Tom cat in the picture, a class has too many responsibilities also has a huge problem:

    1. violates the design principle – the principle of single responsibility (The principle of single responsibility requires that a class only fulfill one responsibility, for example, a Tom only does one thing in sweeping, wiping the table, and mopping the floor, while the implementation of other things can be transferred to the Spike dog or the fat maid), violating this principle will lead to divergent changes, divergent modifications, overly long classes, etc., and will make your class difficult to expand, and even make other programming apes think that you are not professional divergent

    2. changes (There are many places where such modifications are issued), if a class has many responsibilities, then its fan (caller) must be many, and each caller’s modification may make your class have to be modified with it, that is, divergent change

      means that no matter where something goes wrong, your class will suffer

    3. Divergent modification (meaning that there are many places where such modifications cause modifications), the same, if a class has many responsibilities, then the subordinates that support its implementation, that is, fan-out (callee) must be many, if such logic changes, all subordinate callees may have to be modified with it, that is, divergent modification

      means that your class has a problem, no matter where it will suffer

    4. Difficult to extend: If you have a class with a lot of interfaces, what about its subclasses? What about its packaging class? Do they all have to implement so many interfaces and all have to take on the same amount of responsibilities? It’s really troublesome

    5. to expand: [Test Fury] [Operation and Maintenance Rage]

    I’ve written thousands of lines, what should I do?

    Refactoring – extracting redundant code Extracting redundant code is to extract the repeated code into an independent method, and then use this code

    no longer need Ctrl + C, Ctrl + V, but directly call the corresponding method to

    do so or shorten the original method , making the original method more concise and easy to understand

    is that

    if this code needs to be modified, it only needs to be modified in one place, rather than divergently modifying everywhere

    is really a killing stone and using

    IDEA for redundant code extraction

    1. Find the duplicate code

    2. Perform method extraction Right-click-> select the Refactor->Extraction-> method (or use the shortcut Ctrl + Alt + m directly).

    Automatically detects the nuances of individual duplicate codes, some code may only change one or two variables, IDEA will automatically detect it, and remind us when extracting methods, selecting Accept Signature Change on the left can automatically replace more duplicate points in the extracted method

    You can choose to replace all duplicate code (there are 18 places)

    class=”rich_pages wxw-img” src=””>3

    . Refactoring—— Change

    method signature

    If you are not satisfied with the name, parameters, return value or modifier of the extracted method, do not use Ctrl + R to modify, IDEA provides a refactoring method – change the signature (shortcut Ctrl + F6)

    Note: The name of the method refers to what the method does, not how to do it, preferably in verb + noun format

    For example: Tom. sweeping

    () √Tom. sweeping with broom()

    ×Tom. sweeping with a broom

    () ×

    refactoring – transferring member variables + functions (transferring responsibilities)

    To transfer member variables and functions that should not be managed by yourself

    , consider two questions: Who should be transferred? To whom to transfer?

    Look at a figure

    < img

    class=”rich_pages wxw-img” src=””>
      member [partial A] in the figure is called twice by class [A], and only called once by the class [too long class] it is in, so it should be transferred to [A] to manage Because the intimacy between the function [partial A] and the member [partial A] is high (only [partial A] is

    1. called), it should advance and retreat with [partial A], The same is true for transferring to [A

    2. ] member [bias B] and function [bias B] duty 1 (function [1] and member [bias duty 1]) and duty 2 (function [2]

    3. and member

    4. [bias duty 2]) Since no suitable class can be transferred, a new class should be extracted

    Note that deciding which member variable to move

    first, and then deciding which function

    to move

    uses IDEA to transfer member variables and functions

      to move the member variable, select

    1. the member variable -> right-click->Refactor->Move, and then select which class to transfer to

    2. Move function (same steps as move member variable)

    refactoring – extract classes

    When you find that the member variables and functions to be transferred cannot find a suitable class (transfer responsibilities but cannot find the next home), think about it, this is the world of programs, And we programming apes are the creators of classes and objects, it’s time to create a new class that shares the responsibilities (member variables and functions) for us using

    IDEA to extract classes

    1. Refactoring Select the member variables and functions to be moved, right-click->Refactor->Extract->Delegate (extract a delegator, entrust him to manage this part of the variables and functions, if there are only variables or functions, you can extract the parameter object Paramater Object or Method Object method).

    It is not recommended to extract parameter objects, because general parameter objects are used for methods with many parameters (replacing a long parameter with a parameter object), and if the member variable is extracted and does not affect any function, it is useless objects, it is better to delete them directly

    2. Give the new class a name and choose a package


    public number (zhisheng ) reply to Face, ClickHouse, ES, Flink, Spring, Java, Kafka, Monitor keywords such as to view more articles corresponding to keywords.

    like + Looking, less bugs 👇