When “using” obfuscates (C#)

I was working on some code today for a intermediate service (internal WCF service that gets answers from another service and simplifies the output data model). Essentially, a public API service calls my service, which calls a BEA service, etc.

In order to understand the paging logic quickly, I examined some code from a previous developer. I found the following code to handle paging:

var reqPage = new MultiplePagingData {pageKey = count};
req.pagingData = reqPage;

Fairly simple. I create a MultiplePagingData object and add the pageKey and attach to the request object. Since much of the previous work was over simplified, I decided to take a look at MultiplePagingData and ensure I was not missing anything. Right click the line and it goes to the PagingData class. WTF? Due to my insanity, I tried this a few times, just to be sure (insanity being trying the same thing over and over and expecting different results).

Try from the other end? Okay, let’s find usings. And then the problem hit me. It was in the “using” statements. The developer was aliasing a class called PagingData.

using MultiplePagingData = Company.Project.Proxy.ServiceName.PagingData;

Ouch! Looks like something else to write up the code review.

Using is a wonderful keyword. It allows you to avoid fully qualifying all of your objects in your code, which greatly speeds up development. You see it at the top of every C# file:

using System;
using System.Collections.Specialized;
using System.Configuration;

It also serves to alias, with the intent being on creating short, easy to type keywords for namespaces. The primary reason for using the alias is when you have two libraries that contain classes with the same name. For example, suppose I have a customer object in two namespaces:

using customer1 = SomeCompany.Project.Subproject.Domain.Models;
using customer2 = SomeCompany.Project.Subproject.Domain.Models;

I then save a lot of typing, by specifying the customer like so:

customer1.Customer c = new customer1.Customer();

My searching now tells me it is perfectly legal to actually alias the class and call it whatever you want. In my brief testing in .NET 4.0, I have been unable to recreate this nightmare, however. If I had more time, I would play a bit and figure it out.

Now, perhaps someone has a good reason for aliasing an actual class. If so, I would love to hear it, as the code I am looking at merely obfuscates the code.

Peace and Grace,

Twitter: @gbworld


One Response to When “using” obfuscates (C#)

  1. Sean Kenney says:

    I strongly agree with your post. I have enough abstractions in my technical world, I do not need another just for obfuscation. 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: