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,
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
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-
ifs). 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
It might seem obvious, but using
varinstead of the Type name will always be shorter (how many Types other than
intare 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 privateif 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!