Free refactor tool in all versions of Visual Studio, totally cool

Translate This Page

Free refactor tool in all versions of Visual Studio, totally cool

  • Comments 2

I have been using refactoring with C# for quite awhile, and realized that many readers may not have noticed that the refactoring tool is quite smart.  In the following code, I noticed that the code starting with “String ViewportHeight” down to String BallX could be put in a method, but I was being lazy and not wanting to generate a parameter passing function.  Turns out that refactor detects the parameters and puts them into the refactoring for you!

Start with this code in the draw method:

protected override void Draw(GameTime gameTime)
{  
    spriteBatch.Begin();

    spriteBatch.Draw(t_ball, r_ball, Color.Red); 
    /*****************************************************
     * Refactor Code starts here (start paste after
     * the line of asteriks or stars)
     * ***************************************************/
    String ViewportHeight =
        Convert.ToString(graphics.GraphicsDevice.Viewport.Bounds.Height);
    String ViewportWidth = 
        Convert.ToString(graphics.GraphicsDevice.Viewport.Width);
    String ViewportData = 
        "Height =" + ViewportHeight + " Width = " + ViewportWidth;
    String BallY = 
        "Y= " + Convert.ToString(r_ball.Y);
    String BallX = 
        "X= " + Convert.ToString(r_ball.X); 
    /*****************************************************
     * Refactor code finishes here (End paste at the line
     * above the line of asteriks or stars)
     * **************************************************/
    spriteBatch.DrawString(Font1, ViewportWidth, FontPosX, Color.Yellow);
    spriteBatch.DrawString(Font1, BallY, FontPosY, Color.Black);
    spriteBatch.End();           
        
    base.Draw(gameTime);
}
Right click on the commented code and select refactor-extract method, add a method name (that is: don’t use the default name)

The code changes into the following:

 
            /*****************************************************
             * Refactor Code starts here (start paste after
             * the line of asteriks or stars)
             * ***************************************************/
            String ViewportWidth;
            String BallY;
            StringForData(out ViewportWidth, out BallY);
            /*****************************************************
             * Refactor code finishes here (End paste at the line
             * above the line of asteriks or stars)
             * **************************************************/

The following method was added by the refactoring:

private void StringForData(out String ViewportWidth, out String BallY)
 {
     String ViewportHeight =
         Convert.ToString(graphics.GraphicsDevice.Viewport.Bounds.Height);
     ViewportWidth =
         Convert.ToString(graphics.GraphicsDevice.Viewport.Width);
     String ViewportData =
         "Height =" + ViewportHeight + " Width = " + ViewportWidth;
     BallY = "Y= " + Convert.ToString(r_ball.Y);
     String BallX =
         "X= " + Convert.ToString(r_ball.X);
 }
Leave a Comment
  • Please add 1 and 1 and type the answer here:
  • Post
  • At my last job we used ReSharper and it was scary how well it could predict things like variable names when you refactored with it.  Now I'm using the VS2010 built in tools and like them pretty well.

  • Hi Jack W.,

    Yep there are second party tools that are really good, thanks for calling out ReSharper, which can be found at:

    www.jetbrains.com/resharper

    There is a 30 Day free demo, and if students are reading this you might want to give it a shot.  It appears that an accredited institution can get a free use license from ReSharper.  

    Bottom line Jack: Glad you brought this up!  

    Thank you,

    Sam

Page 1 of 1 (2 items)
Generic Content

Legal Note:

  1. The author, Sam Stokes, is an employee of Microsoft
  2. There is no guarantee that anything I write is correct, I do try to make sure that what I write is correct.
  3. Use anything written in this blog at your own risk
  4. Test what I write about before using the information
  • Sometimes posts will disappear because I realized that they really don't fit with the total chaos of what I write about
  • Links are tested and available on date of publication, what others do with their links are out of my control

Restrictions:

  • Whatever I write on this blog is unedited by management or anyone, which should be obvious from the randomness of the blog.  I simply write about things I like.
  • Microsoft does not restrict me in anyway, so I generallyblather about whatever I want to.
  • For personal reasons I will not write about:
    • Company confidential stuff, mainly because Microsoft is more transparent than any company I have ever worked for.
    • Other people unless they give me permission to write about them