Skip to content

Instantly share code, notes, and snippets.

@jpoehls
Created March 30, 2012 19:46
Show Gist options
  • Save jpoehls/2254399 to your computer and use it in GitHub Desktop.
Save jpoehls/2254399 to your computer and use it in GitHub Desktop.
Magic String Anti-Pattern

C#

Sometimes unavoidable, but always PAINFUL because you ruin your compiled language safety net.

  • INotifiyPropertyChanged - Avoidable with LINQ and smart caching for performance.
  • CompareAttribute - Unavoidable.
  • Any ORM - Unavoidable (unless you use F# 3.0 Type Providers).
  • Most reflection.

JavaScript

Basically unavoidable, and unnecessary to avoid since it is an interpreted language.

  • Almost all event/pub-sub libraries (event name is string).
  • Bracketed property access myObj["myProp"].
public class Person : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
NotifyPropertyChanged("Name"); // Shazaam!
}
}
public string Password { get; set; }
[Compare(OtherProperty = "Password")] // Abracadabra!
public string PasswordConfirmation { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment