Quick refactoring with Refactor! Pro

ctodx
29 July 2009

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.

5 comment(s)
Karl Rostock_2
Karl Rostock_2

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

30 July, 2009
Richard Morris (DevExpress)
Richard Morris (DevExpress)

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

31 July, 2009
Karl Rostock_2
Karl Rostock_2

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

31 July, 2009
Shaw Innes
Shaw Innes

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.... ?  

31 July, 2009
Julian Bucknall (DevExpress)
Julian Bucknall (DevExpress)

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

31 July, 2009

Please login or register to post comments.