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));
Tuesday, March 9, 2010
Subscribe to:
Posts (Atom)