<?xml version="1.0" encoding="UTF-8" ?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-US"><title type="html">CliveT</title><subtitle type="html">Software Engineer - Red Gate Software</subtitle><id>http://www.simple-talk.com/community/blogs/clivet/atom.aspx</id><link rel="alternate" type="text/html" href="http://www.simple-talk.com/community/blogs/clivet/default.aspx" /><link rel="self" type="application/atom+xml" href="http://www.simple-talk.com/community/blogs/clivet/atom.aspx" /><generator uri="http://communityserver.org" version="2.0.60217.2664">Community Server</generator><updated>2009-07-20T13:05:00Z</updated><entry><title>Some notes on Reflector 7</title><link rel="alternate" type="text/html" href="http://www.simple-talk.com/community/blogs/clivet/archive/2011/02/01/99118.aspx" /><id>http://www.simple-talk.com/community/blogs/clivet/archive/2011/02/01/99118.aspx</id><published>2011-02-01T08:31:00Z</published><updated>2011-02-01T08:31:00Z</updated><content type="html">&lt;p&gt;Both Bart and I have blogged about some of the changes that we (and other members of the team) have made to .NET Reflector for version 7, including the &lt;a href="http://www.simple-talk.com/community/blogs/bart/archive/2010/12/16/96204.aspx"&gt;new tabbed browsing model&lt;/a&gt;, &lt;a href="http://www.simple-talk.com/community/blogs/bart/archive/2011/01/10/98258.aspx"&gt;the inclusion of Jason Haley's PowerCommands add-in&lt;/a&gt; and some improvements to decompilation such as &lt;a href="http://www.simple-talk.com/community/blogs/clivet/archive/2010/12/16/96199.aspx"&gt;handling iterator blocks&lt;/a&gt;. The intention of this blog post is to cover all of the main new features in one place, and to describe the three new editions of .NET Reflector 7. &lt;/p&gt;  &lt;p&gt;If you'd simply like to try out the latest version of the beta for yourself you can do so &lt;a href="http://reflector.red-gate.com/"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Three new editions&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;.NET Reflector 7 will come in three new editions:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;.NET Reflector&lt;/li&gt;    &lt;li&gt;.NET Reflector VS&lt;/li&gt;    &lt;li&gt;.NET Reflector VSPro&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;The first edition is just the standalone Windows application. The latter two editions include the Windows application, but also add the power of Reflector into Visual Studio so that you can save time switching tools and quickly get to the bottom of a debugging issue that involves third-party code.&lt;/p&gt;  &lt;p&gt;Let's take a look at some of the new features in each edition.&lt;/p&gt;  &lt;h4&gt;Tabbed browsing&lt;/h4&gt;  &lt;h4&gt;&lt;/h4&gt;  &lt;p&gt;.NET Reflector now has a tabbed browsing model, in which the individual tabs have independent histories. You can open a new tab to view the selected object by using CTRL+CLICK. I've found this really useful when I'm investigating a particular piece of code but then want to focus on some other methods that I find along the way. For version 7, we wanted to implement the basic idea of tabs to see whether it is something that users will find helpful. If it is something that enhances productivity, we will add more tab-based features in a future version.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.simple-talk.com/blogbits/Clive.Tong/SomenotesonReflector7_77BA/image.png"&gt;&lt;img title="image" border="0" alt="image" src="http://www.simple-talk.com/blogbits/Clive.Tong/SomenotesonReflector7_77BA/image_thumb.png" width="606" height="347" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;h4&gt;PowerCommands add-in&lt;/h4&gt;  &lt;p&gt;We have also included &lt;a href="http://www.jasonhaley.com/"&gt;Jason Haley&lt;/a&gt;'s PowerCommands add-in as part of version 7. This add-in provides a number of useful commands, including support for opening .xap files and extracting the constituent assemblies, and a query editor that allows C# queries to be written and executed against the Reflector object model . &lt;/p&gt;  &lt;p&gt;All of the PowerCommands features can be turned on from the options menu. We will be really interested to see what people are finding useful for further integration into the main tool in the future.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.simple-talk.com/blogbits/Clive.Tong/SomenotesonReflector7_77BA/image_3.png"&gt;&lt;img title="image" border="0" alt="image" src="http://www.simple-talk.com/blogbits/Clive.Tong/SomenotesonReflector7_77BA/image_thumb_3.png" width="420" height="342" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;My personal favourite part of the PowerCommands add-in is the query editor. You can set up as many of your own queries as you like, but we provide 25 to get you started. These do useful things like listing all extension methods in a given assembly, and displaying other lower-level information, such as the number of times that a given method uses the box IL instruction.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.simple-talk.com/blogbits/Clive.Tong/SomenotesonReflector7_77BA/image_4.png"&gt;&lt;img title="image" border="0" alt="image" src="http://www.simple-talk.com/blogbits/Clive.Tong/SomenotesonReflector7_77BA/image_thumb_4.png" width="420" height="342" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;These queries can be extracted and then executed from the 'Run Query' context menu within the assembly explorer.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.simple-talk.com/blogbits/Clive.Tong/SomenotesonReflector7_77BA/image_5.png"&gt;&lt;img title="image" border="0" alt="image" src="http://www.simple-talk.com/blogbits/Clive.Tong/SomenotesonReflector7_77BA/image_thumb_5.png" width="553" height="615" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Moreover, the queries can be loaded, modified, and saved using the built-in editor, allowing very specific user customization and sharing of queries.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.simple-talk.com/blogbits/Clive.Tong/SomenotesonReflector7_77BA/image_6.png"&gt;&lt;img title="image" border="0" alt="image" src="http://www.simple-talk.com/blogbits/Clive.Tong/SomenotesonReflector7_77BA/image_thumb_6.png" width="606" height="406" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The PowerCommands add-in contains many other useful utilities. For example, you can open an item using an external application, work with enumeration bit flags, or generate assembly binding redirect files. You can see &lt;a href="http://www.simple-talk.com/community/blogs/bart/archive/2011/01/10/98258.aspx"&gt;Bart's earlier post&lt;/a&gt; for a more complete list. &lt;/p&gt;  &lt;h4&gt;.NET Reflector VS&lt;/h4&gt;  &lt;p&gt;.NET Reflector VS adds a brand new Reflector object browser into Visual Studio to save you time opening .NET Reflector separately and browsing for an object. A 'Decompile and Explore' option is also added to the context menu of references in the Solution Explorer, so you don't need to leave Visual Studio to look through decompiled code. We've also added some simple navigation features to allow you to move through the decompiled code as quickly and easily as you can in .NET Reflector.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.simple-talk.com/blogbits/Clive.Tong/SomenotesonReflector7_77BA/image_7.png"&gt;&lt;img title="image" border="0" alt="image" src="http://www.simple-talk.com/blogbits/Clive.Tong/SomenotesonReflector7_77BA/image_thumb_7.png" width="264" height="272" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;When this is selected, the add-in decompiles the given assembly,&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.simple-talk.com/blogbits/Clive.Tong/SomenotesonReflector7_77BA/image_8.png"&gt;&lt;img title="image" border="0" alt="image" src="http://www.simple-talk.com/blogbits/Clive.Tong/SomenotesonReflector7_77BA/image_thumb_8.png" width="448" height="296" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Once the decompilation has finished, a clone of the Reflector assembly explorer can be used inside Visual Studio.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.simple-talk.com/blogbits/Clive.Tong/SomenotesonReflector7_77BA/image_9.png"&gt;&lt;img title="image" border="0" alt="image" src="http://www.simple-talk.com/blogbits/Clive.Tong/SomenotesonReflector7_77BA/image_thumb_9.png" width="606" height="386" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;When Reflector generates the source code, it records the location information. You can therefore navigate from the source file to other decompiled source using the 'Go To Definition' context menu item.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.simple-talk.com/blogbits/Clive.Tong/SomenotesonReflector7_77BA/image_10.png"&gt;&lt;img title="image" border="0" alt="image" src="http://www.simple-talk.com/blogbits/Clive.Tong/SomenotesonReflector7_77BA/image_thumb_10.png" width="606" height="362" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;This then takes you to the definition in another decompiled assembly.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.simple-talk.com/blogbits/Clive.Tong/SomenotesonReflector7_77BA/image_11.png"&gt;&lt;img title="image" border="0" alt="image" src="http://www.simple-talk.com/blogbits/Clive.Tong/SomenotesonReflector7_77BA/image_thumb_11.png" width="606" height="386" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;h4&gt;.NET Reflector VSPro&lt;/h4&gt;  &lt;p&gt;.NET Reflector VSPro builds on the features in .NET Reflector VS to add the ability to debug any source code you decompile. When you decompile with .NET Reflector VSPro, a matching .pdb is generated, so you can use Visual Studio to debug the source code as if it were part of the project. You can now use all the standard debugging techniques that you are used to in the Visual Studio debugger, and step through decompiled code as if it were your own.&lt;/p&gt;  &lt;p&gt;Again, you can select assemblies for decompilation.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.simple-talk.com/blogbits/Clive.Tong/SomenotesonReflector7_77BA/image_12.png"&gt;&lt;img title="image" border="0" alt="image" src="http://www.simple-talk.com/blogbits/Clive.Tong/SomenotesonReflector7_77BA/image_thumb_12.png" width="606" height="386" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;They are then decompiled.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.simple-talk.com/blogbits/Clive.Tong/SomenotesonReflector7_77BA/image_13.png"&gt;&lt;img title="image" border="0" alt="image" src="http://www.simple-talk.com/blogbits/Clive.Tong/SomenotesonReflector7_77BA/image_thumb_13.png" width="605" height="335" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;And then you can debug as if they were one of your own source code files.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.simple-talk.com/blogbits/Clive.Tong/SomenotesonReflector7_77BA/image_14.png"&gt;&lt;img title="image" border="0" alt="image" src="http://www.simple-talk.com/blogbits/Clive.Tong/SomenotesonReflector7_77BA/image_thumb_14.png" width="606" height="386" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;b&gt;The future of .NET Reflector&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;As I have mentioned throughout this post, most of the new features in version 7 are exploratory steps and we will be watching feedback closely. Although we don't want to speculate now about any other new features or bugs that will or won't be fixed in the next few versions of .NET Reflector, Bart has mentioned in a previous post that there are lots of improvements we intend to make. We plan to do this with great care and without taking anything away from the simplicity of the core product.&lt;/p&gt;  &lt;p&gt;User experience is something that we pride ourselves on at Red Gate, and it is clear that Reflector is still a long way off our usual standards. We plan for the next few versions of Reflector to be worked on by some of our top usability specialists who have been involved with our other market-leading products such as the ANTS Profilers and SQL Compare. I re-iterate the need for the really great &lt;u&gt;simple&lt;/u&gt; mode in .NET Reflector to remain intact regardless of any other improvements we are planning to make. &lt;/p&gt;  &lt;p&gt;I really hope that you enjoy using some of the new features in version 7 and that Reflector continues to be your favourite .NET development tool for a long time to come.&lt;/p&gt;&lt;img src="http://www.simple-talk.com/community/aggbug.aspx?PostID=99118" width="1" height="1"&gt;</content><author><name>CliveT</name><uri>http://www.simple-talk.com/community/user/Profile.aspx?UserID=17260</uri></author></entry><entry><title>We've completed the first iteration</title><link rel="alternate" type="text/html" href="http://www.simple-talk.com/community/blogs/clivet/archive/2010/12/16/96199.aspx" /><id>http://www.simple-talk.com/community/blogs/clivet/archive/2010/12/16/96199.aspx</id><published>2010-12-16T00:00:00Z</published><updated>2010-12-16T00:00:00Z</updated><content type="html">&lt;p&gt;There are a lot of features in C# that are implemented by the compiler and not by the underlying platform.    &lt;br /&gt;    &lt;br /&gt;One such feature is a lambda expression. Since local variables cannot be accessed once the current method activation finishes, the compiler has to go out of its way to generate a new class which acts as a home for any variable whose lifetime needs to be extended past the activation of the procedure.     &lt;br /&gt;    &lt;br /&gt;Take the following example:     &lt;br /&gt;    &lt;br /&gt;    Random generator = new Random();     &lt;br /&gt;    Func func = () =&gt; generator.Next(10); &lt;/p&gt;  &lt;p&gt;In this case, the compiler generates a new class called &lt;&gt;c_DisplayClass1 which is marked with the CompilerGenerated attribute.    &lt;br /&gt;    &lt;br /&gt;[CompilerGenerated]     &lt;br /&gt;private sealed class &lt;&gt;c__DisplayClass1     &lt;br /&gt;{     &lt;br /&gt;    // Fields     &lt;br /&gt;    public Random generator; &lt;/p&gt;  &lt;p&gt;    // Methods    &lt;br /&gt;    public int b__0()     &lt;br /&gt;    {     &lt;br /&gt;        return this.generator.Next(10);     &lt;br /&gt;    }     &lt;br /&gt;} &lt;/p&gt;  &lt;p&gt;Two quick comments on this:    &lt;br /&gt;    &lt;br /&gt;(i)    A display was the means that compilers for languages like Algol recorded the various lexical contours of the nested procedure activations on the stack. I imagine that this is what has led to the name.     &lt;br /&gt;    &lt;br /&gt;(ii)    It is a shame that the same attribute is used to mark all compiler generated classes as it makes it hard to figure out what they are being used for. Indeed, you could imagine optimisations that the runtime could perform if it knew that classes corresponded to certain high level concepts. &lt;/p&gt;  &lt;p&gt;We can see that the local variable generator has been turned into a field in the class, and the body of the lambda expression has been turned into a method of the new class. The code that builds the Func object simply constructs an instance of this class and initialises the fields to their initial values.    &lt;br /&gt;    &lt;br /&gt;    &lt;&gt;c__DisplayClass1 class2 = new &lt;&gt;c__DisplayClass1();     &lt;br /&gt;    class2.generator = new Random();     &lt;br /&gt;    Func func = new Func(class2.b__0); &lt;/p&gt;  &lt;p&gt;Reflector already contains code to spot this pattern of code and reproduce the form containing the lambda expression, so this is example is correctly decompiled.    &lt;br /&gt;    &lt;br /&gt;The use of compiler generated code is even more spectacular in the case of iterators. C# introduced the idea of a method that could automatically store its state between calls, so that it can pick up where it left off. The code can express the logical flow with yield return and yield break denoting places where the method should return a particular value and be prepared to resume.     &lt;br /&gt;    &lt;br /&gt;        {     &lt;br /&gt;            yield return 1;     &lt;br /&gt;            yield return 2;     &lt;br /&gt;            yield return 3;     &lt;br /&gt;        } &lt;/p&gt;  &lt;p&gt;Of course, there was already a .NET pattern for expressing the idea of returning a sequence of values with the computation proceeding lazily (in the sense that the work for the next value is executed on demand). This is expressed by the IEnumerable interface with its Current property for fetching the current value and the MoveNext method for forcing the computation of the next value. The sequence is terminated when this method returns false.    &lt;br /&gt;    &lt;br /&gt;The C# compiler links these two ideas together so that an IEnumerator returning method using the yield keyword causes the compiler to produce the implementation of an Iterator.     &lt;br /&gt;    &lt;br /&gt;Take the following piece of code.     &lt;br /&gt;    &lt;br /&gt;        IEnumerable GetItems()     &lt;br /&gt;        {     &lt;br /&gt;            yield return 1;     &lt;br /&gt;            yield return 2;     &lt;br /&gt;            yield return 3;     &lt;br /&gt;        } &lt;/p&gt;  &lt;p&gt;The compiler implements this by defining a new class that implements a state machine. This has an integer state that records which yield point we should go to if we are resumed. It also has a field that records the Current value of the enumerator and a field for recording the thread. This latter value is used for optimising the creation of iterator instances.    &lt;br /&gt;    &lt;br /&gt;[CompilerGenerated]     &lt;br /&gt;private sealed class d__0 : IEnumerable, IEnumerable, IEnumerator, IEnumerator, IDisposable     &lt;br /&gt;{     &lt;br /&gt;    // Fields     &lt;br /&gt;    private int &lt;&gt;1__state;     &lt;br /&gt;    private int &lt;&gt;2__current;     &lt;br /&gt;    public Program &lt;&gt;4__this;     &lt;br /&gt;    private int &lt;&gt;l__initialThreadId; &lt;/p&gt;  &lt;p&gt;The body gets converted into the code to construct and initialize this new class.    &lt;br /&gt;    &lt;br /&gt;private IEnumerable GetItems()     &lt;br /&gt;{     &lt;br /&gt;    d__0 d__ = new d__0(-2);     &lt;br /&gt;    d__.&lt;&gt;4__this = this;     &lt;br /&gt;    return d__;     &lt;br /&gt;} &lt;/p&gt;  &lt;p&gt;When the class is constructed we set the state, which was passed through as -2 and the current thread. &lt;/p&gt;  &lt;p&gt;public d__0(int &lt;&gt;1__state)    &lt;br /&gt;{     &lt;br /&gt;    this.&lt;&gt;1__state = &lt;&gt;1__state;     &lt;br /&gt;    this.&lt;&gt;l__initialThreadId = Thread.CurrentThread.ManagedThreadId;     &lt;br /&gt;} &lt;/p&gt;  &lt;p&gt;The state needs to be set to 0 to represent a valid enumerator and this is done in the GetEnumerator method which optimises for the usual case where the returned enumerator is only used once. &lt;/p&gt;  &lt;p&gt;IEnumerator IEnumerable.GetEnumerator()    &lt;br /&gt;{     &lt;br /&gt;    if ((Thread.CurrentThread.ManagedThreadId == this.&lt;&gt;l__initialThreadId)     &lt;br /&gt;              &amp;&amp; (this.&lt;&gt;1__state == -2))     &lt;br /&gt;    {     &lt;br /&gt;        this.&lt;&gt;1__state = 0;     &lt;br /&gt;        return this;     &lt;br /&gt;    } &lt;/p&gt;  &lt;p&gt;The state machine itself is implemented inside the MoveNext method.    &lt;br /&gt;    &lt;br /&gt;private bool MoveNext()     &lt;br /&gt;{     &lt;br /&gt;    switch (this.&lt;&gt;1__state)     &lt;br /&gt;    {     &lt;br /&gt;        case 0:     &lt;br /&gt;            this.&lt;&gt;1__state = -1;     &lt;br /&gt;            this.&lt;&gt;2__current = 1;     &lt;br /&gt;            this.&lt;&gt;1__state = 1;     &lt;br /&gt;            return true; &lt;/p&gt;  &lt;p&gt;        case 1:    &lt;br /&gt;            this.&lt;&gt;1__state = -1;     &lt;br /&gt;            this.&lt;&gt;2__current = 2;     &lt;br /&gt;            this.&lt;&gt;1__state = 2;     &lt;br /&gt;            return true; &lt;/p&gt;  &lt;p&gt;        case 2:    &lt;br /&gt;            this.&lt;&gt;1__state = -1;     &lt;br /&gt;            this.&lt;&gt;2__current = 3;     &lt;br /&gt;            this.&lt;&gt;1__state = 3;     &lt;br /&gt;            return true; &lt;/p&gt;  &lt;p&gt;        case 3:    &lt;br /&gt;            this.&lt;&gt;1__state = -1;     &lt;br /&gt;            break;     &lt;br /&gt;    }     &lt;br /&gt;    return false;     &lt;br /&gt;} &lt;/p&gt;  &lt;p&gt;At each stage, the current value of the state is used to determine how far we got, and then we generate the next value which we return after recording the next state. Finally we return false from the MoveNext to signify the end of the sequence.    &lt;br /&gt;    &lt;br /&gt;Of course, that example was really simple. The original method body didn't have any local variables. Any local variables need to live between the calls to MoveNext and so they need to be transformed into fields in much the same way that we did in the case of the lambda expression. More complicated MoveNext methods are required to deal with resources that need to be disposed when the iterator finishes, and sometimes the compiler uses a temporary variable to hold the return value.     &lt;br /&gt;    &lt;br /&gt;Why all of this explanation?     &lt;br /&gt;    &lt;br /&gt;We've implemented the de-compilation of iterators in the current EAP version of Reflector (7).     &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="/blogbits/Clive.Tong/Wevecompletedthefirstiteration_C67F/pic1.png"&gt;&lt;img title="pic1" border="0" alt="pic1" src="/blogbits/Clive.Tong/Wevecompletedthefirstiteration_C67F/pic1_thumb.png" width="828" height="437" /&gt;&lt;/a&gt;     &lt;br /&gt;This contrasts with previous version where all you could do was look at the MoveNext method and try to figure out the control flow.&lt;/p&gt;  &lt;p&gt;&lt;a href="/blogbits/Clive.Tong/Wevecompletedthefirstiteration_C67F/pic2.png"&gt;&lt;img title="pic2" border="0" alt="pic2" src="/blogbits/Clive.Tong/Wevecompletedthefirstiteration_C67F/pic2_thumb.png" width="830" height="438" /&gt;&lt;/a&gt;     &lt;br /&gt;    &lt;br /&gt;There's a fair amount of things we have to do. We have to spot the use of a CompilerGenerated class which implements the Enumerator pattern. We need to go to the class and figure out the fields corresponding to the local variables. We then need to go to the MoveNext method and try to break it into the various possible states and spot the state transitions. We can then take these pieces and put them back together into an object model that uses yield return to show the transition points. After that Reflector can carry on optimising using its usual optimisations.     &lt;br /&gt;    &lt;br /&gt;The pattern matching is currently a little too sensitive to changes in the code generation, and we only do a limited analysis of the MoveNext method to determine use of the compiler generated fields. In some ways, it is a pity that iterators are compiled away and there is no metadata that reflects the original intent. Without it, we are always going to dependent on our knowledge of the compiler's implementation. For example, we have noticed that the Async CTP changes the way that iterators are code generated, so we'll have to do some more work to support that.     &lt;br /&gt;    &lt;br /&gt;However, with that warning in place, we seem to do a reasonable job of decompiling the iterators that are built into the framework. Hopefully, the EAP will give us a chance to find examples where we don't spot the pattern correctly or regenerate the wrong code, and we can improve things.     &lt;br /&gt;    &lt;br /&gt;Please give it a go, and report any problems.&lt;/p&gt;&lt;img src="http://www.simple-talk.com/community/aggbug.aspx?PostID=96199" width="1" height="1"&gt;</content><author><name>CliveT</name><uri>http://www.simple-talk.com/community/user/Profile.aspx?UserID=17260</uri></author></entry><entry><title>The Joy of Clojure (book review)</title><link rel="alternate" type="text/html" href="http://www.simple-talk.com/community/blogs/clivet/archive/2010/09/30/94827.aspx" /><id>http://www.simple-talk.com/community/blogs/clivet/archive/2010/09/30/94827.aspx</id><published>2010-09-30T15:02:35Z</published><updated>2010-09-30T15:02:35Z</updated><content type="html">&lt;p&gt;It's been a long time since Lisp and Scheme have been in the news. Despite the abilities of these languages to enable fast prototyping of complicated applications, they got overlooked in the old days due to people's suspicion of garbage collection and their rather strange syntax.   &lt;br /&gt;    &lt;br /&gt;In more modern times, the take up of virtual machine technology and associated technologies such as garbage collection has made it possible for some of these older languages to re-emerge. Indeed, not a day goes by without another language appearing which targets the Java Virtual Machine (JVM). One such language is Clojure, which is making quite a splash. Clojure takes some of the ideas behind Lisp and Scheme, mixes in some other ideas from functional programming, and blends this together with a large dose of practicality. The result is a language that runs nicely on top of the JVM and which allows the programmer to interoperate very easily with existing Java libraries and frameworks.&amp;#160; &lt;br /&gt;    &lt;br /&gt;No more does the Lisp system have to be in charge of the process. Clojure code compiles down to Java class files which can loaded and exercised by a non-Clojure controller. Of course, things are not completely rosy. When you are debugging mixed- language applications, you get to see what the datatypes really look like and see more of the implementation details than you'd really like. However, we are now at a stage where different parts of a problem can be solved by a language which suits that particular problem area, and then these parts can be linked to form a coherent whole.     &lt;br /&gt;    &lt;br /&gt;Lisp has had some notable successes in the past. Paul Graham's ViaWeb (which was bought by Yahoo! in the early days of the web) is one example, and, more recently, some of the travel scheduling companies have used this language to good effect. Clojure might just be one way of getting these advantages into the hands of the programmer-in-the-office. Lisp is very extensible and Clojure's creator, Rich Hickey, has used this to add many interesting concurrency constructs into the language. Since multi-core concurrency is the new hot topic, this makes the language even more deserving of consideration by the average programmer.&amp;#160; &lt;br /&gt;    &lt;br /&gt;The Joy of Clojure is a book which tries to show experienced programmers how Clojure can solve some of the common problems that they have in their day jobs. Section 1.3 of the introduction neatly decomposes the problems with many other languages into the categories: verbosity, unavoidable boiler-plate, a long thought-code-feedback loop, incidental complexity, difficulties in extension, and deficiencies in supporting crucial programming paradigms.&amp;#160; The book does a good job at relating these aspects to the parts of Clojure that help with them, helping to communicate the "why?" instead of just the "how?"    &lt;br /&gt;    &lt;br /&gt;The book starts quickly, and continues at quite a pace. It is full of lots of interesting observations about programming in general and references lots of material from lots of sources. Chapter one, for example, starts out with the three pillars of Clojure - simplicity, freedom to focus and empowerment, and then leaps into some fairly large examples of the language's extensibility and its support for encapsulation, by way of an embedded Domain Specific Language (DSL) for a SQL query and the implementation of a chessboard. This chapter also does a really good job of explaining the rationale for functional programming via the metaphor of a child's flip book, and also explains how Clojure can solve the Expression problem using protocols. The only trouble covering material at this pace is that we are left with lots of forward references to the following chapters. This happens frequently throughout the book, and it's always unclear whether it is worth jumping forwards to follow up on something, or whether we should wait to reach the material. Personally, I like this style, but I imagine some people might find it annoying. The book is definitely worth a couple of reads and on the second pass through the forward referencing isn't quite as annoying.    &lt;br /&gt;    &lt;br /&gt;Chapter 2 of the book covers the basic datatypes and syntax of the Clojure language, starting with basic strings and integers, and then covering the constructed types of list, vector, map and set. The emphasis on the latter two types is what makes this language different from more mainstream languages. We then move on to defining functions including loops and recursion, and then cover very basic Java interoperability and how namespaces affect the visibility of definitions.    &lt;br /&gt;    &lt;br /&gt;Chapters 3, 4 and 5 go back through the scalars and the composite datatypes in more detail, encouraging the reader to experiment at the REPL, (the read-eval-print-loop), which is the means by which Clojure allows the programmer to experiment with the definitions that they have just made. Programming can be a very exploratory activity, and this is the way Clojure makes this possible.    &lt;br /&gt;    &lt;br /&gt;Sequences in Clojure are a nice unifying concept. Most of the datatypes can be viewed as sequences, and chapter 6 takes this to the next level by explaining laziness and how it allows very succinct definitions of things like the triangle numbers. In this case, rather than defining a function to return the nth triangle number, it is much easier to define a lazy sequence of such numbers and then use the standard sequence functions to select a sub-range. This chapter contains a large worked example of implementing a lazy version of quicksort. This example is fascinating, especially as we can generate the first element of the sorted result by asking for the first element, and letting the laziness infer the work that needs to be done.     &lt;br /&gt;    &lt;br /&gt;Chapter 7 covers functional programming in more depth, concentrating on closures and higher order functions. This chapter finishes with an example implementation of the A*search algorithm. Examples like these are one of the strongest parts of the book. Most chapters have examples that were well chosen, being neither too complicated (which tends to obfuscate the point) nor too easy (which misses the chance to show language features in context).    &lt;br /&gt;    &lt;br /&gt;Chapter 8 covers macros, and does a reasonable job of covering all of the related material, with an emphasis on code-as-data. Again there's a good example to illustrate the material.    &lt;br /&gt;    &lt;br /&gt;Chapter 9 moves on to more details about namespaces, multi-methods and records and protocols. This was useful material which was well presented, though having such disparate topics in the same chapter felt a little strange.    &lt;br /&gt;    &lt;br /&gt;Chapter 10 covers the interoperability story, showing how easy it is to call Java methods, and how easy it is to expose functionality written in Clojure to the outside world. I've used Clojure a fair amount in my spare time and certainly learned some things from this chapter.    &lt;br /&gt;    &lt;br /&gt;Chapter 11, entitled Mutation, covers the stateful side of the Clojure story, covering the software transactional memory (via Refs), agents, atoms and vars, with some additional material on using standard Java locks. There's also coverage of the primitives for launching parallel tasks as well as futures and promises. Clojure has a big emphasis on stateless programming, but coordinated changes of application state are required to write anything but trivial applications.    &lt;br /&gt;    &lt;br /&gt;Chapter 12 covers performance, detailing how you can aid the compiler with type hints, which allow it to avoid boxing primitive values too frequently. It also covers local mutation in order to improve performance by using transients, and has a good discussion of memorization.    &lt;br /&gt;    &lt;br /&gt;The final chapter, chapter 13, makes some final points about the usefulness of Clojure. It discusses how easy it is to define embedded DSLs, and how some of traditional design patterns can be implemented without requiring a lot of scaffolding.    &lt;br /&gt;    &lt;br /&gt;This is a good book, which explains very well the major parts of the Clojure language, and contrasts the Clojure style against that of more mainstream languages. With such a range of concepts and ideas, the material is fairly well organised, though some chapters don't really have a single theme running through them. In some cases, the author references other external material, and it is a pity that there isn't time to go into more depth in the text itself. However, at 416 pages, it's clear that there just isn't the space to do this.    &lt;br /&gt;    &lt;br /&gt;The examples concentrate on the language itself. There is nothing to show you how to write database access code in Clojure&amp;#160; or how to hook up to a web server, or even how to use a build tool such as Leiningen to package your Clojure code into a deployable application. By focusing purely on the language instead of its use, the book can go into more technical depth than some other texts.    &lt;br /&gt;    &lt;br /&gt;The Joy of Clojure explains both the "how" and "why" of Clojure, as it promises it will in the introductory chapter, and it is well worth a read. If you haven't done any Lisp or Scheme before, you might be better off reading a more introductory text first, but I can't see any book explaining the essence of Clojure better than this one.&lt;/p&gt;&lt;img src="http://www.simple-talk.com/community/aggbug.aspx?PostID=94827" width="1" height="1"&gt;</content><author><name>CliveT</name><uri>http://www.simple-talk.com/community/user/Profile.aspx?UserID=17260</uri></author></entry><entry><title>F# and the rose-tinted reflection</title><link rel="alternate" type="text/html" href="http://www.simple-talk.com/community/blogs/clivet/archive/2010/05/20/91426.aspx" /><id>http://www.simple-talk.com/community/blogs/clivet/archive/2010/05/20/91426.aspx</id><published>2010-05-20T10:08:03Z</published><updated>2010-05-20T10:08:03Z</updated><content type="html">&lt;p&gt;We're already seeing increasing use of many cores on client desktops. It is a change that has been long predicted. It is not just a change in architecture, but our notions of efficiency in a program. No longer can we focus on the asymptotic complexity of an algorithm by counting the steps that a single core processor would take to execute it. Instead we'll soon be more concerned about the scalability of the algorithm and how well we can increase the performance as we increase the number of cores. This may even lead us to throw away our most efficient algorithms, and switch to less efficient algorithms that scale better. We might even be willing to waste cycles in order to speculatively execute at the algorithm rather than the hardware level.&lt;/p&gt;  &lt;p&gt;State is the big headache in this parallel world. At the hardware level, main memory doesn't necessarily contain the definitive value corresponding to a particular address. An update to a location might still be held in a CPU's local cache and it might be some time before the value gets propagated. To get the latest value, and the notion of &amp;quot;latest&amp;quot; takes a lot of defining in this world of rapidly mutating state, the CPUs may well need to communicate to decide who has the definitive value of a particular address in order to avoid lost updates. At the user program level, this means programmers will need to lock objects before modifying them, or attempt to avoid the overhead of locking by understanding the memory models at a very deep level.&lt;/p&gt;  &lt;p&gt;I think it's this need to avoid statefulness that has led to the recent resurgence of interest in functional languages. In the 1980s, functional languages started getting traction when research was carried out into how programs in such languages could be auto-parallelised. Sadly, the impracticality of some of the languages, the overheads of communication during this parallel execution, and rapid improvements in compiler technology on stock hardware meant that the functional languages fell by the wayside. The one thing that these languages were good at was getting rid of implicit state, and this single idea seems like a solution to the problems we are going to face in the coming years.&lt;/p&gt;  &lt;p&gt;Whether these languages will catch on is hard to predict. The mindset for writing a program in a functional language is really very different from the way that object-oriented problem decomposition happens - one has to focus on the verbs instead of the nouns, which takes some getting used to.&lt;/p&gt;  &lt;p&gt;There are a number of hybrid functional/object languages that have been becoming more popular in recent times. These half-way houses make it easy to use functional ideas for some parts of the program while still allowing access to the underlying object-focused platform without a great deal of impedance mismatch. One example is F# running on the CLR which, in Visual Studio 2010, has because a first class member of the pack. Inside Visual Studio 2010, the tooling for F# has improved to the point where it is easy to set breakpoints and watch values change while debugging at the source level. &lt;/p&gt;  &lt;p&gt;In my opinion, it is the tooling support that will enable the widespread adoption of functional languages - without this support, people will put off any transition into the functional world for as long as they possibly can. Without tool support it will make it hard to learn these languages.&lt;/p&gt;  &lt;p&gt;One tool that doesn't currently support F# is Reflector. The idea of decompiling IL to a functional language is daunting, but F# is potentially so important I couldn't dismiss the idea. As I'm currently developing Reflector 6.5, I thought it wise to take four days just to see how far I could get in doing so, even if it achieved little more than to be clearer on how much was possible, and how long it might take. You can &lt;a href="http://www.simple-talk.com/community/blogs/clivet/archive/2010/05/20/91425.aspx"&gt;read what happened here&lt;/a&gt;, and of the insights it gave us on ways to improve the tool.&lt;/p&gt;&lt;img src="http://www.simple-talk.com/community/aggbug.aspx?PostID=91426" width="1" height="1"&gt;</content><author><name>CliveT</name><uri>http://www.simple-talk.com/community/user/Profile.aspx?UserID=17260</uri></author></entry><entry><title>Towards an F# .NET Reflector add-in</title><link rel="alternate" type="text/html" href="http://www.simple-talk.com/community/blogs/clivet/archive/2010/05/20/91425.aspx" /><id>http://www.simple-talk.com/community/blogs/clivet/archive/2010/05/20/91425.aspx</id><published>2010-05-20T10:04:00Z</published><updated>2010-05-20T10:04:00Z</updated><content type="html">&lt;p&gt;When I had the opportunity to spent some time during Red Gate's recent &lt;a href="http://www.youtube.com/watch?v=nitqtm_ILG8" target="_blank"&gt;"down tools" week&lt;/a&gt; on a project of my choice, the obvious project was an F# add-in for Reflector . To be honest, this was a bit of a misnomer as the amount of time in the designated week for coding was really less than three days, so it was always unlikely that very much progress would be made in such a small amount of time (and that certainly proved to be the case), but I did learn some things from the experiment.&lt;/p&gt;  &lt;p&gt;Like lots of problems, one useful technique is to take examples, get them to work, and then generalise to get something that works across the board. Unfortunately, I didn't have enough time to do the last stage.&lt;/p&gt;  &lt;p&gt;The obvious first step is to take a few function definitions, starting with the obvious hello world, moving on to a non-recursive function and finishing with the ubiquitous recursive Fibonacci function.&lt;/p&gt;  &lt;p&gt;let rec printMessage message&amp;nbsp; =    &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; printfn&amp;nbsp; message&lt;/p&gt;  &lt;p&gt;let foo x&amp;nbsp; =    &lt;br&gt;&amp;nbsp;&amp;nbsp; (x + 1) &lt;/p&gt;  &lt;p&gt;let rec fib x&amp;nbsp; =    &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (x &amp;gt;= 2) then (fib (x - 1) + fib (x - 2)) else 1&lt;/p&gt;  &lt;p&gt;The major problem in decompiling these simple functions is that Reflector has an in-memory object model that is designed to support object-oriented languages. In particular it has a return statement that allows function bodies to finish early. I used some of the in-built functionality to take the IL and produce an in-memory object model for the language, but then needed to write a transformer to push the return statements to the top of the tree to make it easy to render the code into a functional language. This tree transform works in some scenarios, but not in others where we simply regenerate code that looks more like CPS style.&lt;/p&gt;  &lt;p class="illustration"&gt;&lt;img src="/blogbits/Clive.Tong/Clive1.jpg"&gt; &lt;/p&gt;  &lt;p class="illustration"&gt;&lt;img src="/blogbits/Clive.Tong/Clive2.jpg"&gt; &lt;/p&gt;  &lt;p&gt;The next thing to get working was library level bindings of values where these values are calculated at runtime.&lt;/p&gt;  &lt;p&gt;let x = [1 ; 2 ; 3 ; 4]&lt;/p&gt;  &lt;p&gt;let y = List.map&amp;nbsp; (fun x -&amp;gt; foo x) x&lt;/p&gt;  &lt;p&gt;The way that this is translated into a set of classes for the underlying platform means that the code needs to follow references around, from the property exposing the calculated value to the class in which the code for generating the value is embedded.&lt;/p&gt;  &lt;p class="illustration"&gt;&lt;img src="/blogbits/Clive.Tong/Clive3.jpg"&gt; &lt;/p&gt;  &lt;p class="illustration"&gt;&lt;img src="/blogbits/Clive.Tong/Clive4.jpg"&gt; &lt;/p&gt;  &lt;p&gt;One of the strongest selling points of functional languages is the algebraic datatypes, which allow definitions via standard mathematical-style inductive definitions across the union cases.&lt;/p&gt;  &lt;p&gt;type Foo =    &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; | Something of int     &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; | Nothing &lt;/p&gt;  &lt;p&gt;type 'a Foo2 =    &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; | Something2 of 'a     &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; | Nothing2 &lt;/p&gt;  &lt;p&gt;Such a definition is compiled into a number of classes for the cases of the union, which all inherit from a class representing the type itself. It wasn't too hard to get such a de-compilation happening in the cases I tried.&lt;/p&gt;  &lt;p class="illustration"&gt;&lt;img src="/blogbits/Clive.Tong/Clive5.jpg"&gt; &lt;/p&gt;  &lt;p class="illustration"&gt;&lt;img src="/blogbits/Clive.Tong/Clive6.jpg"&gt; &lt;/p&gt;  &lt;p&gt;What did I learn from this?&lt;/p&gt;  &lt;p&gt;Firstly, that there are various bits of functionality inside Reflector that it would be useful for us to allow add-in writers to access. In particular, there are various implementations of the Visitor pattern which implement algorithms such as calculating the number of references for particular variables, and which perform various substitutions which could be more generally useful to add-in writers. I hope to do something about this at some point in the future. &lt;/p&gt;  &lt;p&gt;Secondly, when you transform a functional language into something that runs on top of an object-based platform, you lose some fidelity in the representation. The F# compiler leaves attributes in place so that tools can tell which classes represent classes from the source program and which are there for purposes of the implementation, allowing the decompiler to regenerate these constructs again. However, decompilation technology is a long way from being able to take unannotated IL and transform it into a program in a different language. For a simple function definition, like Fibonacci, I could write a simple static function and have it come out in F# as the same function, but it would be practically impossible to take a mass of class definitions and have a decompiler translate it automatically into an F# algebraic data type.&lt;/p&gt;  &lt;p&gt;What have we got out of this?&lt;/p&gt;  &lt;p&gt;Some data on the feasibility of implementing an F# decompiler inside Reflector, though it's hard at the moment to say how long this would take to do. The work we did is included the 6.5 EAP for Reflector that you can get from the &lt;a href="http://www.red-gate.com/messageboard/viewforum.php?f=117" target="_blank"&gt;EAP forum&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;All things considered though, it was a useful way to gain more familiarity with the process of writing an add-in and understand difficulties other add-in authors might experience.&lt;/p&gt;&lt;img src="http://www.simple-talk.com/community/aggbug.aspx?PostID=91425" width="1" height="1"&gt;</content><author><name>CliveT</name><uri>http://www.simple-talk.com/community/user/Profile.aspx?UserID=17260</uri></author></entry><entry><title>.Net Reflector 6.5 EAP now available</title><link rel="alternate" type="text/html" href="http://www.simple-talk.com/community/blogs/clivet/archive/2010/03/26/90409.aspx" /><id>http://www.simple-talk.com/community/blogs/clivet/archive/2010/03/26/90409.aspx</id><published>2010-03-26T06:29:00Z</published><updated>2010-03-26T06:29:00Z</updated><content type="html">&lt;P&gt;With the release of CLR 4 being so close, we’ve been working hard on getting the new C# and VB language features implemented inside Reflector. The work isn’t complete yet, but we have some of the features working. Most importantly, there are going to be changes to the Reflector object model, and we though it would be useful for people to see the changes and have an opportunity to comment on them.
&lt;P&gt;Before going any further, we should tell you what the EAP contains that’s different from the released version.
&lt;P&gt;A number of bugs have been fixed, mainly bugs that were raised via the forum. This is slightly offset by the fact that this EAP hasn’t had a whole lot of testing and there may have been new bugs introduced during the development work we’ve been doing.
&lt;P&gt;The C# language writer has been changed to display in and out co- and contra-variance markers on interfaces and delegates, and to display default values for optional parameters in method definitions. We also concisely display values passed by reference into COM calls. However, we do not change callsites to display calls using named parameters; this looks like hard work to get right.
&lt;P&gt;The forthcoming version of the C# language introduces dynamic types and dynamic calls. The new version of Reflector should display a dynamic call rather than the generated C#:
&lt;PRE&gt;
  dynamic target = MyTestObject();
  target.Hello("Mum");
&lt;/PRE&gt;
&lt;P&gt;We have a few bugs in this area where we are not casting to dynamic when necessary. These have been fixed on a branch and should make their way into the next EAP.
&lt;P&gt;To support the dynamic features, we’ve added the types IDynamicMethodReferenceExpression, IDynamicPropertyIndexerExpression, and IDynamicPropertyReferenceExpression to the object model. These types, based on the versions without “Dynamic” in the name, reflect the fact that we don’t have full information about the method that is going to be called, but only have its name (as a string). These interfaces are going to change – in an internal version, they have been extended to include information about which parameter positions use runtime types and which use compile time types. There’s also the interface, IDynamicVariableDeclaration, that can be used to determine if a particular variable is used at dynamic call sites as a target. 
&lt;P&gt;A couple of these language changes have also been added to the Visual Basic language writer.
&lt;P&gt;The new features are exposed only when the optimization level is set to .NET 4. When the level is set this high, the other standard language writers will simply display a message to say that they do not handle such an optimization level. Reflector Pro now has 4.0 as an optional compilation target and we have done some work to get the pdb generation right for these new features.
&lt;P&gt;The EAP version of Reflector no longer installs the add-in on startup. The first time you run the EAP, it displays the integration options dialog. You can use the checkboxes to select the versions of Visual Studio into which you want to install the EAP version. Note that you can only have one version of Reflector Pro installed in Visual Studio; if you install into a Visual Studio that has another version installed, the previous version will be removed.
&lt;P&gt;Please try it out and send your feedback to the &lt;A href="http://www.red-gate.com/MessageBoard/viewforum.php?f=117"&gt;EAP forum&lt;/A&gt;.&lt;img src="http://www.simple-talk.com/community/aggbug.aspx?PostID=90409" width="1" height="1"&gt;</content><author><name>CliveT</name><uri>http://www.simple-talk.com/community/user/Profile.aspx?UserID=17260</uri></author></entry><entry><title>Reflector Pro has now been released!</title><link rel="alternate" type="text/html" href="http://www.simple-talk.com/community/blogs/clivet/archive/2010/02/15/89883.aspx" /><id>http://www.simple-talk.com/community/blogs/clivet/archive/2010/02/15/89883.aspx</id><published>2010-02-15T04:56:00Z</published><updated>2010-02-15T04:56:00Z</updated><content type="html">&lt;P&gt;After &lt;A href="/community/blogs/clivet/archive/2009/07/20/74071.aspx"&gt;moving into the .NET division in May &lt;/A&gt;, and having a great time working on Reflector, I'm pleased to say that the results of that work are &lt;A href="http://www.red-gate.com/products/reflector/index.htm?utm_source=simpletalk&amp;amp;utm_medium=blog&amp;amp;utm_content=reflector6release-cliveblog&amp;amp;utm_campaign=reflector"&gt;now available&lt;/A&gt;. &lt;A href="http://www.red-gate.com/messageboard/viewtopic.php?t=10519&amp;amp;utm_source=simpletalk&amp;amp;utm_medium=blog&amp;amp;utm_content=reflector6release-cliveblog&amp;amp;utm_campaign=reflector"&gt;Reflector Pro has now been released&lt;/A&gt;!&lt;/P&gt;
&lt;P&gt;The old Reflector as you know and love it is still available free of charge, and as part of this project we've fixed a number of bugs in the de-compilation that have been around for a long time. The Pro version comes as an add-in for Visual Studio - this offers dynamic de-compilation and generation of pdb files which allow you to step into the de-compiled code. Alex has some good pictures of this functionality on his &lt;A href="/community/blogs/alex/archive/2010/01/20/87639.aspx"&gt;beta post&lt;/A&gt; from around a month ago.&lt;/P&gt;
&lt;P&gt;Thanks to the other guys who've worked on this for taking me along for the ride -&amp;nbsp;&lt;A href="/community/blogs/alex/"&gt;Alex&lt;/A&gt;, &lt;A href="/community/blogs/andrewh/"&gt;Andrew&lt;/A&gt;, &lt;A href="/community/blogs/bart/"&gt;Bart&lt;/A&gt; and &lt;A href="/community/blogs/jcrease/default.aspx"&gt;Jason&lt;/A&gt;. &lt;A href="/author/stephen-chambers/"&gt;Stephen&lt;/A&gt; did some great usability work, Chris Alford did some great technical authoring and &lt;A href="/community/blogs/laila/"&gt;Laila&lt;/A&gt;&amp;nbsp;handled the&amp;nbsp;launch publicity.&lt;/P&gt;
&lt;P&gt;Like all projects, there's always more I'd like to have done, but what we have looks like a pretty powerful addition to the developer's set of tools to me. Please try it and give us feedback on &lt;A href="http://www.red-gate.com/messageboard/viewforum.php?f=85&amp;amp;utm_source=simpletalk&amp;amp;utm_medium=blog&amp;amp;utm_content=reflector6release-cliveblog&amp;amp;utm_campaign=reflector"&gt;the forum&lt;/A&gt;.&lt;/P&gt;&lt;img src="http://www.simple-talk.com/community/aggbug.aspx?PostID=89883" width="1" height="1"&gt;</content><author><name>CliveT</name><uri>http://www.simple-talk.com/community/user/Profile.aspx?UserID=17260</uri></author></entry><entry><title>Real-World Functional Programming (book review)</title><link rel="alternate" type="text/html" href="http://www.simple-talk.com/community/blogs/clivet/archive/2010/01/03/85227.aspx" /><id>http://www.simple-talk.com/community/blogs/clivet/archive/2010/01/03/85227.aspx</id><published>2010-01-03T08:39:00Z</published><updated>2010-01-03T08:39:00Z</updated><content type="html">&lt;p&gt;I had been reading Tomas Petricek's blog for some time when he announced that he was going to be writing a book. I can’t remember when this announcement was made, and somehow the book seems to have taken a long time to come out, but Real-World Functional Programming (co-authored with Jon Skeet) was certainly worth the wait.

&lt;p&gt;This book isn’t just a simple introduction to programming in F#; it’s an introductory text on functional programming covering the many reasons why it is time for this programming paradigm to finally be accepted by mainstream programmers. And it also contains much more... 

&lt;p&gt;The authors promote the idea of functional programming as a new way of solving problems, where ideas such as immutability and functions as values can be used to solve parts of a problem in data- and behaviour-centric ways. Two benefits of these ideas are that the concurrency may well become easier to extract from the solution, and it may be easier to unit test the resulting code. These are two issues that are currently receiving much debate in the programming community.

&lt;p&gt;The functional examples are written in both F# and C#, and show how many of the features introduced in version 3 of C# allow a more declarative style of programming. The examples clearly demonstrate how many of the benefits of functional programming can be grasped by C# programmers. In particular, the book relates the use of LINQ in C# with the use of pipelining in F. Many of the examples show the brevity of the F# syntax which makes it the more attractive option for functional programming.  The authors also demonstrate how easy it is to mix code in C# and F#, showing that it is may now be possible to write algorithm-centric code in F#, and more object-oriented code such as GUI code in C#. Along the way, there are some tricks for getting generics to behave in C#, and an interesting section that compares the type checking algorithms of the two languages.

&lt;p&gt;The book doesn’t cover all of the syntax of F#, but describes the major parts. Indeed, the coverage of computational workflows, also known as monads, is probably the best that I have ever read.  Later chapters of the book look at functional reactive programming, an interesting if somewhat esoteric theory that neatly matches the abilities of functional programming languages to express domain specific ideas with a declarative means of specifying graphical models.

&lt;p&gt;There are also chapters on asynchronous and parallel programming, which emphasise the benefits of the functional style; the former contains a very interesting example of fetching data from a web service in order to visualise it via graphs. The chapter on reactive functional programming is preceded by one showing how easy it is to write compositional functional libraries. These chapters truly emphasise the benefits of the functional approach.

&lt;p&gt;The book is fairly long at close to 600 pages, and in my opinion, there isn’t much that is wrong with it. The authors introduce the concepts layer by layer, with several sections telling you that more detail will be available in later chapters. Having done a fair amount of functional programming in the past, I found this stratum by stratum approach a little annoying to start with, but with every section containing lots of interesting observations there was little opportunity for skipping parts of the book. In the end, the richness of the observations outweighed my annoyance at the repetition.  It would have been nice to have more discussion of functional data types, though there is a lot of material available elsewhere on this subject. The book also contained my pet bug-bear – the use of memorization to improve the performance of functional code without any discussion of how to avoid the memorization cache becoming a memory leak.

&lt;p&gt;Overall, this is an amazingly good book that puts into words the many benefits of the functional paradigm, and shows that the F# programming language can embrace both the functional and object-oriented styles of programming. C# programmers, too, can gain something from this book, using it to understand the ideas behind LINQ and lambda expressions and why these have been incorporated into the language. I’m going to be re-reading it and working through the examples in more depth in the near future.

&lt;img src="http://www.simple-talk.com/community/aggbug.aspx?PostID=85227" width="1" height="1"&gt;</content><author><name>CliveT</name><uri>http://www.simple-talk.com/community/user/Profile.aspx?UserID=17260</uri></author></entry><entry><title>What a Job Opportunity...</title><link rel="alternate" type="text/html" href="http://www.simple-talk.com/community/blogs/clivet/archive/2009/07/20/74071.aspx" /><id>http://www.simple-talk.com/community/blogs/clivet/archive/2009/07/20/74071.aspx</id><published>2009-07-20T06:05:00Z</published><updated>2009-07-20T06:05:00Z</updated><content type="html">&lt;p&gt;Life as a developer at Red Gate has many opportunities! One  moment you’re finishing off code in the first release of a &lt;a href="http://www.red-gate.com/products/Exchange/index.htm"&gt;new tool&lt;/a&gt; and  then you move into the .NET division to work with a &lt;a href="/dotnet/editors-corner/the-managed-heap/"&gt;group  of talented but scary-looking individuals&lt;/a&gt;. What do you get to work on? A  tool for allowing you to debug de-compiled source code in other assemblies than  your own, from within Visual Studio.&lt;/p&gt;

&lt;p&gt;As a developer there’s a lot to understand when you use the  .NET framework and third-party component libraries. You can read the  documentation and experiment with API functions, but there’s nothing better for  improving your understanding of them than seeing the code and following through  as it does its work. Using this tool, you will get to see the code for handling  particular corner cases and the many subtle points which are never covered in  documentation and are hard to deduce from a static view of the code. For  example, I’ve already looked at how ASP.NET queries are processed and how the  Windows Forms library interacts with the Windows message loop. With a better  understanding of the behaviour of the code, when you find problems in a  library, you have a better chance of any workaround not just working in a  couple of cases that you’ve had time to test, but all the time.&lt;/p&gt;

&lt;p&gt;I’m convinced that programming is an exploratory activity,  and that this is a great tool for helping with the exploration. &lt;/p&gt;

&lt;p&gt;This will work by adapting the Reflector de-compilation  technology and using it to produce a .pdb file that links this generated code  to the original assembly. A .PDB file is used by Visual Studio, and stores  debugging information about a .NET module such as a list of all symbols in a  module with their addresses, the name of the file and the line on which the  symbol was declared. &lt;a href="/community/blogs/bart/archive/2009/03/23/72559.aspx"&gt;Bart  talked a little about this project before&lt;/a&gt; and &lt;a href="/community/blogs/andrewh/default.aspx"&gt;Andrew&lt;/a&gt; Hunter, aka the master of the dark arts, has been working on it for a while as  a background project.&lt;/p&gt;

&lt;p&gt;There’s a long way to go, but we’ve started to push harder  at this project. As a developer, it’s an incredibly liberating experience to be  able to step through the .NET framework at long last. In the past, I too have  stared into the haystack of source code using Reflector. Reflector is great, but  to understand the behaviour of a system it would be even better if one could  see the code paths that are commonly taken and typical values for variables in  the various stack frames.&lt;/p&gt;

&lt;p&gt;So in this project, there are two sets of tasks to do. Obviously  the de-compilation to source and the production of the .pdb file is one of  them; the second is to integrate the production into Visual Studio to make it  easy for the developer to select the assemblies that they wish to step through.&lt;/p&gt;

&lt;p&gt;We can start with a simple project that calls into the  framework code.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;&lt;div align="center"&gt;&lt;img src="/blogbits/redatwork/Clive1.jpg"&gt;&lt;/div&gt;

&lt;p&gt;Hitting F5 runs some code in an add-in before the  application starts that scans the solution to find all of the assemblies that  are referenced. These are presented to the user, who may choose those that  should have a .pdb and source code generated for them. Here I have simply  selected the mscorlib.dll&lt;/p&gt;

&lt;p&gt;In case you haven’t guessed, this dialog is a placeholder  until the proper design is put into place.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;&lt;div align="center"&gt;&lt;img src="/blogbits/redatwork/clive2.jpg"&gt;&lt;/div&gt;

&lt;p&gt;Pressing “ok”, dismisses the dialog and Visual Studio walks  to the breakpoint that I cunningly preset earlier.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;&lt;div align="center"&gt;&lt;img src="/blogbits/redatwork/clive3.jpg"&gt;&lt;/div&gt;

&lt;p&gt;The add-in generated source code and produced a .pdb file.  Visual Studio’s search path was also modified to make the .pdb file available,  so when we press F11 (to step into), we find ourselves inside the de-compiled  code for the .NET framework with the locals window showing the value of the  local variable, which Reflector has chosen to name “value”.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;&lt;div align="center"&gt;&lt;img src="/blogbits/redatwork/clive4.jpg"&gt;&lt;/div&gt;

&lt;p&gt;Stepping into and onwards, we get further into the framework  where we can look at more interesting locals.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;&lt;div align="center"&gt;&lt;img src="/blogbits/redatwork/clive5.jpg"&gt;&lt;/div&gt;

&lt;p&gt;We can even set breakpoints.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;&lt;div align="center"&gt;&lt;img src="/blogbits/redatwork/clive6.jpg"&gt;&lt;/div&gt;

&lt;p&gt;Given that Reflector supports Visual Basic, after some  recent work &lt;a href="/community/blogs/alex/default.aspx"&gt;by Alex&lt;/a&gt;,  we can instead choose to see the de-compilation in Visual Basic.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;&lt;div align="center"&gt;&lt;img src="/blogbits/redatwork/Clive7.jpg"&gt;&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;&lt;div align="center"&gt;&lt;img src="/blogbits/redatwork/clive8.jpg"&gt;&lt;/div&gt;

&lt;p&gt;At present, there’s even some support for working at the IL  level.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;&lt;div align="center"&gt;&lt;img src="/blogbits/redatwork/Clive9.jpg"&gt;&lt;/div&gt;

&lt;p&gt;Of course the debugging experience isn’t seamless. We  probably won’t go to the trouble of scoping the locals, so that only live local  variables are displayed, and it isn’t always possible for Visual Studio to  calculate the variable values when you are stepping through optimised code. Also  Edit and continue will not be supported. However you can change the variable  values in the locals window and set the next statement so the user can change  the code path.&lt;/p&gt;

&lt;p&gt;How close is this to a real product? It is some way off yet,  and there are still some unanswered questions as to how well we can generate a  .pdb for optimised and obfuscated code. We’re making progress though, and the  results are encouraging. Builds are popping out of the build server and unit  tests are transitioning between red and green at quite a rate.&lt;/p&gt;
&lt;p&gt;And I’m having a great time working on it too!&lt;/p&gt;&lt;img src="http://www.simple-talk.com/community/aggbug.aspx?PostID=74071" width="1" height="1"&gt;</content><author><name>CliveT</name><uri>http://www.simple-talk.com/community/user/Profile.aspx?UserID=17260</uri></author></entry></feed>
