Thursday, November 25, 2010

Pex4Fun

Check out this really cool web site. Not only you can write code in your browser you also have IntelliSense. The way it works is you are given a puzzle or a duel to solve (think code breaker C# edition). The goal of the game is to write the same algorithm than the hidden solution. You get hints by running Pex against both the hidden puzzle and your solution.

Pex is a tool that will try to call a function with any kind of input parameters required to use all the code path of the function. It will highlight thing like passing null might cause a NullReferenceException down the line and you should guard against it. So by trying all those combinations against your code and the hidden puzzle will give hints on the difference of implementation. You can then update your code and try again each time getting closer and closer to the solution.

You can even create an account and keep track of all the challenges you solved. A section of the site is structured as a course where you learn about C# and cool features like Code Contracts.

Code Contracts helps us create a more declarative way of expressing pre and post conditions to our code. And because of the declarative way they works, tools can then to provides us with static compile time validation, run time guard code and automated API documentation of the contracts.

Pex4Fun is really a great way to play around those 2 great tools without having to setup libraries on our machine. I can even show some basic C# stuff to my friends, on the web and even from my smart phone (but then it’s not that fun I must admit!)

Tuesday, November 9, 2010

Introduction to LinqPad

It’s really sad that in the late 2010 most people still don’t know about this incredible little tool.  For me it is a bit like what Reflector used to be in the early days of .Net, a great and FREE productivity tool outside of the Visual Studio IDE and Microsoft tooling.


LinqPad was created by Joseph Albahari as a companion tool for his book C# 3.0 in a Nutshell.  You don’t have to buy the book to use the tool (actually I haven’t bought the book either! me bad).
It’s really difficult to do justice to LinqPad in a small blog and I don’t want to write a too long post so I’ll try to expose the most useful features to you today and eventually blog about specific ones in detail in the future.

Code Snippet execution

This is the reason I use LinqPad almost every day.  If I want to test a snippet of code I don’t create a dummy VS project, I simply try it in LinqPad.  You can even write a full program if you want and save it as a code snippet.  I use this to create most of my utility tools like when I need to read and display an xml file or generate one I just use LinqToXml.


LinqPad1

Learning Tool for Linq and other .Net features

Went I first found out about LinqPad it was when .Net 3.5 came out and understanding Linq queries was still hard for me.  The tool has a lot of good Linq code sample built in so I could learn and play with the queries.  What makes this interesting is the Dump extension method of the Object type which is really powerful to display the state of any simple or complex data structure.

LinqPad2

Integrated LinqToSql

Another useful thing to do is connecting to a database and have LinqPad setup a LinqToSql wrapper over the schema so you can query it with code rather than SQL.  I cannot say I’ve used this feature a lot but sometime it can be nice if you want to do some processing of data coming from a database.  A Linq to Entity and OData options are also available.  In the future I guess I’ll be using LinqPad to query source like NetFlix and the other OData feeds too.

LinqPad3

External Library

This is something new for me (and I guess to a lot of people given I haven’t seen much info about it on the net), you can load any dlls in LinqPad to use in your queries.  This is an excellent way to create libraries of useful functions and extension methods.  I’ll be using this feature to query assemblies using Mono.Cecil with some extra functions of my own.  I’ll share with you the result of my experiments in a future series of posts.


Go download LinqPad!  You should definitely give this great tool a try.