Click here to Skip to main content
15,879,535 members
Articles / Desktop Programming / WPF

No More Magic Strings! Presenting: @string.of

Rate me:
Please Sign up or sign in to vote.
4.63/5 (31 votes)
23 Nov 2010Ms-PL1 min read 78.9K   23   41
Implementing a "stringof" operator using expression trees
A Technical Blog article. View entire blog here.

The Problem

How many times have you seen the following code snippets?

  1. Checking method parameters

    C#
    if (executeMethod == null)
    {
        throw new ArgumentNullException("executeMethod");
    }
  2. Implementing a property in a WPF / SL view-model

    C#
    public double Size
    {
        get { return _size; }
        set
        {
            _size = value;
            RaisePropertyChanged("Size");
        }
    }

The first time I had to wrote code like this I felt uneasy. Hardcoded strings are bad practice and should be rarely used.

Even worse, using a hardcoded string of an identifier is just a bug waiting to happen.

Consider what happens when you need to refactor your code and change the name of the Size property (second example) to something different like MaxSize. The refactoring tools will not change the hardcoded string "Size" and now your WPF application stops notifying on property change the way it should!

These bugs are very difficult to spot. No compile time error, no runtime error. Nothing. The only way to spot this bug is by testing this specific functionality.

I’ve checked a small WPF application and found 158 (!) instances of these almost-bugs.

The Solution

Are you familiar with the typeof operator? Don’t you just loved it if there was a stringof operator? Well, there isn’t! So I’ve implemented the closest thing: @string.of().

To use it simply write:

C#
string s = @string.of(() => Size);

Since the implementation uses expression trees the syntax might look strange, but as I’ve said, that’s the closest you can get without being a member of the C# development team.

Here you can find a full example that uses the @string.of operator on various inputs: local variable, parameter, property, field and function.
(CodeProject users can easily download the demo project from the attached link)

That’s it for now,
Arik Poznanski.

This article was originally posted at http://feeds.feedburner.com/ArikPoznanskisBlog

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer (Senior) Verint
Israel Israel
Arik Poznanski is a senior software developer at Verint. He completed two B.Sc. degrees in Mathematics & Computer Science, summa cum laude, from the Technion in Israel.

Arik has extensive knowledge and experience in many Microsoft technologies, including .NET with C#, WPF, Silverlight, WinForms, Interop, COM/ATL programming, C++ Win32 programming and reverse engineering (assembly, IL).

Comments and Discussions

 
Questionsimpler Pin
stixoffire26-Aug-15 4:07
stixoffire26-Aug-15 4:07 
GeneralMy vote of 5 Pin
zenwalker198524-Oct-11 3:55
zenwalker198524-Oct-11 3:55 
GeneralNice trick! Pin
Karby29-Nov-10 12:48
Karby29-Nov-10 12:48 
GeneralMy vote of 5 Pin
Emile van Gerwen23-Nov-10 21:04
Emile van Gerwen23-Nov-10 21:04 
GeneralMy vote of 4 Pin
AspDotNetDev23-Nov-10 10:52
protectorAspDotNetDev23-Nov-10 10:52 
GeneralMy vote of 3 Pin
AspDotNetDev23-Nov-10 10:49
protectorAspDotNetDev23-Nov-10 10:49 
QuestionPerformance Impact? Pin
SledgeHammer0123-Nov-10 5:00
SledgeHammer0123-Nov-10 5:00 
GeneralMy vote of 1 Pin
JalalAldeen23-Nov-10 4:09
JalalAldeen23-Nov-10 4:09 
GeneralMy vote of 1 Pin
AspDotNetDev23-Nov-10 10:53
protectorAspDotNetDev23-Nov-10 10:53 
JokeRe: My vote of 1 Pin
sjelen30-Nov-10 1:18
professionalsjelen30-Nov-10 1:18 
QuestionAny solution for attribute parameter? Pin
I'm Chris22-Nov-10 21:19
professionalI'm Chris22-Nov-10 21:19 
AnswerRe: Any solution for attribute parameter? Pin
Arik Poznanski23-Nov-10 3:50
Arik Poznanski23-Nov-10 3:50 
Unfortunately no.
This is why we need support for a real stringof operator which works at compile time.
Arik Poznanski

GeneralHalf full... Pin
Oleg Shilo22-Nov-10 12:37
Oleg Shilo22-Nov-10 12:37 
GeneralRe: Half full... Pin
Arik Poznanski23-Nov-10 3:57
Arik Poznanski23-Nov-10 3:57 
GeneralRe: Half full... Pin
Menelaos Vergis1-Jun-12 1:53
Menelaos Vergis1-Jun-12 1:53 
GeneralMy vote of 2 Pin
pzkpfw22-Nov-10 10:05
pzkpfw22-Nov-10 10:05 
GeneralMy vote of 3 Pin
tec-goblin22-Nov-10 6:05
tec-goblin22-Nov-10 6:05 
GeneralRe: My vote of 3 Pin
Arik Poznanski22-Nov-10 7:13
Arik Poznanski22-Nov-10 7:13 
GeneralFrustrating! PinPopular
Darchangel22-Nov-10 4:47
Darchangel22-Nov-10 4:47 
GeneralRe: Frustrating! Pin
Arik Poznanski22-Nov-10 6:57
Arik Poznanski22-Nov-10 6:57 
GeneralRe: Frustrating! PinPopular
Darchangel22-Nov-10 7:12
Darchangel22-Nov-10 7:12 
GeneralRe: Frustrating! Pin
Arik Poznanski22-Nov-10 7:18
Arik Poznanski22-Nov-10 7:18 
GeneralRe: Frustrating! Pin
Darchangel22-Nov-10 7:21
Darchangel22-Nov-10 7:21 
GeneralRe: Frustrating! Pin
Arik Poznanski22-Nov-10 7:30
Arik Poznanski22-Nov-10 7:30 
GeneralRe: Frustrating! Pin
Arik Poznanski22-Nov-10 7:34
Arik Poznanski22-Nov-10 7:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.