Click here to Skip to main content
14,269,309 members

No More Magic Strings! Presenting: @string.of

Rate this:
4.63 (31 votes)
Please Sign up or sign in to vote.
4.63 (31 votes)
23 Nov 2010Ms-PL
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

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

    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:

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.

License

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

Share

About the Author

Arik Poznanski
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

 
GeneralRe: Nice replacement for lack of C++ preprocessor Pin
Wolfgang_Baron26-Nov-10 12:17
memberWolfgang_Baron26-Nov-10 12:17 
GeneralMy vote of 4 Pin
Graham Downs22-Nov-10 4:09
memberGraham Downs22-Nov-10 4:09 
GeneralMy vote of 1 Pin
Dmitri Nеstеruk20-Nov-10 10:17
memberDmitri Nеstеruk20-Nov-10 10:17 
GeneralRe: My vote of 1 Pin
HaBiX21-Nov-10 21:47
memberHaBiX21-Nov-10 21:47 
GeneralRe: My vote of 1 Pin
cwienands22-Nov-10 1:21
membercwienands22-Nov-10 1:21 
GeneralRe: My vote of 1 Pin
Dmitri Nеstеruk22-Nov-10 1:24
memberDmitri Nеstеruk22-Nov-10 1:24 
GeneralRe: My vote of 1 Pin
jim lahey22-Nov-10 8:30
memberjim lahey22-Nov-10 8:30 
GeneralRe: My vote of 1 Pin
Arik Poznanski22-Nov-10 7:03
memberArik Poznanski22-Nov-10 7:03 
You can't have the cake and eat it too.
Either this is known by everyone, hence you know the implementation details, or not!

I agree this trick is widely known, this is why I only mentioned it uses expression trees for the implementation.
Still, I haven't found over the web a complete implementation with nice syntax, so I generated one myself.

Also, please note this code-project "article" is actually a blog post.
Given that, I think I provided enough details.
Arik Poznanski

GeneralMy vote of 5 Pin
jim lahey17-Nov-10 22:00
memberjim lahey17-Nov-10 22:00 
GeneralMy vote of 5 Pin
muhahahahahahaa17-Nov-10 21:38
membermuhahahahahahaa17-Nov-10 21:38 
GeneralMy vote of 4 Pin
SledgeHammer0117-Nov-10 13:14
memberSledgeHammer0117-Nov-10 13:14 
GeneralRe: My vote of 4 Pin
jgauffin20-Nov-10 9:39
memberjgauffin20-Nov-10 9:39 
GeneralRe: My vote of 4 Pin
Arik Poznanski22-Nov-10 6:59
memberArik Poznanski22-Nov-10 6:59 
GeneralRe: My vote of 4 Pin
SledgeHammer0123-Nov-10 4:51
memberSledgeHammer0123-Nov-10 4:51 
GeneralRe: My vote of 4 Pin
Arik Poznanski23-Nov-10 4:53
memberArik Poznanski23-Nov-10 4:53 

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.

Technical Blog
Posted 17 Nov 2010

Tagged as

Stats

69.4K views
23 bookmarked