Click here to Skip to main content
Click here to Skip to main content
Technical Blog

No More Magic Strings! Presenting: @string.of

, 23 Nov 2010 Ms-PL
Rate this:
Please Sign up or sign in to vote.
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).
Follow on   Twitter   Google+

Comments and Discussions

 
GeneralMy vote of 5 Pinmemberzenwalker198524-Oct-11 4:55 
GeneralNice trick! PinmemberKarby29-Nov-10 13:48 
GeneralMy vote of 5 PinmemberEmile van Gerwen23-Nov-10 22:04 
GeneralMy vote of 4 Pinmemberaspdotnetdev23-Nov-10 11:52 
GeneralMy vote of 3 Pinmemberaspdotnetdev23-Nov-10 11:49 
QuestionPerformance Impact? PinmemberSledgeHammer0123-Nov-10 6:00 
GeneralMy vote of 1 PinmemberJalalAldeen23-Nov-10 5:09 
GeneralMy vote of 1 Pinmemberaspdotnetdev23-Nov-10 11:53 
JokeRe: My vote of 1 Pinmembersjelen30-Nov-10 2:18 
QuestionAny solution for attribute parameter? PinmemberI'm Chris22-Nov-10 22:19 
AnswerRe: Any solution for attribute parameter? PinmemberArik Poznanski23-Nov-10 4:50 
GeneralHalf full... PinmemberOleg Shilo22-Nov-10 13:37 
GeneralRe: Half full... PinmemberArik Poznanski23-Nov-10 4:57 
GeneralRe: Half full... PinmemberMenelaos Vergis1-Jun-12 2:53 
GeneralMy vote of 2 Pinmemberpzkpfw22-Nov-10 11:05 
GeneralMy vote of 3 Pinmembertec-goblin22-Nov-10 7:05 
GeneralRe: My vote of 3 PinmemberArik Poznanski22-Nov-10 8:13 
GeneralFrustrating! PinmemberDarchangel22-Nov-10 5:47 
GeneralRe: Frustrating! PinmemberArik Poznanski22-Nov-10 7:57 
GeneralRe: Frustrating! PinmemberDarchangel22-Nov-10 8:12 
GeneralRe: Frustrating! PinmemberArik Poznanski22-Nov-10 8:18 
GeneralRe: Frustrating! PinmemberDarchangel22-Nov-10 8:21 
GeneralRe: Frustrating! PinmemberArik Poznanski22-Nov-10 8:30 
GeneralRe: Frustrating! PinmemberArik Poznanski22-Nov-10 8:34 
GeneralNice replacement for lack of C++ preprocessor PinmemberNeil A. Harding22-Nov-10 5:44 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141216.1 | Last Updated 23 Nov 2010
Article Copyright 2010 by Arik Poznanski
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid