Thursday, April 30, 2009

Extension Method to Imitate Times() in Ruby

RubyMy friend and co-worker James is gettin' pretty darned good with Ruby. My experience with Ruby is severely limited. So far, the only thing I've done with it was to implement a "business rules engine" to host a DSL in Iron Ruby.

Today, he showed me a pretty cool method that's built into Ruby called int.times() and I was pretty much instantly jealousified! I decided I had to have one . . . and now I do:
public static void Times(this int times, Action action)
{
for (int i = 0; i < times; i++) action();
}
I through together a few test cases to try it out. I had to make a few determinations though. If you call .Times on a negative number, it loops indefinitely (seemingly). I decided that you should never want to say "do something infinity times." If you do want to do that, you should just have your action call itself at the end of its execution. So, here's the test and the output:
[TestCase(4)]
[TestCase(-6)]
public void test_times_iterator(int times)
{
int i = 0;
times.Times(() =>
{
Console.WriteLine("Index: {0}", i);
i++;
});

Assert.AreEqual(Math.Max(times, 0), i);
}

// Index: 0
// Index: 1
// Index: 2
// Index: 3

Wednesday, April 29, 2009

IComparer Extension Methods for Fluent Interface

IComparerI've pretty much been a blogging fiend today! I love it when I find a topic like IComparer or Extension Methods that are entertaining enough to write about.

Today alone, I wrote articles about how IComparer works, using the proxy pattern to sort by multiple IComparers, and converting a Comparison delegate to an IComparer. Bragging aside though, I've found a fun way to combine my current favorite topics: IComparer and Extension Methods.

I wrote 3 extension methods to help me do some pretty neat things with IComparers and Comparisons. You'll see references to two of my prior articles in this post. The ComparerProxy sorts by multiple comparers and the ComparisonComparer converts a Comparison to an IComparer.

I'll show you the extension methods first, then I'll explain the reasoning behind them, and then I'll show you some usage.
public static class IComparerExtensions
{
public static IComparer<T> Then<T>(this IComparer<T> priority, IComparer<T> then)
{
return new ComparerProxy<T>(priority, then);
}

public static IComparer<T> Invert<T>(this IComparer<T> comparer)
{
return new ComparisonComparer<T>((x, y) => comparer.Compare(x, y) * -1);
}

public static IComparer<T> ToComparer<T>(this Comparison<T> comparison)
{
return new ComparisonComparer<T>(comparison);
}
}
So, lately I've been pretty big on fluent interfaces. I haven't written one yet, but I do use extension methods to make my implementations look a little more fluent. That's why, when I wrote the ComparerProxy, I thought, "Hey, wouldn't be cool if I could just take an IComparer and tack another comparer onto it? And then another? And another?" That's why I wrote the IComparer.Then() extension method.

Next, I realized that if you have a Comparison delegate, it'd be nice to be able to add that to a list of IComparers so I added the ToComparer method.

Finally, another thing that I often want to do is write an IComparer once but to be able to use it in reverse. Id est, I want to invert the logic. Obviously, I could List.Sort() and List.Reverse(), but that's much less efficient than having a CaseInsensitiveComparer and a ReverseCaseInsensitiveComparer.

The problem is, I don't really want to have to write both of those comparers. Instead, now I can call IComparer.Invert() and I get an IComparer that performs exactly like the original, but with inverted logic.

So, without further ado, here are some usage examples. I'm using the same test setup as I did with the ComparerProxy. I've also created several more IComparers to try out:
Comparison<Person> length = (x, y) => x.ToString().Length - y.ToString().Length;
IComparer<Person> lengthLast = new ComparerProxy<Person>(length.ToComparer(), last);

IComparer<Person> lastFirstComposed = last.Then(first);
I executed the following lines which produced the commented results:
_folks.Sort(length);
PrintList(_folks);

// Woody, Pete
// Woody, Lauren
// Caldwell, Cory
// Caldwell, Colin
// Brechtel, Jamus
// Woody, Royce Ann
// Brechtel, Ashley
// Caldwell, Jennifer


_folks.Sort(lengthLast);
PrintList(_folks);

// Woody, Pete
// Woody, Lauren
// Caldwell, Cory
// Brechtel, Jamus
// Caldwell, Colin
// Brechtel, Ashley
// Woody, Royce Ann
// Caldwell, Jennifer


_folks.Sort(lastFirstComposed);
PrintList(_folks);

// Brechtel, Ashley
// Brechtel, Jamus
// Caldwell, Colin
// Caldwell, Cory
// Caldwell, Jennifer
// Woody, Lauren
// Woody, Pete
// Woody, Royce Ann


_folks.Sort(last.Invert().Then(first));
PrintList(_folks);

// Woody, Lauren
// Woody, Pete
// Woody, Royce Ann
// Caldwell, Colin
// Caldwell, Cory
// Caldwell, Jennifer
// Brechtel, Ashley
// Brechtel, Jamus

Converting Comparison<T> to IComparer<T>

IComparerToday, I was working with IComparers (mostly for my Demystifying IComparers blog post). I realized that there's a lot of use for IComparers when working with C# and Linq. I decided that I should dedicate an entire IComparer section to C# Icomparers.

I was working on fodder for several articles when I came across a little annoyance. There's not a Comparer class that takes a Comparison delegate as a constructor argument. Thus, I can call List.Sort() and pass a Comparison delegate or a lambda expression but I cannot create an IComparer that executes that delegate or lambda.

Dissatisfied with this state of affairs, I throw together this little class:
public class ComparisonComparer<T> : IComparer<T>
{
private readonly Comparison<T> _comparison;

public ComparisonComparer(Comparison<T> comparison)
{
_comparison = comparison;
}

public int Compare(T x, T y)
{
return _comparison(x, y);
}
}

IComparer Proxy to Sort by Multiple Conditions

IComparerI thought of another cool use for implementing IComparer.

If you've ever wanted to sort a list of objects using more than one comparer (i.e., you want to sort a list of people by last name and then by first name), then you probably had to compose your own IComparer object to do so. If you wanted to be able to dynamically change sort orders, then your task was more difficult.

To address this problem, I wrote a class called ComparerProxy which implements IComparer and proxies comparisons through a list of IComparers. First, I'll show you the class and then I'll explain a few nuances.
public class ComparerProxy<T> : IComparer<T>
{
private readonly IComparer<T>[] _comparers;

public ComparerProxy(params IComparer<T>[] comparers)
{
_comparers = comparers;
}

public int Compare(T x, T y)
{
int retVal = 0, i = 0;

while (retVal == 0 && i < _comparers.Length)
retVal = _comparers[i++].Compare(x, y);

return retVal;
}
}
So, what's happening here is that when ComparerProxy.Compare() is called, it loops through the collection of comparers until it either runs out of comparers and deems the two objects equal or it identifies a difference and returns the value of that difference.

That sounds confusing even to me and I wrote the class so I'll break it down. Basically, imagine you have a Person object (and soon we will). The person has a FirstName property and a LastName property. If you want to sort by LastName and then by FirstName, it would go something like this:
  1. Sort the two objects by LastName
  2. If the two objects have the same LastName, then sort by FirstName
  3. If the two objects have the same FirstName, then the two objects are equal
If that doesn't make sense, please comment and I'll elaborate.

So, shall we see what it does? I created a Person class (very similar do the one described above) just to do some testing. I overrode the ToString method to print "Last, First" and I added two IComparers. Here's what that class looks like:
private class Person
{
public string First, Last;

public class FirstNameComparer : IComparer<Person>
{
public int Compare(Person x, Person y)
{
return x.First.CompareTo(y.First);
}
}

public class LastNameComparer : IComparer<Person>
{
public int Compare(Person x, Person y)
{
return x.Last.CompareTo(y.Last);
}
}

public override string ToString()
{
return string.Format("{0}, {1}", Last, First);
}
}
I created a test list of Persons and a set of IComparers to test with including two complex IComparers using the ComparerProxy:
List<Person> _folks = new List<Person>();
_folks.Add(new Person { First = "Jennifer", Last = "Caldwell" });
_folks.Add(new Person { First = "Cory", Last = "Caldwell" });
_folks.Add(new Person { First = "Lauren", Last = "Woody" });
_folks.Add(new Person { First = "Colin", Last = "Caldwell" });
_folks.Add(new Person { First = "Jamus", Last = "Brechtel" });
_folks.Add(new Person { First = "Pete", Last = "Woody" });
_folks.Add(new Person { First = "Royce Ann", Last = "Woody" });
_folks.Add(new Person { First = "Ashley", Last = "Brechtel" });

IComparer<Person> first = new Person.FirstNameComparer();
IComparer<Person> last = new Person.LastNameComparer();

IComparer<Person> firstLast = new ComparerProxy<Person>(first, last);
IComparer<Person> lastFirst = new ComparerProxy<Person>(last, first);
I executed the following lines and, lo and behold, achieved the expected results:
_folks.Sort(last);
PrintList(_folks);

// Brechtel, Jamus
// Brechtel, Ashley
// Caldwell, Jennifer
// Caldwell, Colin
// Caldwell, Cory
// Woody, Lauren
// Woody, Royce Ann
// Woody, Pete


_folks.Sort(first);
PrintList(_folks);

// Brechtel, Ashley
// Caldwell, Colin
// Caldwell, Cory
// Brechtel, Jamus
// Caldwell, Jennifer
// Woody, Lauren
// Woody, Pete
// Woody, Royce Ann


_folks.Sort(firstLast);
PrintList(_folks);

// this one is the same as just sorting by first name
// in retrospect, I should've added some folks with the same first name
// c'est la vie
//
// Brechtel, Ashley
// Caldwell, Colin
// Caldwell, Cory
// Brechtel, Jamus
// Caldwell, Jennifer
// Woody, Lauren
// Woody, Pete
// Woody, Royce Ann


_folks.Sort(lastFirst);
PrintList(_folks);

// Brechtel, Ashley
// Brechtel, Jamus
// Caldwell, Colin
// Caldwell, Cory
// Caldwell, Jennifer
// Woody, Lauren
// Woody, Pete
// Woody, Royce Ann

Demystifying IComparer in C#

IComparerI was looking through the analytics on my blog and I noticed a trend. I get a ton of searches for things related to the IComparer in C# article I wrote a few months back. I can only imagine that the reason people are searching for information on the IComparer so frequently is because the IComparer is a little mysterious to most people. It's pretty uncommon that you need to use one (let alone write it).

In truth, the IComparer Documentation isn't really all that helpful. Most examples of an IComparer that I found when I was working on my best fit select and my quickselect articles simply use x.CompareTo(y) or a built in IComparer.

Sure, that works, but I think people are hitting my blog trying to uncover the inner workings of the actual comparison.

Well, here it is. IComparer has one member method called Compare(object x, object y) (the generic version is strongly typed) which returns an integer. If the value of x is less than y, the result is less than 0. If the value of x is greater than y, the result is greater than zero. If the two values are equal, the result is 0.

Now, in most implementations I've seen, the results are nominal data (i.e., one trit valued -1, 0, or 1); however, I don't really see any reason you can't return ordinal, interval, or ratio data as well (although, returning ordinal or interval data would be relatively difficult as it requires knowledge of the rest of the collection). If that didn't make sense, take a look at levels of measurement. If you'd like me to elaborate on levels of measurement, leave a comment and I'll write an entire post about it.

Using my fictional class Person with the Age property, a common AgeComparer class would look something like this:
public class AgeComparer : IComparer<Person>
{
public virtual int Compare(Person x, Person y)
{
if (x.Age == y.Age) return 0;
return (x.Age > y.Age) ? 1 : -1;
}
}
While that's useful for sorting, it doesn't really give you any information about the magnitude of the difference between the two objects and may as well just be called WhichOneIsGreater(object x, object y). If you were using the comparer to graph items in a list, this would be relatively useless. Also, it's an entire line of code more than you need, so I'm proposing a solution like the following:
public class AgeComparer : IComparer<Person>
{
public virtual int Compare(Person x, Person y)
{
return x.Age - y.Age;
}
}
One caveat is that some people use IComparers expecting 1, 0, or -1 (i.e., if (IComparer.Compare(x, y) == 1) {...}). Certainly, using a non-nominal result would break their code, but IMO had they read the documentation on IComparer, they'd have seen nothing about 1, 0, and -1 as the return value.

I challenge you to think of other comparisons you could make between objects and post them in the comments of this blog post. In the meantime, I'm going to start an entire blog section on IComparer and we'll see what we can come up with.

Thursday, April 23, 2009

Required iPhone Knowledge for International Travel

iPhone Abroad

PDA Mode

You can disable the cellphone antenna on the iPhone by putting it in airplane mode. By default, airplane mode also disables the wifi antenna. You can re-enable the wifi antenna independently of the cellphone antenna by clicking Wi-Fi and flipping the switch to on. This will allow you to access the internet without incurring roaming charges.

Textual Intercourse

One thing you'll miss is text messaging. Do not fret Littlefoot; in the days between snail mail and text messages, people sent emails. iPhone works great with emails. Set up your gmail account on your iPhone and check it whenever you get internet access.

If you need more immediate response times, get a multi-protocol iPhone IM client. There are several great IM clients for the iPhone that you can get for free. My favorites are Fring and Palringo. I like the Palringo interface a lot for IMing, but I like Fring for IM and Skype support.

Phone Home

Calling home is a lot of fun when you're on an international adventure so you can make all of your friends and family jealous. I'm a big fan of the Skype unlimited U.S. land line calling plan for $2.95 so that's what I use. Also, Skype-to-Skype calls are free. There may be better VoIP services out there, but I haven't found one yet.

So, how do you Skype on the iPhone? Like I said before, Fring supports Skype and multiple IM clients and has a decent interface. As a note, when you use Fring to make a Skype call, you have to put +1 before the area code and phone number (i.e., +17705551212). Skype recently came out with its own iPhone application but it's spotty at best for me; you may have better luck.

Social Networking

Facebook has a great free iPhone application to keep you in touch with your friends and family back home. If you tweet, I really like TwitterFon. TwitterFon conveniently supports twitpic too.

Finances

When you're traveling internationally, it's nice to be able to check on your bank accounts back home; however, it'd be pretty tough to get to every page you're interested in within the 1 hour of internet access you get for 5 billion euro. If you use Mint.com (and you should), then you can get the free Mint.com iPhone application and check every balance at once and get a detailed transaction list.

Literature

Search the iTunes App Store for literature you may want. You can find travel guides, maps, and translators. Also, search the internet for images relevant to your trip. For example, find a large image of the subway system and break it into sections so you can view it on your iPhone.

Tuesday, April 7, 2009

Extension Method to Join Strings with a Delimiter

String ArrayI was talking with my coworker James Brechtel today about writing an extension method to join an array of strings together with a delimiter like string.Join() does. I'd prefer string[].Join() because it seems more fluent.

After I put the IList<T>.ToArray<T>() extension method together, implementing the fluent style join was a simple task.

Here's what that looks like:
public static string Join(this IList<string> list, string separator)
{
return string.Join(separator, list.ToArray());
}
That's it! :)

ToArray Extension Method Converts IList<T> to T[]

String ArrayI was talking with my coworker James Brechtel today about writing an extension method to join an array of strings together with a delimiter like string.Join() does. I'd prefer string[].Join() because it seems more fluent. I was throwing the IList<string>.Join() method together when I ran into a small snag.

I needed an array of strings for the string.Join() method. I put together a quick ToArray<T>() extension method which iterated through each item in the array and created an array T[]. The problem is, if I already have an array, that's a lot of extra work and some wasted memory.

So, I tried IList<T> is Array and it works great. I just cast the IList<T> as T[] and return it. Otherwise, I execute the iteration. Here's what it looks like:
public static T[] ToArray<T>(this IList<T> list)
{
if (list is Array) return (T[]) list;

T[] retval = new T[list.Count];
for (int i = 0; i < retval.Length; i++)
retval[i] = list[i];

return retval;
}
Pretty basic, but nice n' fast.

Why You'll Always Need a Programmer

Business Analyst LadyI've been writing software on various platforms for a little longer than a decade now and I have noticed a reoccurring trend. Customers are asking for and software vendors are trying to develop a way to allow business analysts to implement business logic in their applications.

The idea is that the business analyst knows the business inside and out. Therefore, if the business analyst had a way to implement the business logic, then there would be no need to have both the business analyst and the programmer (and the ensuing communication overhead).

I, however, am not concerned. In my experience, I have come to the conclusion that not only is the implementation of business a programming task, but it is a programming task that requires a strong development background.

What I mean is, you can write DSLs, query builders, and the like until you're blue in the face (presuming that you would eventually turn blue in the face writing DSLs and query builders), and the technically savvy business analyst will be able to learn to use these tools; however, without the foundation of good programming practices, this business analyst will produce vast amounts of technical debt which will, in the long run (if not the short run), cost the company much more than the programmer would have in the first place.

The Problem

Over the last several decades, programmers have developed practices and patterns that allow us to write applications that are easily upgradeable, scalable, maintainable, and even readable. We have built on this body of knowledge and we have tested it. Sure, there are some disagreements here and there, but for the most part, as we develop professionally we are forming a special way of looking at the world and solving problems which is relatively unique to programmers.

Similarly, business analysts have been doing the same thing. They look at the world and approach problems in their own special way. I believe they are two sides of the same coin. Both are responsible for understanding, documenting, and implementing business rules. One side communicates these rules to humans and the other to machines.

Enabling a business analyst to communicate directly to machines seems like a money saving flexibility that can't go wrong. It can go wrong — terribly wrong, and it can do so before you realize there's a problem. I believe that the business analyst would take as long to learn about the programming practices required to make solid architectural decisions as it would take the programmer to learn to be a business analyst.

If the business analyst is able to implement solutions, it is almost certain that there will be errors that a programmer would have caught. When the needs change, the analyst may even be able to address these changes, but this will lead not only to more errors, but also to an increased complexity that a programmer would have avoided.

Thus, the more work that goes into the solution over time, the more difficult it will be to maintain. Dealing with maintenance issues will lead to unintended consequences which will be corrected with more patches resulting in what programmers would liken to spaghetti code.

Eventually, the design entropy will lead to such a disaster that the vendor will need to be called in. Vendor engineers for large software companies can cost more than 300 dollars an hour.

Case Study - Business Process Automation

I have been working with a BPM engine for several years now. It's really great at what it does. It provides an asynchronous state engine, a visual workflow designer, and data persistence. The only real problem with this application is the way it is marketed to customers.

The company that produces the application claims that business processes can be automated without the need of a programming staff. They tout the visual designer as business analyst friendly. I agree that it is BA friendly . . . to read. Generating workflows is a vastly different story.

I have implemented this software for my own clients, for a very large government organization, and indeed for the software vendor itself. In every case, I have shown that even at a very low complexity, these implementations still required a knowledgeable programmer.

Case Study - Data Validation

I have a friend who works with a dynamic data collection application which implements data validation on the server side in SQL stored procedures and in javascript code snippets on the client side. The person who develops most of the data validation for one of his customers is an entry level programmer.

All of the validation works and everything is well and good until something changes. Even minor somethings can result in very intense development, debugging, and testing cycles. The people who developed these applications are very smart and clever people, but their lack of programming background has resulted in code which is almost impossible to maintain.

A more solid understanding of programming principles would have prevented probably 90% of the code repetition in these business rules. One small change wouldn't require a code change to every single business rule. Isolating bugs and errors could be accomplished through tests of small units of code rather than very large blocks of difficult to follow code.

The Solution

You will always need a programmer. If you are trying to develop a software based solution to save your company money, an architect with a solid programming foundation will produce a much more robust and more easily maintainable solution. The solution will last longer and will scale easier than anything your business analyst can come up with alone.

A good BA / software engineer team will produce benefits to your company that you, your BA, and your engineer could never have invented individually. The initial cost may be higher, but consider this.

Most of the time, your company will have to foot a hefty bill to train the BA. There will be evaluations, proof of concepts, and tests. More often than not, the determination will be that a BA just doesn't have the experience required. At that point, you will either have to hire a programmer and go through the same process or you will have to contract with the vendor or one of its partners.

The worse scenario is that your BA can implement a solution and that it works. When the application changes, you generate need for more application changes. These changes generate more changes. These changes compound and you're faced with a very costly overhaul of the entire system.

It is far better to start with a BA and a programmer. Send them both to the appropriate training. Have them both work together to produce adequate project documentation. Foster an environment where they are mutually productive. Not only will the whole be more than the sum of its parts, you'll save a great deal of expense and heartache.

Monday, April 6, 2009

More Yahoo Pipes

Pipe ImageLast week, I wrote a blog post about Yahoo Pipes. Shortly after I finished that post, I was doing some testing and my little brother Colin sent me an email complaint.

See, I am using my pipe to power an RSS feed in feed burner which I use both on my corporate profile and in my twitterfeed. I have facebook monitoring my tweets and updating my status.

The problem is, it looks pretty silly when I get a spam comment (or any comment really) and it pops up as my facebook status. So, when Colin sent me a message and said, "Pat, I have no idea what your status means or what that has to do with anything," I decided to update my pipe.

This is my new feed aggregation Yahoo pipe with context information.

Saturday, April 4, 2009

Using Yahoo Pipes

Yahoo PipesI was looking for a good way to combine a set of RSS feeds into one feed. I saw several web based services out there, but none was quite good enough for my needs. In my research, I happened across Yahoo Pipes. In about 3 minutes, I logged in, added 6 feeds, sorted them by publication date, and selected the top 25. Now, I have one RSS feed to aggregate everything including my two blogs (computing and aviation) and the comments from each one as well as my picasa albums and their comments.

The next thing you know, I have twitterfeed reading from that RSS feed and updating my twitter. The twitter facebook application then updates my statuses. Combining all of these services lets pretty much all of my friends and professional contacts know what I'm up to and what I'm publishing. Good stuff.

The only problem so far is that the twitter statuses look a bit confusing when I'm setting my facebook status to someone's comment on one of my blogs. I'm glad it's published, but I'm going to be looking into some of the transformations available in Yahoo Pipes to see if there's a way I can prefix posts to identify them as blog posts vs. comments.

PIN Entry Screens Considered Harmful

Digital PIN PadI never wrote a "Considered Harmful" blog post before so I figured it was high time I did so. I often find myself at the grocery store, gas station, or fast-food restaurant keying my 4 digit PIN into some electronic entry system.

I look around and see someone watching me type in my code. Whether they're doing it intentionally or they're doing it incidentally, it still bothers me. I don't really want anyone seeing my PIN, particularly if that person wants to know it.

The other day, I was waiting behind some woman at the grocery store. In my frustration, I watched every move she made. She pulled her card out of her purse, handed it to the cashier, and started keying her PIN. From my angle, I couldn't see the actual numbers, but when she finished, I was completely shocked that I knew what her PIN was!

Evidently, without even noticing that I was doing it, I watched her key her PIN and I figured out what it was by the location of her presses alone. I had to be sure, so I asked, "ma'am, is your pin 4321?" Shocked and appalled she said, "why were you paying attention to that?" I replied, "it was an accident . . . I just happened to see where you were pressing."

That's when I had an idea. The digital keypads must be simply displaying the numbers on the screen. It seems that it would be completely reasonable to scramble the actual numbers so that nobody could decode your PIN simply by watching the location of your key presses.