Blogs

News

Favorite Posts

ctodx

Discussions, news and rants from the CTO of Developer Express, Julian M Bucknall

Quick refactoring with Refactor! Pro

     

A moment ago, I was writing some code that configured a chart and I thought it would be good to quickly post what I was doing, not because it was especially difficult XtraCharts code, but it illustrated a refactoring technique that not many people know.

The code I'd written added two horizontal constant lines to a bar chart. This was the starting point:

    // set up the constant lines
    ConstantLine line = new ConstantLine();
    line.ShowBehind = true;
    line.AxisValue = 1000000;
    line.ShowInLegend = false;
    line.Color = Color.DarkSeaGreen;
    line.LineStyle.DashStyle = DashStyle.Dot;
    diagram.AxisY.ConstantLines.Add(line);

    line = new ConstantLine();
    line.ShowBehind = true;
    line.AxisValue = 1500000;
    line.ShowInLegend = false;
    line.Color = Color.DarkSeaGreen;
    line.LineStyle.DashStyle = DashStyle.Dot;
    diagram.AxisY.ConstantLines.Add(line);

I'm sure, like me, you're shuddering at the repetition. No sweat, I deliberately decided to code it like this, prior to removing the redundancy, so that I could make sure that (a) it worked, and (b) that I'd configured the lines the way I wanted. As it happened, after that bit of coding, the only difference between the two blocks of code was the value, but I could have decided on, say, different colors for the lines as well, so it was worth doing it this way.

I highlighted the first block of code, minus the line that added the line to the axis' collection of lines, hit the Refactor key (the back tick) and extracted a method. The extracted method looked like this:

  private static ConstantLine GetLine() {
    ConstantLine line = new ConstantLine();
    line.ShowBehind = true;
    line.AxisValue = 1000000;
    line.ShowInLegend = false;
    line.Color = Color.DarkSeaGreen;
    line.LineStyle.DashStyle = DashStyle.Dot;
    return line;
  }

I'm fine with the automatically-generated name of the method (and that it's marked static), but I now need to add a parameter to pass in the axis value (here it's 1000000). This is a two step process.

First declare the constant as a local: highlight the value and select the "Declare Local" refactoring. I called the new variable axisValue:

  private static ConstantLine GetLine() {
    ConstantLine line = new ConstantLine();
    line.ShowBehind = true;
    int axisValue = 1000000;
    line.AxisValue = axisValue;
    line.ShowInLegend = false;
    line.Color = Color.DarkSeaGreen;
    line.LineStyle.DashStyle = DashStyle.Dot;
    return line;
  }

Second, promote that local variable to a parameter. No need to highlight the variable name, just hit the Refactor key while the cursor is in the identifier.

  private static ConstantLine GetLine(int axisValue) {
    ConstantLine line = new ConstantLine();
    line.ShowBehind = true;
    line.AxisValue = axisValue;
    line.ShowInLegend = false;
    line.Color = Color.DarkSeaGreen;
    line.LineStyle.DashStyle = DashStyle.Dot;
    return line;
  }

Just what I wanted. I then hit Esc to gather the marker left by the Extract Method refactoring to see this:

    // set up the constant lines
    ConstantLine line = GetLine(1000000);
    diagram.AxisY.ConstantLines.Add(line);

    line = new ConstantLine();
    line.ShowBehind = true;
    line.AxisValue = 1500000;
    line.ShowInLegend = false;
    line.Color = Color.DarkSeaGreen;
    line.LineStyle.DashStyle = DashStyle.Dot;
    diagram.AxisY.ConstantLines.Add(line);

I can now inline that assignment since there's no need to assign the result of GetLine to a variable, and then pass the variable to the Add method. Note the variable is not removed by the refactoring since it's used lower down.

    // set up the constant lines
    ConstantLine line = GetLine(1000000);
    diagram.AxisY.ConstantLines.Add(GetLine(1000000));

    line = new ConstantLine();
    line.ShowBehind = true;
    line.AxisValue = 1500000;
    line.ShowInLegend = false;
    line.Color = Color.DarkSeaGreen;
    line.LineStyle.DashStyle = DashStyle.Dot;
    diagram.AxisY.ConstantLines.Add(line);

Now I can dupe the code that adds the new line from a call to GetLine, change the parameter to 1500000, and delete the other unnecessary lines.

    // set up the constant lines
    diagram.AxisY.ConstantLines.Add(GetLine(1000000));
    diagram.AxisY.ConstantLines.Add(GetLine(1500000));

Done. Of course, it would be nice if Refactor! Pro noticed the duplicated block during the Extract Method refactoring and fix it up too. We're working on that.

Published Jul 29 2009, 12:57 PM by Julian Bucknall (DevExpress)
Filed under: ,
Technorati tags: Refactor, XtraCharts
Bookmark and Share

Comments

 

Karl Rostock_2 said:

Hi Julian,

Ive gotta say Refactor & Code Rush have come a long way in the last 2 years congrats, but..... the thing i dont like about the Coderush & Refactor packages is the free versions offer nearly as many features as the Pro version so when it comes round to renewing my license next year im going to just get the winforms package and download the free version of C&R instead of the enterprise suite, its one thing to promote a product with a free intro but its another to provide a full working suite that lowers the value of its counter part Pro version.

Im sure many of the paid subscribers will agree that if i where to renew the enterprise license now i would subconcioulsy feel im getting less bang for my buck.

Just my thoughts.

Karl

July 30, 2009 10:32 AM
 

Richard Morris (DevExpress) said:

Among other features CodeRush Xpress doesn't have the 20,000+ code templates that are in the Pro version.  Personally I think I'd miss those more than any other CodeRush feature.

The full set of differences are here community.devexpress.com/.../differences-between-coderush-xpress-and-coderush.aspx

July 31, 2009 2:42 AM
 

Karl Rostock_2 said:

I wasnt aware of that breakdown it looks good, and looking at it there are significant benefits to the pro version.

Thanks for the reply

Karl

July 31, 2009 7:10 AM
 

Shaw Innes said:

One thing that confuses me is whether I should install the free versions alongside the pro version?  Are they complimentary or can I just install the pro version.... ?  

July 31, 2009 7:45 AM
 

Julian Bucknall (DevExpress) said:

Shaw: If you have the full version, you don't need the free one. Everything that's in the free version is there in the full version; you're not missing out on anything Smile.

Cheers, Julian

July 31, 2009 6:06 PM

About Julian Bucknall (DevExpress)

Julian is the Chief Technology Officer at Developer Express. You can reach him directly at julianb@devexpress.com. You can also follow him on Twitter with the ID JMBucknall.
More from DevExpress
Live Chat
Have a pre-sales question?
Need assistance with your evaluation?
We are here to help.
Chat is one of the many ways you can contact members of the DevExpress Team. We are available Monday-Friday between 8:30am and 5:00pm Pacific Time.
If you need additional product information, require pre-sales assistance, or want help with your order, write to us at info@devexpress.com or call us at
+1 (818) 844-3383.