Tuesday, May 20, 2008

Use the force (intellisense, snippets, refactor).

Let go of the compiler. Use the features Luke.

I received feedback one of my posts that a line of code was too complicated for average to starting programmers:


private static IEnumerable<list<int>> permutations(int size)



The interesting thing about this line is I did not write it.

I did not even write all of the Line that calls it:


foreach (List<int> permutation in permutations(9))

I do recall my thinking as I was creating it. I was thinking I want to loop over a bunch of collection of permutations dealing so I typed in the first letters of 'foreach'. Intellisense kicked in and suggested the 'foreach' snippet. I hit tab twice to accept it and the following code was generated:


foreach (object var in collection_to_loop)
{
}

The snippet highlighted the snippet literals to be replaced. I had not thought what type a permutation would best be. So I used a list of integers 'List'. I already knew I wanted a permutation so naturally the collection would be called permutations. But since I knew I wanted to experiment with yield I turned it into a method. Which left me in the same position as those 'typical' readers. But selecting 'Generate Method Stub' from the context menu resulted in complex type.

What is interesting is how the compiler has moved from nagging us about our mistakes after the fact to proactively helping us write code. So I stop worrying about the compiler and it know worries about me.

No comments: