Let code refactoring fade away series (2) -- reduce code duplication

Source: Internet
Author: User
Tags repetition

Reduce code repetition: the so-called code repetition refers not only to multiple lines of identical code, but also to some duplicated or unnecessary code, such as the following code:

1 Public void repeated code example (Int? Parameter 1) 2 {3 if (parameter 1! = NULL) 4 {5 // process the code .... 6} 7 else 8 {9 parameter 1 = NULL; 10 // process code ..... 11} 12 13 // process the code ..... 14 15}

Some friends may say at first glance that there is no problem with this Code. How can I repeat it? Are there really no duplicates? Is there really no problem with this code?

So what is the meaning of this line of code in else? "parameter 1 = NULL? After all, only when "parameter 1" is null will else branch conditions be met before it enters this branch. That is to say, you already know that when this parameter is null, assign null to it, isn't this a duplicate?

Some may say who will write such code. In fact, I have seen similar code in the project. In fact, writing such code is nothing strange, because sometimes it is possible to encounter this problem when you follow the business logic.

To put it simply, the above sample code can reconstitute the following code:

1 Public void repeated code example (Int? Parameter 1) 2 {3 if (parameter 1! = NULL) 4 {5 // Process Code... 6} 7 8 // Process Code... 9 10}

 

Next, let's take a look at the familiar repeated code format:

Public void repeated code example method 1 (string parameter 1) {testmodel model = new testmodel (); If (! String. isnullorempty (parameter 1) {model. name = parameter 1; model. age = 0; model. creator = "current user"; model. createtime = datetime. now; // logic code} public void repeated code example method 2 (Int? Parameter 2) {testmodel model = new testmodel (); If (parameter 2! = NULL) {model. Age = parameter 2. value; model. creator = "current user"; model. createtime = datetime. Now; // logical code }}
 public class TestModel    {        public string Name { set; get; }        public int Age { set; get; }        public string Creator { set; get; }        public DateTime CreateTime { set; get; }    }

Can I refactor it to reduce repeated code? I don't think you want to repeat the Code either!

1 Public void repeated code example method 1 (string parameter 1) 2 {3 testmodel model = new testmodel (); 4 If (! String. isnullorempty (parameter 1) 5 {6 model. name = parameter 1; 7 model. age = 0; 8 newmethod (model); 9 // logic code 10} 11 12} 13 14 public void repeated code example method 2 (Int? Parameter 2) 15 {16 testmodel model = new testmodel (); 17 if (parameter 2! = NULL) 18 {19 model. age = parameter 2. value; 20 newmethod (model); 21 // logic code 22} 23 24} 25 26 Private Static void newmethod (testmodel model) 27 {28 model. creator = "current user"; 29 model. createtime = datetime. now; 30}

In this way, if the business changes in the future, we only need to modify the newmethod (testmodel model) method to modify the creator and time, instead of modifying the original two places, does it reduce code repetition and facilitate maintenance?

Of course, due to my limited ability, the above example is not particularly appropriate, but I want to talk about this idea, reduce repetitive code and reduce code duplication.

Or, if we think about this when writing code, do we still need to spend more time doing This refactoring? How can code reconstruction not get farther and farther away?

Links to the series of articles are as follows:

Let code refactoring fade-away series (1) -- remove multi-layer nesting

 

Let code refactoring fade away series (2) -- reduce code duplication

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.