If-then-else as lambda expressions

It’s visual sugar, but I like it better than:

if (foo != null)
{
  foo.bar();
}

Here’s the extension methods:

public class Foo
{
	public void Print() { Console.WriteLine("Hello World!"); }
}

public static class LambdaIfThenElse
{
	public static bool IsNull(this T obj)
	{
		return obj == null;
	}

	public static bool IsNull(this T obj, Action action)
	{
		bool ret = obj == null;

		if (ret) action();

		return ret;
	}

	public static bool IsNotNull(this T obj)
	{
		return obj != null;
	}

	public static bool IfNotNull(this T obj, Action action)
	{
		bool ret = obj != null;

		if (ret) action(obj);

		return ret;
	}

	public static bool Then(this bool b, Action f)
	{
		if (b) { f(); }

		return b;
	}

	public static bool Else(this bool b, Action f)
	{
		if (!b) { f(); }

		return !b;
	}

	public static bool And(this bool b, Action f)
	{
		if (b) { f(); }

		return b;
	}
}

Here’s a usage example:

class Program
{
	static void Main(string[] args)
	{
		(1 == 1).Then(() => Console.WriteLine("True")).Else(() => Console.WriteLine("False"));

		(1 == 1).Then(() => Console.Write("True"))
			.And(() => Console.WriteLine(" and also"))
			.Else(() => Console.Write("False"))
			.And(() => Console.WriteLine(" and more so"));

		(1 == 2).Then(() => Console.Write("True"))
		        .And(() => Console.WriteLine(" and also"))
			.Else(() => Console.Write("False"))
			.And(() => Console.WriteLine(" and more so"));

		Foo foo = new Foo();
		foo.IfNotNull(f => f.Print()).Else(() => Console.WriteLine("Goodbye World!"));

		Foo foo2 = null;
		foo2.IfNotNull(f => f.Print()).Else(() => Console.WriteLine("Goodbye World!"));
	}
}

And here’s the output:

True
True and also
False and more so
Hello World!
Goodbye World!

Note how the And extension method can be used by both “then” (true) and “else” (false) sides of the conditional because the Else extension method returns !b.

Enjoy!

A Build of open-webkit-sharp That Actually Compiles and Runs

What gives Open Source a bad rep?  It’s projects like open-webkit-sharp, where the source code doesn’t compile (whether you’re using the SVN pull or the download package), where the project setup is different depending on whether you’re using Visual Studio 2010 or 2012, and where the binaries don’t run without errors.  I could go on, describing poorly formatted code, compiler warnings that are so obscure because they don’t normally happen, overriding assembly names from their namespace names, leaving a trail of five different versions of the same assembly but each having a different size and timestamp, local structs that duplicate the same struct by the same namespace and name in referenced assemblies, oh wait, I wasn’t going to go on.

That said, after spending about a day on it, I was able to get the code to compile and execute.   I cleaned up the warnings, and I offer a snapshot of my folder to anyone who wants to download it – you will have to email me though, as I don’t want to provide a direct link to a 28MB zip file on a public blog.  Note that this is a VS2012 build.  And yes, it even builds in Debug mode.

One of the most obscure things I had to deal with was embedded interop, which turned out (thank you stackoverflow.com) to be simple enough–right click on the assembly in the references section of the project, select Properties from  the popup menu, and set the “Embed Interop Types” property value to false.

However, I will tease you with this screenshot of the demo running:

openwebkitsharp

Marc