Refactoring-make the code more concise and elegant: Practical Experience (provides a skill that allows you to save N more code)

Source: Internet
Author: User

I haven't written much in the past few days, because I used the CYQ. Data framework to reconstruct the source code of a previous blog. in the reconstruction process, the most important thing is to simplify the code.

 

Today, I will describe a very typical example. After reading this example, don't be surprised, don't doubt, it's not a magic horse, it's not a float cloud,

 

It is a very practical way to save N more code and make your code look more concise and elegant.

 

A typical example here is applied from my current blog reconstruction:

 

I. Normal Development

 

1: Take a look at the current project Solution

 

2: Let's talk about the Module library.

In general, we usually have a page base class. Of course there are different flower names. Some people often call it "PageBase". Here it is called HttpCustom, for example

 

3: Basic page content

For base classes, we encapsulate many common attributes and methods to make it easier to call each page, such

 

4: Page inheritance base class

After writing the base class, the page is basically inherited, and then we can easily get each attribute value, as shown in

 

5: complicated business Logic, which can be processed in Logic projects.

Therefore, we encapsulate many methods. Because we cannot directly obtain the attributes of the base class, we have to write the following method. Many parameters are added to the method so that the page can be uploaded one by one.

 

6: Next we will call

We feel that the call is very easy, and the page is called. We pass the parameters one by one. The page code is much simpler, and it is quite satisfied with a little sense of accomplishment.

 

 

Okay. Now, everyone has thought about it. Is it normal? Are you satisfied with the conciseness of your code?

Can it be more concise? 

 

Think, think, think... think of Shenma and fuyun?

After you have read the following content, the above Code is cloudification and cloudification !!!

 

Ii. refactoring-make the code more concise and elegant

 

On the night of refactoring, when I saw this code, my first thought was how to get the HttpCustom base class parameters in the Logic project.

As long as this can be done, the Logic class method can omit N parameters. For the page, you can also transmit N parameters in a batch of page calls + a batch of Logic classes, how big is it if all provinces are saved? If you think hard and think hard, everything will look better as long as you can save a large number of parameters, so I started to think about it ~~~

 

1: Find a pumping idea 

I wonder if anyone has the idea at the beginning: for example, let LogicAction inherit from the base class HttpCustom, so that you can also use the attributes and methods.

Where do you want to seek the ball from objects like HttpContext like Y? Waiting for the yellow pages to be hashed by the NULL value?

 

2: digging ideas from the find-type idea

Although the idea of 1 is very helpful, the idea of 1 is often the direction.

It also inherits from the base class, but not the previous HttpCustom, but the new abstract base class.

How should the new base class be associated with HttpCustom? Or simply, how do I obtain the value from HttpCustom?

 

3: refactor and extract the interface from HttpCustom

A: select the HttpCustom page base class, right-click and choose "Reconstruction"> "extract interface ".

 

B: select common attributes.

 

 

After confirmation, HttpCustom automatically inherits from IHttpCustom and generates the IHttpCustom file.

 

4: Create an abstract base class LogicBase and inherit the IHttpCustom Interface

Of course, the implementation interface is a bunch of Get and Set

 

 

5: logically let LogicAction inherit LogicBase

OK. You can get the parameter data from the base class. The method parameters are much simpler. The parameter passing over N is hidden, and the image of the above LogicAction class is compared, see how simple the code is ~~

 

 

6. Pass parameters through APIS

In the base class HttpCustom, we add a new method: SetInit, as shown in figure

 

 

7: Final Page call results

Compared with the previous page calls, you do not need to pass parameters at all. It is effort-saving to start coding ~

 

 

Of course, you can also load it in turn. IHttpCustom is added to the constructor in LogicBase.

The final implementation page is called as follows:

 

 

Statement:

A deliberate refactoring saves us the input and output of a large number of original parameters. Let's look back at our previous business logic code to see if a large number of Identical parameters are being passed through ~~~

~ Everything just wanted to make the code more concise and elegant, so I thought a lot about it ~~~

 

 

 

Is there a feeling that I want to say it, but I can't say it ~ So I leave a message: I am very excited, but I can't say it ~ Really want to ride in the cloud ~~

 

 

 

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.