Saturday, 27 December 2008

MIX 10k Challenge

I just submitted my MIX 10k Challenge entry - so I can stop worrying about tweaking and move on to the next little project. I'll post a link if/when it's approved, but for now here is the single line of c#

and the single line of XAML

and the 'proof' that it's only 10k


I had planned to write a whole post about 'how to fit more code into 10k', but Bill's Thoughts on the MIX 10k Challenge and Adam's What can you make with 10k of Silverlight or WPF? have covered most of the tricks already... I'll just mention a handful of comments/additions below:

Bill's second suggestions is to use short class/method/property/field/variable names - single characters if possible. In the interests of 'maintainability' I did not quite go that far (using two- and three-letter acronyms) but in general the code looks like it's been obfuscated (as you can see on the class diagram, which also shows I 'missed' a couple of opportunities for refactoring)

I'd originally used human-readable names (the code was well over 20k originally, with comments) and used the Visual Studio Refactor->Rename feature to arrive at the 'compressed' version.

Bill also said "Make sure to use using statements to avoid having to specify the namespace". Good idea - but using can be even more helpful if you alias classes that you plan to use a lot. For example,
using v=System.Windows.Media.Animation.Storyboard;
requires 50 chars for the using declaration, but nine instances of 10 chars became 1 char (Storyboard --> v) which is 81 chars less, an improvement of 31 chars :) You only need to use PropertyChangedEventHandler three times before it's more efficient to
using p=System.ComponentModel.PropertyChangedEventHandler;

Another one I'd expand on is "Reduce whitespace. C# can be written as a single line for the entire file". It's not just line-breaks that you can save - C# in particular can be much shorter if you remove unnecessary spaces (that the IDE inserts) around parens, braces, commas and semi-colons, and omit braces where not required (single-statement-if s). Not sure how space-efficient you can be with Visual Basic...
Another area where the IDE will frustrate you is by offering to put each of your classes, code-behinds, etc into a individual files. DON'T LET IT! there's nothing special about a code-behind file, so put ALL your code into one .cs (I put the entire codebase into Page.xaml.cs). This saves you mulitple using clauses and namespace declarations.

It might seem obvious, but using var instead of the Type name will always be shorter (how many Types other than int are three chars or less?).

Also obvious: declare variables of the same Type together ('globally' if required) and initialize them in the declaration if they need initializing. eg int i,j,k=256; rather than int i;int j; int k; k = 256;
Re-use variables across methods. Don't use a property where a field will do.

Use defaults to your advantage - for example don't specify public protected private if you don't need to. Think about the defaults that apply in Xaml, Databinding, etc.

Unfortunately I don't have any hints on making 'small' applications "look good". I definitely did not inherit the Designer-gene ... for that I'd check out Jose's blog.

Good luck with your MIX 10k entry!

2 comments:

  1. One thing I don't understand about this competition is why the 10K challenge has to be measured by source code and not the compiled and compressed code.
    It forces people to make ugly code, and it doesn't really matter to the end user how much code there was, only how big the download is.
    There are so many other things you can tweak on, like using xaml instead of images, using better zip compression, ensure you have fewer IL statements etc.

    ReplyDelete
  2. Similar sentiments were posted here. Adam's response was We went with counting the source code for a few reasons, ... due to the fact we're supporting different technologies... the contest was inspired by Butterfield's 5k contest, where the focus was on the code written and resources used, not the HTML or JavaScript engine.
    Admittedly this is our first attempt at a contest like this and we're keeping notes.


    It's fair enough to say "this is like Butterfields" - they got their inspiration and ran with it. I'm sure they realised they couldn't please everyone...

    HOWEVER, it still seems weird for a Microsoft competition. He says because...we're supporting different technologies -- Silverlight/XBAP/ClickOnce may have some major differences I'm not aware of -- but they're all fundamentally CLR/IL, right? There isn't a better metric than source-size?

    Every time I was forced to use some verbose Xaml element/attribute I cringed - Xaml really isn't well suited to size-limitation. Ditto for some of the framework classes. And I've got NO idea how you'd go saving whitespace in Visual Basic source - disincenting VB devs isn't very 'Microsoft', is it?

    I do see their point about not using "download size" though - my final assembly is 23k; but the XAP is 250k because I need System.Xml.Linq .Serialization and .Utils which need to be packaged up and downloaded. You'd need more explicit rules to say which Microsoft-supplied assemblies are allowed and which aren't (not to mention how limited the Silverlight applications
    functionality would be).

    It will be interesting once all the entries are in to see what the average 'final assembly' size is... perhaps a 2^15 (32,768bytes) Silverlight assembly would be a good benchmark (based on more features than my 23k assembly, and picking a power-of-two for good measure - 16k would be too small I think).

    If I get a chance I might 'port' my entry to XBAP and ClickOnce just to see how they differ in source, assembly and final download size...

    ReplyDelete

Note: only a member of this blog may post a comment.