Why does the .Net world seem to embrace magic strings instead of staticly typed alternatives? - Software Engineering Stack Exchangemost recent 30 from softwareengineering.stackexchange.com2019-09-15T11:20:44Zhttps://softwareengineering.stackexchange.com/feeds/question/187126https://creativecommons.org/licenses/by-sa/4.0/rdfhttps://softwareengineering.stackexchange.com/q/18712647Why does the .Net world seem to embrace magic strings instead of staticly typed alternatives?Earlzhttps://softwareengineering.stackexchange.com/users/4832013-02-14T18:33:04Z2013-02-14T21:04:06Z
<p>So, I work in .Net. I make open source projects in .Net. One of my biggest problems with it isn't necessariyl with .Net, but with the community and frameworks around it. It seems everywhere that magical naming schemes and strings is treated as the best way to do everything. Bold statement, but look at it: </p>
<p>ASP.Net MVC: </p>
<p>Hello world route:</p>
<pre><code> routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
</code></pre>
<p>What this means is that ASP.Net MVC will somehow look up <code>HomeController</code> in your code. Somehow make a new instance of it, and then call the function <code>Index</code> apparently with an <code>id</code> parameter of some sort. And then there are other things like:</p>
<pre><code>RenderView("Categories", categories);
...or..
ViewData["Foobar"]="meh";
</code></pre>
<p>And then there are similar things with XAML as well. <code>DataContext</code> is treated as an object and you have to hope and pray that it resolves to the type you want. DependencyProperties must use magic strings and magic naming conventions. And things like this:</p>
<pre><code> MyData myDataObject = new MyData(DateTime.Now);
Binding myBinding = new Binding("MyDataProperty");
myBinding.Source = myDataObject;
</code></pre>
<p>Although it relies more on casting and various magical runtime supports. </p>
<p>Anyway, I say all that to end up here: Why is this so well tolerated in the .Net world? Aren't we using statically typed languages to almost always know what the type of things are? Why is reflection and type/method/property/whatever names(as strings) prefered so much in comparison to generics and delegates or even code generation? </p>
<p>Are there inherit reasons that I'm missing for why ASP.Net's routing syntax relies almost exclusively on reflection to actually resolve how to handle a route? I hate when I change the name of a method or property and suddenly things break, but there don't appear to be any references to that method or property and there are of course no compiler errors. Why was the apparent convenience of magic strings considered "worth it"? </p>
<p>I know there are also commonly statically typed alternatives to some things, but they usually take a backseat and seem to never be in tutorials or other beginner material. </p>
https://softwareengineering.stackexchange.com/questions/187126/-/187137#18713731Answer by Michael Brown for Why does the .Net world seem to embrace magic strings instead of staticly typed alternatives?Michael Brownhttps://softwareengineering.stackexchange.com/users/131812013-02-14T21:04:06Z2013-02-14T21:04:06Z<p>Actually there is a push back in the .NET world against these very things you mentioned. In the first example you gave however, the routing engine is given a convention for mapping the default route. The very fact that the routes are dynamic make it nigh impossible to use a static configuration.</p>
<p>You also mention XAML/WPF, both of which were under development well before generics were introduced into .NET and going back to support generics would have delayed an already very late product (Longhorn/Vista) even further.</p>
<p>There are examples within the ASP.NET MVC framework of using lambda expressions in place of magic strings and the Entity Framework/LINQ takes it even further where the language and framework provides native support for composing SQL queries over a static object graph (instead of constructing magic SQL strings, you get compile time validation of your queries). </p>
<p>For other examples of static configuration see structuremap and other modern dependency injection containers, and other frameworks that need to inspect the object graph at runtime but allow the developer to statically provide hints using lambda expressions.</p>
<p>So the short answer is that historically, .NET did not support static traversal of an object graph until the 3.5 release. Now that we have it, many developers prefer it over magic strings and many have been pushing for even deeper support such as a symbolOf operator that works similar to the typeOf operator.</p>