Saturday, January 3, 2009

MIX09 10K Smart Coding Challenge

I entered a Zleek Silverlight application into the MIX09 10K Smart Coding Challenge. Please Vote for my entry by rating it 5 stars!

It is a very "lite" version of Zleek that contains only a single page where you can upload images, manipulate them using drag/rotate/scale, and save your layout. You can't share layouts with other users, but the application will remember your last saved layout.

The idea of the competition is to fit as much functionality in only 10KB of source code. Here's a snippet of what size constraints will make your code look like (note that my actual submission was all on a single line of text):
I should write code like this at work! I'm sure everyone would appreciate how easy to read it is! Anyway, it was fun putting together code completely at odds with my normal coding style. For you geeks out there thinking about entering, here is what I learned from the experience:

  • var is your friend.
  • Use lambdas where possible. The implicitly typed arguments are great for shaving off type names (just like var).
  • LINQ extensions to IEnumerable and IList are great. ForEach and Cast will save you a lot of code especially when combined with lambdas.
  • Use fields over properties. Remember that leaving off the access modifier will mark them private.
  • Question every single framework call you make and whether or not it makes sense to encapsulate it. I ended up taking a bunch of the common ones and creating static methods that wrap the calls.
  • If you have a function that takes multiple parameters of the same type, use an array declaration instead. (e.g. CalcDistance(Point a, Point b) becomes CalcDistance(Point[] p) -- and of course the actual function name should be trimmed to 1-2 characters)
  • XAML vs. Code vs. external image -- In many cases, code is preferred over XAML. The obvious exceptions here is XAML that makes heavy use of Path.Data (code does not accept the shorthand notation, AFAIK). The code for downloading an image using WebClient is fairly small. Definitely worth looking at. Note that downloading remote XAML is frowned upon by the contest moderators and should not be used.
  • Consider subclassing commonly-used non-sealed framework classes. Unfortunately, all of the common long-named classes I was using were sealed, so I couldn't do this... but I wanted to try and subclass framework classes that I used a lot so that I wouldn't waste space with the type name that often.
  • Place all of your CODE in a single file so that you only have to take up space for "using" directives once. (Note: you might have to add some class assignment directives as well e.g. using Path=System.Windows.Shapes.Path when System.IO is also imported)
  • Cleanup all of the whitespace. Remember special spacing rules like int[]a and int?a are valid and do not require spacing after the type name due to the non-alpha character.

No comments: