Tuesday, March 9, 2010

Inversable Functions

How many times have we written pairs of functions like this:

string urlEncode(string plaintext){ .. contents... }

string urlDecode(string encodedtext){ .. contents... }

Have you ever noticed that the code in the second function is just doing the inverse operations of the first , but in the reverse order?

That is redundant code in my mind. And redundant code has more opportunity to introduce bugs because I may update the first function and forget to update the second , or update the second incorrectly.

We should therefor have a general programming construct whereby we can mark certain functions as inversable. A function is automatically inversable if it is itself an ordered collection of inversable functions/operations.

In this way we should be able to use an inverse operator like so:

string url = urlEncode("52%");

Assert.AreEqual("52%",^urlEncode(url));

Or something like that anyway... Notice the ^ operator for doing the inverse.

Or maybe we could declare the name of the inverse function in the definition:

string urlEncode(string plaintext) : inverse urlDecode
{ .. contents... }

which generates a comiple error if the function is not actually inversable. Then we can call

Assert.AreEqual("52%",urlDecode(url));

Saturday, July 18, 2009

Introduction

I'm Andrew, a developer working predominantly with C#.

I've only really been in the game for about 3 yrs now, and I have a lot to learn, but I'm starting to feel like there's a lot we can do to improve the way we program.

I'm not a fantastic researcher, I'm more of "an ideas man" (think The Castle). There are lots of times I am looking for a solution to really common programming problem, and I just cant seem to find any solution that solves it in a really abstract and complete way.

The solutions we have (ie languages,toolkits,frameworks) provide useful abstractions, but are more often than not leaky, and nearly always incomplete. I say an incomplete abstraction nearly always increases the amount of knowledge required to understand a section of code.

In the mathematics world we have a beautiful abstraction called a matrix. If we can turn a set of simultaneous linear equations into a matrix, it represents a very useful and complete abstraction that we can do all sorts of simple operations on. Like find an inverse, or an eigen-vector. It takes some time to learn the abstraction, but by golly once you learn it you never have to look beyond the surface to see whats actually happening underneath.

As I've been working with C# 3.5, NHibernate, CAB (Composite Application Block) , ASP.Net, and the Microsoft MVC framework, I've been noticing some real holes, and being a person who is not content to just complain about things, I've decided I need to put some ideas out there on where the existing solutions don't cut the mustard, and some suggestions as to what we could do about it.

I hope that by putting out my ideas, and getting lots of criticism, that our communal understanding would be refined, and we will come up with some really useful stuff that might actually change the way we program!