Questions tagged [magic-string]

A magic string is an input that a programmer believes will never come externally and which activates otherwise hidden functionality. A user of this program would likely provide input that gives an expected response in most situations. However, if the user does in fact innocently provide the pre-defined input, invoking the internal functionality, the program response is often quite unexpected to the user (thus appearing 'magical').

36 questions
63
votes
3 answers

What does "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" means in WebSocket Protocol

I don't understand the meaning of "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" in RFC 6455. Why does the server need this magic string? And why does the WebSocket protocol need this mechanism?
Hakju Oh
  • 731
  • 1
  • 5
  • 5
22
votes
4 answers

Compile Time Reflection in C#

I frequently write C# code that has to use magic strings to express property names. Everyone knows the problems with magic strings. They are very difficult to refactor, they have no compile time checking, and often they lead to hard-to-diagnose…
MgSam
  • 12,139
  • 19
  • 64
  • 95
10
votes
7 answers

Magic strings in ASP.NET MVC

I have a background in desktop software development and am getting started with learning ASP.NET MVC. In my default HomeController I have the Index action which has code that looks like this: if (!Request.IsAuthenticated) return…
Phil
  • 6,561
  • 4
  • 44
  • 69
9
votes
6 answers

.Net - Strategies to avoid magic string

In code at work, we have many uses of magic strings like the following code snippet: if (user.HasRight("Profile.View")) {...} So there are many places where we pass a string as a parameter to see if the user has a specific right. I don't like that…
Melursus
  • 10,328
  • 19
  • 69
  • 103
7
votes
1 answer

Identify implementations of base class in an array

I have the following problem: I have a set of engines which automaticly (listening to events) controls my model. The following picture shows in general the class diagram: Now I have a client which knows the EngineFacade and i want to set the…
Yggdrasil
  • 1,377
  • 2
  • 13
  • 27
6
votes
2 answers

Using a lambda expression to avoid using a "magic string" to specify a property

I am writing a service to take a collection of objects of a particular type and output its primitive, string, and DateTime types to a string in CSV Format. I have both of the below statements working. I find the the lambda based version to be much…
ahsteele
  • 26,243
  • 28
  • 134
  • 248
6
votes
2 answers

Decouple the screens without magic strings

My WPF project will be organised like this : Screens Group1 Screen1 View.xaml ViewModel.cs Group2 Screen2 View.xaml ViewModel.cs To show the Screen1 from the Screen2 I'll use something like…
Catalin DICU
  • 4,610
  • 5
  • 34
  • 47
6
votes
1 answer

ASP.net MVC Action URLs with lambda expression

I'm sure I have seen this syntax <%= Url.Action((MyController c) => c.MyMethod("a")) %> or something like it as a way to generate action URLs in ASP.net MVCs without magic strings. However, I can't find that Action overload. I have ASP.NET MVC 1.0.…
erikkallen
  • 33,800
  • 13
  • 85
  • 120
5
votes
1 answer

How can I remove magic strings from custom model binders?

I've written a couple of custom model binders now, and have realised that I've fallen into the trap of relying on magic strings, e.g.: if (bindingContext.ValueProvider.ContainsPrefix("PaymentKey")) { paymentKey =…
Paul Suart
  • 6,505
  • 7
  • 44
  • 65
5
votes
3 answers

An "elegant" way of identifying a field?

I'm writing a system that underlies programmer applications and that needs to detect their access to certain data. I can mostly do so with properties, like this: public class NiceClass { public int x { get; set; } } Then I go in and tweak the…
Alix
  • 927
  • 7
  • 21
5
votes
1 answer

Should I avoid magic strings as possible?

I have the next piece of code: internal static string GetNetBiosDomainFromMember(string memberName) { int indexOf = memberName.IndexOf("DC=", StringComparison.InvariantCultureIgnoreCase); indexOf += "DC=".Length; …
Marco Medrano
  • 2,530
  • 1
  • 21
  • 35
4
votes
2 answers

T4MVC for Web.config

The good thing about T4MVC is that it allows you to get rid of literal/magic strings. T4MVC is a T4 template for ASP.NET MVC apps that creates strongly typed helpers that eliminate the use of literal strings when referring the controllers,…
Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
3
votes
3 answers

ActionScript 3 object's property name to string?

I want to eliminate usage of magic strings in these: BindingUtils.bindProperty(obj1, "propertyName", obj2, ["childObj", "anotherProperty"]); or var ddl:DropDownList = new DropDownList(); ddl.labelField = "propertyName"; it would be sweet to just…
mizi_sk
  • 1,007
  • 7
  • 33
3
votes
2 answers

Getting magic strings out of QueryOver (or Fluent NHibernate perhaps)?

One of the many reason to use FluentNHibernate, the new QueryOver API, and the new Linq provider are all because they eliminate "magic string," or strings representing properties or other things that could be represented at compile time. Sadly, I am…
David Pfeffer
  • 38,869
  • 30
  • 127
  • 202
2
votes
3 answers

How do I avoid magic strings (PHP MVC Frameworks)

I want to use a MVC-framework together with PHP to make a nice separation between code and presentation. I've currently started to look at CakePHP. It looks nice, but whats about all those magic strings? Take a look at the code below (taken from the…
SanSaurus
  • 63
  • 4
1
2 3