Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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:

ASP.Net MVC:

Hello world route:

        routes.MapRoute(
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        );

What this means is that ASP.Net MVC will somehow look up HomeController in your code. Somehow make a new instance of it, and then call the function Index apparently with an id parameter of some sort. And then there are other things like:

RenderView("Categories", categories);
...or..
ViewData["Foobar"]="meh";

And then there are similar things with XAML as well. DataContext 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:

  MyData myDataObject = new MyData(DateTime.Now);      
  Binding myBinding = new Binding("MyDataProperty");
  myBinding.Source = myDataObject;

Although it relies more on casting and various magical runtime supports.

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?

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"?

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.

share|improve this question
10  
Pointless dynamism has been all the rage ever since our CPUs were fast enough to execute it. – DeadMG Feb 14 at 18:52
I can understand if you choose a dynamic language like Ruby, you can bet that types won't be deterministic and they'll be a lot of metaprogramming. The language was built for it and the framework will take advantage. However, for ASP.Net MVC for instance, it's designed to run on statically typed C#. Why must it use so much metaprogramming to actually work? You'd think a static framework for a static language just like every dynamic language usually has only dynamic frameworks – Earlz Feb 14 at 18:55
5  
How so? I've not seen any such examples with LINQ. – DeadMG Feb 14 at 19:00
5  
My humble guess is that the properly statically typed alternative is too freaking inconvenient. That's a theme that turns up a lot with most type systems really: "We'd like to do this in the type system, but it's not expressive enough." (Or the inverse: "We successfully expressed this in the type system, but it made things three times as complex.") @GlenH7 I'm not terribly familiar with all of LINQ, but the bits I've used don't exhibit anything even near what this post describes. Care to give an example? – delnan Feb 14 at 19:00
2  
@Earlz More importantly, anonymous objects are statically typed. There are no magic strings, the compiler knows the name and type of everything involved. Save for every other use of var. – delnan Feb 14 at 19:02
show 16 more comments

1 Answer

up vote 19 down vote accepted

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.

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.

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).

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.

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.

share|improve this answer
As far as XAML goes, you'd think they would've took the chance to improve it in WinRT since it's basically a complete rewrite anyway(which isn't API compatible). or everything else. What do you mean "routes are dynamic" though? I know you can't replace the URL pattern with some object graph, but what about the other parts? And as far as Entity Framework/Linq, I've experienced this. Very awesome being able to change your database and suddenly get compiler errors for the incompatibilities. – Earlz Feb 14 at 21:19
And I don't quite understand ".Net did not support static transversal of the object graph until the 3.5 release". Can you elaborate on what was missing? Generics arrived in 2.0 – Earlz Feb 14 at 21:21
XAML is verbose as it is...adding support for generics would make it even moreso (yes I know the whole XAML isn't made for humans Argument). Besides, I see XAML as more akin to HTML than an actual programming language. It shouldn't care about the type of the objects it is displaying, just how to display it. – Mike Brown Feb 14 at 21:22
1  
Generics arrived in 2.0, but we didn't get Expressions until 3.5 (with LINQ). Expressions are what power LINQ and the community has taken that power and run with it. The technique is known as static reflection – Mike Brown Feb 14 at 21:25
I would tend to agree, except for it really isn't. What really made XAML click for me is that it isn't just a markup language. It's a way of describing an object graph. Although I see your point for avoiding verbosity. – Earlz Feb 14 at 21:25
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.