Monthly Archives: November 2009

Dynamic objects and ReSharper

 

As you might have heard by now, C# 4.0 (or is it just 4?…) comes with a new keyword: dynamic. This means that you could do something like the following:

image

Simply put, ExpandoObject is a class that allows you to add and remove members at runtime. This allows us to call methods that are resolved at runtime. As such, the previous code will compile.

Just as you can declare methods, you can also declare properties:

image

This no doubt can come in handy when working with ViewModels and ASP.NET MVC.

However, there is one minor problem with dynamic objects: you lose intellisense, which means that if in your view, instead of typing dynaCustomer.FirstName, you type dynaCustomer.FristName, you won’t get any errors until you run the app.

image

And that’s where ReSharper can help:

image

This is the same code, but with ReSharper activated inside Visual Studio 2010! I typed the FirstName property for the first time. After that, I have full intellisense support for it. The same would apply to methods:

image

That puts dynamic into perspective.

Getting your OSS binaries with Horn

 

Recently Billy McCafferty wrote a post on Horn, a package manager that Paul Cowan and Dave the Ninja have developed, similar to the idea of Ruby’s Gem. Although it’s a step in the right direction, it does have issues. To be able to use it, you need to download Horn and build it. But not only that, you also need to install Subversion, Git, Powershell, Rake, PSake, Cake, Fake…and a whole slew of SCM’s and build tools. Why? Because Horn builds from source. As such, it needs to access the SCM a specific project uses, download the files locally and run the build script. Since OSS projects are free to choose what build script they use, that also adds to the complexity. Some are happy with MSBuild, which isn’t normally an issue since it’s part of every .NET install. However, others use Rake, which in turn requires Ruby. You get the picture…

Although this isn’t necessarily an issue for some people, for others, it is. Forgetting for a moment scenarios such as those imposed by IT departments on corporate networks (permissions to install software, firewall, etc..), there are many that are not too comfortable with building from source, something beyond running an MSBuild file or compiling a VS solution inside the IDE.

 

Welcome to Horn Server-Side

In order to ease some of these concerns, the Horn team developed a server-side solution. A service now takes care of figuring out all dependencies and building packages and since it does this on a scheduled basis, it saves you the time of having to wait for a project to be built. This also saves you from having to install anything on your machine, including SCM’s and build tools. In fact, you don’t need to install anything if you don’t want to. For you to interact with this server, Dave built an ASP.NET MVC application that shows all the OSS packages in their respective categories. For each package there is normally the official released version and the trunk. iMeta has gracefully offered to host the server up in clouds. Go see for yourself.

If you prefer a command line version, I hacked one together in a morning and it’s available from the Horn repository. This allows you to issue commands like:

horn-get –u http://hornget.net –c orm –d nhibernate-2.1

which will download NHibernate 2.1 zip for you with the necessary binaries.

 

Take a look, experiment and provide feedback to the Horn team. There is still room for improvement, but no doubt, Horn is helping remove some of the barriers in the adoption of Open Source.

var improves readability

 

Countless times I’ve heard the argument that you should use the var keyword with caution, that it decreases readability of your code, or how it can be misused.

The example given in the linked post is:

    var Data = GetData();

 

According to the blog post, GetData returns a DataTable, something not inherently apparent. The problem however is due to the naming conventions used by the developer.

Firstly, GetData is a method indicating that it returns Data. The problem is, pretty much anything is Data. There has to be a more precise definition of what it is the method is actually doing. By this, I don’t mean you should necessarily rename the method to GetDataTable, since this doesn’t help. This just indicates what type it is returning, not what the method is doing. It would be an appropriate name if the domain of the problem were somehow about types, but in a business scenario, it doesn’t provide much value.

The second issue and just as important, is the variable name, data. Again, it is not descriptive enough. What is data? What kind of information does it hold? Is it a car? Is it many cars?

By using var, you are forcing yourself to think more about how you name methods and variables, instead of relying on the type system to improve readability, something that is more an implementation detail. Using the var keyword is not about being lazy, quite the contrary.