When refactoring encounters IOC

Source: Internet
Author: User
Document directory
  • Code-level Reconstruction
  • Method-level Reconstruction
  • Entity, structure-level reconstruction-inheritance and Polymorphism
  • Module-level Reconstruction
When refactoring encounters IOC

 

Origin Oo and Reconstruction

Introduction

I remember that when I was in college, oo thought like a popular song. As long as I was a computer student, I could say that I was a good guy. You may ask what Oo is. At that time, we (mostly) talked about singing, encapsulation, inheritance, and polymorphism. You may ask how to implement it. We will say: encapsulation is to hide the attribute get and set. Inheritance means that there is a parent class subclass that inherits it for reuse. polymorphism means that a class object dynamically points to the parent class and is called polymorphism. At that time, I knew that Oo was just verbal.

 

I remember a buddy who went to bed and bought several master books, such as The Mythical Man-moon, Java programming ideas, and design patterns, at that time, I despised him and wanted to ask a question. Do you really understand it? Because no code has been written to a certain amount, it is also a white view, because I am also doing this, I can't do it any more. One or two colleagues in the series often walked by me with a suspicious look. At that time, it seems that I didn't have any books like this .......

 

Many people open the restructures, design patterns, and so on, just like entering a "sky garden", so many techniques make you sigh and spend a lot of time understanding. But when I joined the book, I immediately entered another place, and the "sky garden" was so far away from you, so high. As the amount of code you write reaches a certain level, you will gradually understand their application scenarios and solutions.

 

What is refactoring?

Refactoring is to adjust the program code to improve the quality and performance of the software without changing the existing functions of the software, so that the program design mode and architecture become more reasonable, improve software scalability and maintainability.

 

Benefits of refactoring

It is obvious that it is easy to read, locate potential bugs, improve software efficiency, and reduce maintenance costs.

 

When to refactor?

Cubic rule
When you do something for the first time, you just need to do it. If you do something similar for the second time, you will be disgusted, but you cannot do it again. If you do something similar for the third time, you should rebuild it.

  1. Refactoring when adding features
  2. Rebuild together when fixing errors
  3. Review code with refactoring

However, the software cannot run normally or should not be restructured when the software is delivered

 

 

The following are several phases of Self-writing code refactoring:

Code-level Reconstruction

At the beginning, we often Ctrl + C and CTRL + V for 10 Ctrl + V pages. At the beginning, we thought Ctrl + V was the most time-saving and convenient. When I was reading a book, I had to work on a small project, but I used Ctrl + C and CTRL + V in many places. The most important thing I remember is to do a permission verification. After a user logs on, he can get the permissions of the user. When the user accesses the page, it verifies whether the user has the permission to access the page. I cached the user information locally and added verification judgment to each page constructor. If the verification fails, I will jump to the prompt page. I remember there were about 30 pages. If you have been tired of copying for about 30 times, you may not be able to copy the file because the copy is missing or the copy is wrong. CTRL + V test, I will go, verify that there are vulnerabilities written in the rules, and then change each page one by one, with 30 pages. There is no problem after the test is completed, however, if the function does not meet the requirements, the user table will be changed and the authentication method will change. There is no way to change the code for each page .... An extremely painful and bloody lesson!

Code-level refactoring involves many other types of refactoring, such as renaming, encapsulating fields, improving readability, and adding comments.

 

Method-level Reconstruction

Knowing the pain points will change, and gradually learning to extract the public parts into a method.The method should be as simple as possible, with specific functions. To be retained. Method overload and missing parameters are also used in different situations

 

We will gradually accumulate many common methods in our work, such as paging, sqlhelper, image processing, and random numbers. With the increasing development experience, public methods will become more and more valuable resources.

 

Entity, structure-level reconstruction-inheritance and Polymorphism

We often use inheritance at the beginning. As the demand changes, our parent class may add new attributes or methods. Slowly, we will find that inheritance may cause infinite expansion, in addition, sub-classes are designed for victims to inherit things that are completely unnecessary and affect performance. It is also difficult to maintain and modify objects.

Later, we found that inheritance should be used less, unless it is used only when there is a strong parent-child inheritance relationship. The flexibility of coupling is much higher than that of inheritance.

I personally think that polymorphism is the core of object orientation. I often do not know when to use polymorphism in the object structure before, and then I am gradually confused that, whether to use abstract classes or interfaces when using polymorphism.

 

Later we heard that the idea of High Cohesion and low coupling can find the use of polymorphism in abstract programming (interface-Oriented Programming. Slowly, We will extract the object and behavior to abstract the interface implementation to avoid the design of a large base class. Abstract or completely separated to respond to changes in requirements.

 

Here we often use some design patterns to cope with changes and extensions.

 

Module-level Reconstruction

Previously, we may have a headache for page and business logic, business logic, and data instantiation. Then we know that in order to better organize development at different layers and separate coupling, we will also adopt MVC, MVP, mvvm mode or hierarchical development;

 

In the project, in order to remove the coupling between modules and components, we will also use the IOC idea to decouple and introduce the IOC framework;

 

If the project data processing volume is not very large here, we will introduce the ORM idea to Accelerate the development speed and maintainability of the project to make the project develop faster and more convenient;

 

When users constantly increase system pressure and gradually increase users' long waits, we may consider writing the cache mechanism or introducing the Cache architecture.

 

If our applications and services are staggered, we may consider using SOA architecture, WCF, Web service, etc.

 

Confusions

As the system grows, every time the development department receives a new requirement or needs change, it seems that everyone has some coolness, fearing that they need to make small changes, introduce yourself or the entire project into fear of errors. Maybe you want to say, "God, let's do it again. We must be near perfect ...". I used to dream about it. Reconstruction is continuous. No one can rebuild the system perfectly at a time. Even if it was perfect at that time, with the passage of time, demand and changes, we still need to rebuild the system. This phenomenon is mainly caused by the following reasons:

  1. You do not have the courage to refactor, And you are afraid that mistakes will cause unnecessary troubles.
  2. I am so confused that other colleagues are happy and unbalanced. As a result, your desired restructuring plan becomes fictitious.
  3. The amount of work is too large. If you want to modify a large number of interfaces or structures, I suggest you rewrite them.

 

Refactoring is an inevitable process for programmers, and there have been many books and tools since now. refactoring-improving the design of existing code and the 31-day reconstruction recommendation of the Paladin. In addition to vs, which is used by the refactoring tool itself, the Reshaper is a great tool that can help you analyze and find the places to be reconstructed. Next I will talk about IOC. coming soon ......

 

 

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.