Extending Visual Studio (part 1)

I love Visual Studio, always have. From the moment I wrote my first “Hello World!” in C, until becoming an expert C# developer.

With all due respect, however, there was always something missing. One user needs this, the other needs that. Of course, an IDE can’t have it all. The question is only whether an average user can extend VS to inlcude whatever is missing. This series of posts will try to fill the gaps, trying to answer this question: how to get into Visual Studio that which is missing. I will stick them all into a category named “Extending Visual Studio” for easy reference.

But before I begin, here’s a disclaimer – I am no expert in VS extensions; these posts are simply a collection of my experiences and adventures in the world of extending Visual Studio.

Before we are able to extend Visual Studio, we need the Visual Studio SDK. Install it and then continue reading!

So, you have the SDK? Great!

We’re done with technicalities, let’s create an extension!

Continue reading

3.Hours() + 5.Minutes() + 30.Seconds()

Today I devised an interesting abuse of the C# extension methods.

Take a look at these three methods:

public static TimeSpan Hours(this int count)
{
    return TimeSpan.FromHours(count);
}
public static TimeSpan Minutes(this int count)
{
    return TimeSpan.FromMinutes(count);
}
public static TimeSpan Seconds(this int count)
{
    return TimeSpan.FromSeconds(count);
}

With these methods, one coud do this:

TimeSpan timespan = 3.Hours() + 5.Minutes() + 30.Seconds();

Not many people would categorize this as very readable or useful, but it’s an interesting way to make code read like English.

Continue reading

Climb the curve

I’ve recently had a project in which an interesting challenge popped up.

Write a function that will round a timestamp to the closest 15min value.

Something like this:

challenge

Additional example:

For May 1 2015, 18:44:20.000 – round to May 1 2015, 18:45:00.
For May 1 2015, 18:07:30.000 – round to May 1 2015, 18:00:00.
For May 1 2015, 23:52:30.001 – round to May 2 2015, 00:00:00.

For this discussion, let’s allow the mid-points to be rounded to any (of the two) neighbouring 15min timestamps. The problem seems easy. And it is. But something strange happened.

I asked this question several people around the workplace. They were able to start with a sound idea in seconds (well, most of them). The thing that shocked me is that no two persons proposed the same solution. Every one of them had his own way.

So here are few of the first ideas:

Continue reading

Use .Any instead of .Count

I am amazed by how many developers use IEnumerable<T>.Count() when they should use IEnumerable<T>.Any().

How many times have you seen/written this:

if (collection.Count() > 0)
{
    // Now that we know we have some elements,
    // let's do something meaningful
}

There are (at least) 3 basic flaws with this approach:

1. Highly inefficient
2. Semantical implications
3. Infinite loops

Continue reading