Custom Visual Studio Extension Feeds

I found this difficult to figure out so I thought I would write something up real quick. I recently created a Visual Studio extension for the team I am currently working with. The extension isn’t generally useful but it is useful for my team, so I didn’t want to deploy it to the public extension gallery. One of the things I wanted to figure out was how to distribute the extension to the people on my team so that they just get automatic updates like every other extension.

It turns out all you have to do is to make the extension available via http then create an ATOM feed for your extensions. I just dropped a static atom.xml file onto a webserver on my backup dev machine and update it manually when I get a new build of the extension and presto! You just give your team the URL and they can configure VS to also look at that URL when searching for extensions.

Here is the structure of the atom XML you need to create in your feed to make it work:

<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title type="text"></title>
  <id>uuid:7d461a9c-df42-4fc3-bb84-b83c5147e145;id=1</id>
  <updated>2012-11-06T22:19:45Z</updated>
  <entry>
    <id>2bde3d56-7ac0-4d8d-8ae8-794b631b0b27</id>
    <title type="text">Example Extension</title>
    <summary type="text">An example of hosting a VS extension for my team.</summary>
    <published>2012-11-02T10:43:32-05:00</published>
    <updated>2012-11-06T11:31:29-06:00</updated>
    <author>
      <name>Microsoft</name>
    </author>
    <content type="application/octet-stream" src="Example.vsix" />
    <Vsix xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/developer/vsx-syndication-schema/2010">
      <Id>2bde3d56-7ac0-4d8d-8ae8-794b631b0b27</Id>
      <Version>1.2</Version>
      <References>Visual Studio MPF 11.0\Microsoft.VisualStudio.MPF.11.0\[11.0,);</References>
      <Rating xsi:nil="true" />
      <RatingCount xsi:nil="true" />
      <DownloadCount xsi:nil="true" />
    </Vsix>
  </entry>
</feed>

Test Budgeting

I ran across this concept in an internal discussion group a while back and thought it would be worth sharing. Arlo Belshee is the coiner of the phrase as far as I know. The idea is simply that you come up with a threshold of time that you find acceptable for your tests to run in. One which allows you to still maintain a TDD workflow. Below is Arlo’s recommended test budget.
Test Budget
  1. Up to 3 tests that take longer than 2 seconds.
  2. Up to 100 tests that take longer than .02 seconds.
  3. Infinite tests that take less than .02 seconds.
I also think that you should have a ratio of the three, #1 should not comprise more than 1% of your tests and #2 should not be more than 10% of your tests. The idea is that as you’re writing unit tests you need to design your code in such a way so that it is easily testable in order to meet your test budgets. If you’re writing slow tests early you end up hitting a wall and the friction caused by the tests degrades the value proposition of TDD.

This is WAT makes TypeScript good

By now I’m sure you have all seen the hilarious WAT video, but if not here it is for your viewing pleasure: https://www.destroyallsoftware.com/talks/wat

The very first question that the speaker Gary Bernhardt poses (after the Ruby parts) is that of what is expected when you execute the following javascript:

[ ] + [ ]

He says in that video that he would expect an empty array to result or he would also accept a Type error. But what you actually get when this is run is an empty string. Then we all WAT and laugh. He shows a few other examples of questionable things that javascript will do without any problems whatsoever.

So lets take this famous WAT-inducing example over to the TypeScript playground and lets see what happens…

Type error! So this is wat makes TypeScript good :)

Maybe with Linq

Not long ago I wrote a post on using Maybe not null. In that post however I did not provide an implementation of the Maybe monad. There are a few floating around the internet (including the Maybe package on nuget) but it’s fun and I think it’s worth showing my own just for the sake of it. Here is my simplified version of the Maybe monad in C#:

Download on skydrive

using System;

namespace System.Monads
{
public interface IMaybe<T>
{
bool HasValue { get; }
T Value { get; }
}

public class Maybe<T> : IMaybe<T>
{
public static readonly Maybe<T> Nothing = new Maybe<T>();

public bool HasValue { get; private set; }
public T Value { get; private set; }

private Maybe()
{
HasValue = false;
Value = default(T);
}

internal Maybe(T value)
{
HasValue = !object.Equals(value, null);
Value = value;
}

public static implicit operator Maybe<T>(T value)
{
return new Maybe<T>(value);
}
}

public static class Maybe
{
public static IMaybe<T> ToMaybe<T>(this T value)
{
IMaybe<T> maybe = value as IMaybe<T>;
if (maybe != null)
return maybe;

return new Maybe<T>(value);
}

// Monadic Bind operator
public static IMaybe<TResult> Bind<TSource, TResult>(this IMaybe<TSource> m, Func<TSource, IMaybe<TResult>> f)
{
if (!m.HasValue)
return Maybe<TResult>.Nothing;

return f(m.Value);
}

public static IMaybe<TResult> Select<TSource, TResult>(this IMaybe<TSource> m, Func<TSource, TResult> f)
{
return m.Bind(x => f(x).ToMaybe());
}

public static IMaybe<TResult> SelectMany<TSource, TResult>(this IMaybe<TSource> m, Func<TSource, TResult> f)
{
return m.Bind(x => f(x).ToMaybe());
}

public static IMaybe<TResult> SelectMany<TSource, TMaybe, TResult>(this IMaybe<TSource> m, Func<TSource, IMaybe<TMaybe>> f, Func<TSource, TMaybe, TResult> g)
{
return m.Bind(x => f(x).Bind(y => g(x, y).ToMaybe()));
}

// Simplified form, check for null or use Nullable<T>.HasValue instead of Maybe.
// This form is not as strictly correct but it results in a more useable syntax in C#.
public static TResult Select<TSource, TResult>(this TSource m, Func<TSource, TResult> f)
{
return m.ToMaybe().Bind(x => f(x).ToMaybe()).Value;
}

public static TResult SelectMany<TSource, TResult>(this TSource m, Func<TSource, TResult> f)
{
return m.ToMaybe().Bind(x => f(x).ToMaybe()).Value;
}

public static TResult SelectMany<TSource, TMaybe, TResult>(this TSource m, Func<TSource, TMaybe> f, Func<TSource, TMaybe, TResult> g)
{
return m.ToMaybe().Bind(x => f(x).ToMaybe().Bind(y => g(x, y).ToMaybe())).Value;
}
}
}

It’s a pretty simple implementation, I tried to stay true to it’s monadic nature by creating the Bind method with the correct function signature but I also created a number of Select and SelectMany functions which allow you to use Linq syntax to bind to your Maybe monad.
 
I also created a few non-maybe functions which allow you to bind to regular objects without needing to wrap it in a maybe object. If your object is a value type then it will never be null anyway and if you use a reference type (including Nullable<T>) then you do a null check on the result instead of HasValue check. Strictly speaking that isn’t the purest version of a Monad but with the syntax limitations of C# I think it results in the cleanest usage possible. It’s a reasonable compromise.
 
Here are some examples of how you can use it:
// Most verbose form, most strictly correct
var m1 = 5
.ToMaybe()
.Select(x => x + 10);

// prettier but result will be of type int
var m2 = from x in 5
select x + 10;

// if you use nullable structs, it's pretty and result will be HasValue=false if anything is null
var m3 = from x in (int?)5
from y in (int?)10
select x + y;

// identical to above except the long form, with a null
int? five = 5;
int? ten = null;
var m4 = five.Select(x => ten.SelectMany(y => x + y));

// Pretty form for reference types, check for null on result.
var m5 = from x in "testing"
from y in (string)null
select x + y;

// Maybe form of above, check for HasValue
var m6 = from x in "testing".ToMaybe()
from y in ((string)null).ToMaybe()
select x + y;

// Longer select chains
var m7 = from x in new Example()
from y in x.Inner
from z in y.Inner
select z.Value;

var m8 = from x in new Example { Inner = new Example { Inner = new Example { Value = "success!" } } }
from y in x.Inner
from z in y.Inner
select z.Value;

Console.WriteLine("m1: {0}->{1}", m1.HasValue, m1.Value);
Console.WriteLine("m2: True->{0} (non-nullable)", m2);
Console.WriteLine("m3: {0}->{1}", m3.HasValue, m3.Value);
Console.WriteLine("m4: {0}", m4.HasValue);
Console.WriteLine("m5: {0}", m5 != null);
Console.WriteLine("m6: {0}", m6.HasValue);
Console.WriteLine("m7: {0}", m7 != null);
Console.WriteLine("m8: {0}->{1}", m8 != null, m8);
Console.ReadKey(true);