Sign in
  • Blog
  • Archive
  • About
  • Contact

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.

Sites I've visited recently

Rtur.net / ASP.NET Forums / Karstad .NET User Group / JetBrains ReSharper / Scott Hanselman / dnrTV! / MSDN Radio / MIX'08 | Sessions / BlogEngine.Net / YUI Theater

Categories

  • RSS feed for .NET.NET
  • RSS feed for ASP.NET 2.0ASP.NET 2.0
  • RSS feed for BlogEngine.NETBlogEngine.NET
  • RSS feed for C# 2.0C# 2.0
  • RSS feed for C# 3.0C# 3.0
  • RSS feed for CSSCSS
  • RSS feed for Design by ContractDesign by Contract
  • RSS feed for Design PatternsDesign Patterns
  • RSS feed for JavaScriptJavaScript
  • RSS feed for TDDTDD
  • RSS feed for User tipUser tip

Five most recent posts

  • ReSharper User tip #2: Refactor rename namespace
  • Building a Photo Album widget for BlogEngine.NET
  • Applying stylesheets dynamically with jQuery
  • ReSharper User tip: Refactor magical strings to variable
  • Blogging with MS Word 2007

Tag cloud

  • ajax
  • blog
  • blogengine.net
  • c#
  • css
  • dbc
  • design by contract
  • dom
  • douglas crockford
  • foto
  • getweekofyear
  • gregoriancalendar
  • highlight
  • html
  • humble dialog box
  • iso 8601
  • javascript
  • jquery
  • jscript
  • julian bucknall
  • metaweblog api
  • model-view-presenter
  • mvp
  • photo album
  • picasa
  • recent posts
  • refactor
  • refactoring
  • release
  • resharper
  • rhino mocks
  • syntax
  • syntax highlighter
  • tdd
  • test coverage
  • types
  • web service
  • week
  • widget
  • word 2007
  • yahoo
  • yui

Recent comments

  • Syntax highlighting in BlogEngine.NET (9)
    darren wrote: thanks for this - got it up and running no problem… [More]
  • Syntax Highlighter Release 0.2 Beta (7)
    Rickard wrote: Line numbers are not supported in the current rele… [More]
  • Syntax Highlighter Release 0.2 Beta (7)
    Jack wrote: How can i show line numbers? [More]
  • Syntax Highlighter Release 0.2 Beta (7)
    Tahir Khalid wrote: Hi Rickard, thank you for replying to my humble bl… [More]
  • Building a Photo Album widget for BlogEngine.NET (5)
    Bryan Avery wrote: Great code, but you need to include the google lib… [More]
<< 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)

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:   resharper, refactoring
Categories:   C# 3.0
Actions:   E-mail | Permalink | Kick it! | DZone it! | del.icio.us | Comments (0) | Comment RSSRSS comment feed

Related posts

ReSharper User tip #2: Refactor rename namespaceHow to rename a namespace for all of its classes using ReSharper.The Humble dialog v.2 Update! Download source: TheHumbleDialog2.zip (29,96 kb) I've been working to get my head ...Driving out a correct implementation of ISO week numbers using TDD: Refactor Abstract So, now we make up another test. Looking at the calendar we see that new years eave 20...
Saving the comment

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading



 
Copyright © 2008 rickardnilsson.net