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#
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKtocRoPIoC-J6G4cC3Giw56422iohwgrdSiptHWVjYELtx1Db9deek11NEiWUUJHFgpTByAEsrV-_PEcNVmDf50myi4V12wM8SkcUf5J8hfxQAYmVAk8Y9cd2eQV7uQToUhjE/s400/code_sml.png)
and the single line of XAML
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6wTVF_Yzl2GCUlFfC5O5pkZ1PtNDoMA3AlLUV6SADkzzerB31zEuA5eGDjRae3U95iqvq99Cv2IrCXiRv7F5alc66YC_E-D9J1sT_p4aHBKN4N1D3345eGnAZbRrrKAHKC30o/s320/xaml_sml.png)
and the 'proof' that it's only 10k
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgSBqw2WdXotrrVCNeTSp3ScTQURx2jJLpS-2hU1h0FJ18teiH_etj2aN5_IHoww7adnaB3F-lFUqMKLoNPSq5xNKIxaSTNNabQP7xoj-CT-Ie6X77EgumIqALSdvNODYmYRbOD/s320/size.png)
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)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi9RAj5wLTBC83pTos2CQ-a5FxxWvNa3i3JuMvP6fZ1lTjdMgAQlSz47QYeI293TblwWg6_7q9dPEuLptGFy-9zCkUnFrGRGkhPI6qcgM4UUFkL9dUcemcBhAUJsYhDZAS28uZe/s400/classes.png)
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!