Rickard Nilsson

  • Blog
  • Archive
  • About
  • Contact
Sign in

Welcome to rickardnilsson.net

rickardnilsson.net is a weblog and the online home of web developer and father of three, Rickard Nilsson... More

Rickard blogs about creating software solutions using ASP.NET and agile practices.

Follow @rickardn

Top Posts

  • Applying stylesheets dynamically with jQuery
  • ASP.NET MVC 2 Framework and Unity 2.0 Dependency Injection Container
  • Code Kata Cast
  • Isolate your code from ASP.NET with Moles Isolation Framework
  • ReSharper User tip #2: Refactor rename namespace
  • Dependency injection in ASP.NET MVC with Unity IoC Container

Categories

  • .NET
  • Agile
  • ASP.NET 2.0
  • ASP.NET 3.5
  • ASP.NET 4
  • ASP.NET MVC
  • BlogEngine.NET
  • C# 2.0
  • C# 3.0
  • C# 4.0
  • Continuous Integration
  • CSS
  • Design by Contract
  • Design Patterns
  • iPhone
  • JavaScript
  • Kata
  • Moles
  • Open source
  • Personal
  • Review
  • Social media
  • Software development
  • TDD
  • Testing
  • Umbraco
  • Unit testing
  • Unity
  • User tip
  • Web development

Five most recent posts

  • C# REPL and Interactive interpreter
  • TDD Kata 2 - Interaction based testing
  • Reflections from day 3 - Umbraco Codegarden 11 in Copenhagen
  • Reflections from day 2 - Umbraco Codegarden 11 in Copenhagen
  • Reflections from day 1 - Umbraco Codegarden 11 in Copenhagen

Tag cloud

  • agile
  • blogengine.net
  • c#
  • code kata
  • codegarden11
  • continuous integration
  • css
  • dependency injection
  • fakes
  • iso 8601
  • javascript
  • jquery
  • refactoring
  • resharper
  • tdd
  • testing
  • umbraco
  • unit test
<< Blogging with MS Word 2007 | Applying stylesheets dynamically with jQuery >>

ReSharper User tip: Refactor magical strings to variable

Thursday, 31 July 2008 00:28 by Rickard

I've been using Jetbrains ReSharper a while now and I love it. I can't even imagine going back to plain Visual Studio anymore because there are so many things in my daily work that involves ReSharper, even simple tasks like editing source code and navigating through code and source files, let alone creating files and running unit tests.

Now I want to share a user tip I found that isn't obvious to find nor part of any context menu. It's actually a refactoring and I call it Magical strings to variable. You can use it when you end up with multiple equal string literals in a piece of code. You would probably want to gather all of the string literals in a variable and reuse it through out the code. You can use ReSharper to do it for you in a couple of key strokes.

Example

[Test]
public void ParentPresenter_Update_should_update_view() {
    var model = new User {
                        Name = "foo"
                    };

    using (mockery.Record()) {
        Expect.Call(parentView.Username = "foo");
    }

    using (mockery.Playback()) {
        IParentPresenter presenter = new ParentPresenter(parentView) {
                                Model = model
                            };
        presenter.Update();
        Assert.That(presenter.Model.Name, Is.EqualTo("foo"));
    }
}

[Test]
public void ParentPresenter_Update_should_update_view() {
    var name = "foo";
    var model = new User {
                        Name = name
                    };

    using (mockery.Record()) {
        Expect.Call(parentView.Username = name);
    }

    using (mockery.Playback()) {
        IParentPresenter presenter =
            new ParentPresenter(parentView) {Model = model};
        presenter.Update();
        Assert.That(presenter.Model.Name, Is.EqualTo(name));
    }
}

Mechanics

1. Highlight one of the string literals

2. Press Ctrl+R, V (Ctrl+Alt+V)* to introduce a variable

3. Select to replace all occurrences (default option)

4. Pick a name for the variable

..and you're done! Please leave a comment if you find this usefull.

* Visual Studio scheme (ReSharper 2.x / IDEA scheme)

Tags:   resharper, refactoring
Categories:   C# 3.0 | User tip
Actions:  

Related posts

ReSharper User tip #2: Refactor rename namespaceHow to rename a namespace for all of its classes using ReSharper.ReSharper templates from the Code Kata CastAfter I posted my Code Kata Cast I received some feedback regarding the ReSharper templates I use to...The Humble dialog v.2Update! Download source: TheHumbleDialog2.zip (29,96 kb) I've been working to get my head around th...
Comments are closed
 
Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© 2008-2011 rickardnilsson.net
Creative Commons-licens