Friday, December 21, 2012

Using Dynamic Instead of Type Casting

Yesterday I was writing some NUnit tests for an MVC controller. My test was making sure that a property on the model being placed in the view had a property set correctly:

   1:  var sut = new MyController(fakeProvider);
   2:  var result = (ViewResult) sut.Index(id);
   3:  var model = result.Model;
   4:  Assert.That(model.MyProperty, Is.EqualTo(expectedValue);

Often type casting can be full of syntactic noise. I could refactor that code to remove a line:

   1:  var sut = new MyController(fakeProvider);
   2:  var model = ((ViewResult) sut.Index(id)).Model;
   3:  Assert.That(model.MyProperty, Is.EqualTo(expectedValue);

All those parenthesis on line two are like fingernails on a chalkboard to me. Luckily I was having a problem with the razor view when I was trying to preview the controller that resulted in me hacking away at my code until it looked like:

   1:  var sut = new MyController(fakeProvider);
   2:  dynamic result = sut.Index(id);
   3:  Assert.That(result.Model.MyProperty, Is.EqualTo(expectedValue);

I didn’t even notice until this morning when working on another action in the controller that I had removed the need for casting my ActionResult to a ViewResult in my test. What a happy accident. I suspect that the compiler is doing the type casting for me under the covers, but I leave ILDASM to @vcsjones, so I’ll never know for sure.

I am filing that little tidbit in my bag of tricks and will definitely be using that in the future.

Tuesday, December 4, 2012

Mani-Pedis with Kyro

I just finished reading a well written article "Why I'm No Metrosexual" Kyro Beshay in which he laments the lack of skuomorphism (or at least a beveled button) in Windows 8.

The foundation of his argument is that we need this visual cues to tell us how to interact with an interface. I find this argument without merit in as much as I'm sure Kyro has managed to navigate his way around the internet using hyperlinks and not just buttons. Nothing in the physical world would translate an underlined word as something to interact with, yet billions of hyperlinks are clicked each day.

Then how do we know to click on these underlined words? Someone taught us. There was probably a time in each of our lives when we first looked at an HTML hyperlink and never thought to click the word. Then one day we saw someone else browsing a page, and they clicked a word and it took them to another part of the page, or a whole new document. From then on we looked for clues that the words in the text we were reading would take us to some other, releated content.

In recent months, or in the coming months, you will be introduced to the new Windows Start screen. "Can I click on on those tiles?" Kyro asks. This is as valid a qeustion as, "Can I click on those words?"  It takes only a few seconds to be taught that every tile on the Start screen will open an application for you, and just a few minutes more to learn that the live tiles are infinately more helpful in providing information, even to the point of preventing you from needing to launch the application in the first place. Why can't a button also be a billboard?

So Kyro, why not come out with me for a mani-pedi, and I'll teach you the visual language of Windows 8. Its really not that hard.
Follow me on