Get Microsoft Silverlight

Wednesday, December 31, 2008

Mix 10K Coding Challenge - Few Tips I have used to limit the code size



While developing for the MIX 10K coding challenge, the major challenge to face is to limit the code in 10K size. So I had to question each and every line of code that would it be possible to write this even with fewer bytes.
 Check out my application at http://2009.visitmix.com/MIXtify/TenKDisplay.aspx?SubmissionID=0044 Make sure to run the application in fullscreen mode.
 Here I am compiling a list of tips and features I used to reduce the code size. 
1.       Automatic Properties - Used this .NET feature to create the Tweet class which represents an RSS feed item from the search result   
public class Tweet {
  public string Guid { get;set; }
  public string Title { get;set; }
  public string Author { get;set;

    … ….

2.       Object Initializers
new Tweet {Title = element.Element("title").Value,
Author = element.Element("author").Value,
Guid = element.Element("guid").Value}};
3.       LINQ to XML - I have used this to read the XML stream from the search result handler.
4.       Collection Initializer - Collection initialize has used along with the LINQ query, which does a huge amount of job with fewer lines.
5.       Use 'Var' - Used to initialize a variable without specifying its type
 The bellow few lines do all the work to grab the RSS feed in to a collection variable which utilizes the features 3,4 and 5 above
XElement elements = XElement.Parse(e.Result);
var items = from element inelements.Descendants("item")
select new Tweet { PrevZ = double.NaN, Title = element.Element("title").Value
 6.       Anonymous functions - This save you some bytes. Bellow line I have used to toggle between the Fullscreen modes when the user clicks on a checkbox named 'chkFullScreen'
chkFullScreen.Click +=delegate{Application.Current.Host.Content.IsFullScreen = !Application.Current.Host.Content.IsFullScreen; };
 7.        Remove all Private keywords - Default member accessor is 'Private' so you don’t need to specify that :)
8.       Remove all unused 'Using' statements - Easy ways to check – When you finish coding, removes all the ‘using’ statements and then correct the compile error
Some other editing tricks (This can make the source code un-readable too).
  Reduce the Tab size to zero
  Remove all white spaces
  Make the entire source code into few lines.
  Put all the source code in to a single source file 
  Single character variable names.

I haven’t used much of these editing tricks. The code was just 8.96KB. So I kept the source readable and in 4 separate files.

1 comment:

婷婷 said...

Your blog is wonderful, I like it very much, thank you!
By the way, do you like Burberry Polos, which are very chic, especially the Ralph Lauren Polo Shirts, I love them very much. I also like playing sports, it can keep healthy, what do you like to do?
We are the outlet of Wholesale Polo Shirt, Polo Ralph Lauren shirts on sale,these products are best-seller in our store cheap polo shirts Cheap Polo ShirtsToday is Christmas, and our products also have a corresponding discount activitiesWholesale Polo Shirts -50% OFF,cheap polo shirts Cheap Polo ShirtsSo if you love these, you should not miss our store, we can meet what you want, and you can find many surprise in our store