The difference between code refactoring and architectural refactoring

1 Code refactoring

definition

Make any changes to the software code to increase readability or simplify the structure without affecting the output.

Purpose

Increase readability, increase maintainability, expandability

3 key points

  • does not affect output
  • do not fix bugs
  • Does not add new functionality

When refactoring the code, I found that a function implementation logic is unreasonable, can I modify it directly?

Of course not!

2 Architecture reconstruction

definition

Repair system quality problems without affecting overall system capability through the whole system architecture (4R).

Purpose

Fix quality issues (performance, usability, scalability...)

key point

  • Fix quality (architecture, not code level) issues and improve architecture quality
  • Does not affect overall system functionality
  • The nature of the architecture has not changed

Changing the implementation of a subsystem from hard-coding to a rule engine, is it code refactoring or architecture refactoring?

It belongs to architecture refactoring, architecture design scheme, and system scalability.

3 Code Refactoring VS Architecture Refactoring

4 Architecture Refactoring Techniques

4.0 Means

Can architectural refactoring modify Rank in 4R?

cannot! Modifying rank is not refactoring, but evolution. Dismantling microservices does not belong to changing rank. The external system collaboration method has to be modified. For example, Alipay, the payment method of Taobao, was separated and became an Alipay company.

4.1 First local optimization and then architecture reconstruction

local optimization

Definition: To optimize some services or functions without affecting the system architecture.

Common means:

  • Add index to database, optimize index
  • A data cache update strategy adopts background update
  • Increase the number of load balancing services
  • Optimize the logic of concurrency in the code
  • Modify the Innodb buffer pool configuration to allocate more memory
  • Add a parameter to an interface between services

Architecture Refactoring

Definition: optimize the system architecture, improve the overall quality, and architecture reconstruction will affect the 4R definition of the architecture.

Common means:

  • Introduce message queue (increase Role)
  • Remove ZooKeeper and change to built-in Raft algorithm implementation (delete Role)
  • Change Memcached to Redis (change Role)
  • Split microservices according to stability (split Role)
  • Merge microservices that are too fine-grained (merge Role)
  • Change the communication method between services from HTTP to gRPC (modify Relation)
  • The SDK changed from reading the local configuration file to reading the configuration from the management system (modify the Rule)

4.2 Targeted

the case

  • Development efficiency is very slow, P business and M system affect each other
  • There are many online problems, especially data problems
  • M system performance is very low

Targeted:

Refactoring only solves the first problem (development efficiency is very slow, P business and M system affect each other). What about other problems, architect, don’t you solve it? After the architecture is reconstructed, each business department will solve their own problems, such as the P business backend optimization problem, and the M system optimization performance problem, because these problems cannot be solved by refactoring, but by refactoring After splitting, each continues to optimize.

4.3 Combining vertical and horizontal

Combined

Convince business parties and bosses

  1. speak with data

    Convert "scalability" to "version development speed is very slow and then give the corresponding project data (pay attention to collecting data at ordinary times).

  2. Speak with a case (in fact, it is more effective, and the impact on people is more obvious) If there is no data, take an extreme case, such as a small function, which only takes 5 days to develop and test, but it takes 1 month to go online.

Lianheng

Convince other teams.

  1. Only by empathizing and thinking about the benefits to other teams can people cooperate.
  2. When reporting and summarizing win-win cooperation, bring other teams along.

the case

He Zong: Tell the PM and the project manager the extreme case, the design took 2 weeks, the development took 2 days, and it took a month to go online.

Lian Heng: P business online problems are greatly reduced, and P business will not be affected by other businesses

4.4 Strategizing

① Problem classification

Categorize the problems and focus on dealing with such problems for a period of time. Avoid comparing Excel tables and solve them one by one.

② Ranking of questions

Sort after classification and land in order of priority.

Avoid arranging refactoring tasks in a needle-like manner, and don't take the business's free ride to refactor:

  • avoid taking the blame
  • no significant effect
  • Unable to schedule heavy refactorings

③ Break through one by one

Each type of problem is first easy and then difficult.

Tackle easy problems and build confidence.

④ case

Before:

  • 1 Excel problem table with more than 100 rows, solved one by one
  • Specially pick soft persimmons
  • See every stitch

After:

  1. Categories: Performance, Components, Architecture, Code
  2. Phases: Optimization -> Architecture Refactoring -> Architecture Evolution
  3. Special implementation: clear time, goal, and version

5 Architecture Refactoring FAQ

Can architectural refactoring introduce new technologies?

Yes, but as few as possible, the architecture reconstruction requires fast and accurate .

What if the business does not have time to refactor?

Crying babies have milk to eat. Collect data and cases and speak with facts.

What if other teams don't cooperate?

Learn to use the power of superiors. If the superiors don't support it, it means that what you are doing is meaningless, so the leaders don't care. Then don't do it.

The business schedule is very tight, what should I do if there is not enough manpower?

Collect evidence that needs to be refactored, and make a technical report with justification

6 tests

6.1 Judgment

  1. Code refactoring, architecture refactoring, and architecture evolution do not need to fix problems×
  2. Microservice splitting can be both a means of architecture reconstruction and a means of architecture evolution√
  3. Architecture refactoring should take the free ride of the business version to avoid impact on the business version×
  4. Architecture refactoring is to fix problems, so all problems left in the system should be fixed during architecture refactoring×
  5. Architectural refactoring should be categorized and implemented step by step according to priority√

6.2 Thinking

When the architecture is refactored, can the code be refactored as well? Because the version is arranged anyway. no!

Local optimization is not part of code/architecture refactoring.


ref: Code refactoring and architecture refactoring: the difference you need to know - Nuggets

Guess you like

Origin blog.csdn.net/qq_34626094/article/details/130186614