I’m writing another MVC3 app. I’m in the same world of pain with respect to magic strings and anonymous classes. I don’t like it here.

This is a method signature that keeps tripping me up:

Screenshot

But surely there are other overloads than that? Well, yes, and this is the list of them:

Screenshot

One of the major benefits of a strongly-typed language is not having everything dynamically typed - and that’s effectively what’s happening here, with string as a substitute for the dynamic type.

When we’re in a world of magic-string pain, the first thing to do is generally start creating some conventions. The second thing, of course, is that we test those conventions using unit tests. Hold on a second, though: we’re using a strongly-typed language and yet we’re writing unit tests to test our conventions because we’re using strings and anonymous classes? Why don’t we have a tool that does this for us?

We want a convention-based test harness that:

  1. Runs on every build.
  2. Has a set of conventions that are clear and unambiguous.
  3. Doesn’t make us manually write tests.
  4. Will auto-generate test cases for every new piece of code we write.
  5. Is refactoring-friendly.
  6. Is fast.
  7. Will fail the build if something’s wrong.

I think we’ve forgotten something important. Can anyone point to a tool that’s all of the above, comes pre-installed with every version of Visual Studio and requires zero integration work, no NuGet packages and just works?

Anyone? Anyone? No? Here’s one: csc.exe. Yep, that’s right: use the compiler.

Call me old-school, but a compiler is all of the above. Consider this method:

Screenshot

Think about it: why don’t I have to test that a and b are integers? Sure, I should be testing for edge cases here, but I don’t have to type-check or null-check my inputs. Why not? Because the compiler is enforcing the convention that when I say “int”, I mean a 32-bit integer and it simply won’t let anyone pass in anything else.

I don’t have to write a single unit test to enforce these conventions. The compiler will provide me with fast and reliable feedback - at compile time - if I’ve broken anything, which is far better than getting the feedback at run-time using unit tests (or worse, at run-time when a user hits an issue).

I think we as developers can afford to take a bit more time to write strongly-typed code, e.g. HtmlHelpers for controller actions. Try this one:

Screenshot

You can make your code infer the controller name, the action name, the area and all sorts of things without ever having a magic string. You could even add strongly-typed parameters to it (built using a fluent interface or via tiny types) so that it’s effectively impossible to get it wrong without the compiler complaining.

So why don’t more people use such a great convention-based test tool? I have no idea.

Let’s make old-school cool again.