Friday, February 24, 2012

ILSpy (free .net decompiler): possible replacement for .net Reflector.

Today I am going to share about and very simple yet helpful tool named ILSpy. It could be a possible replacement for the “.net Reflector” which is now a commercial version by Red Get Software.

ILSpy is a useful and yet not that much feature rich tool, may be because of its early stage, however; the rood map shows that it could turn out to be a handy tool in near future.

Users can download this from the http://wiki.sharpdevelop.net/ilspy.ashx. Did I mention that its open source and absolutely free!

image

Some interesting features are
  • Assembly browsing
  • IL Disassembly
  • Decompilation to C#
    • Supports lambdas and 'yield return'
    • Shows XML documentation
  • Saving of resources
  • Save decompiled assembly as .csproj
  • Search for types/methods/properties (substring)
  • Hyperlink-based type/method/property navigation
  • Base/Derived types navigation
  • Navigation history
  • BAML to XAML decompiler
  • Save Assembly as C# Project
  • Find usage of field/method
  • Extensible via plugins (MEF)
  • Please visit the home site of Ilspy for more information on this tool.

    Until next time, happy coding.

    Friday, February 17, 2012

    “Dead code”, a very common code smell.

    Remove dead code from you code files

    Today we are going to take a look at a simple code smell named “Dead code”. You would be surprised to know how many dead code exists in your project when you take a close look at the project file. of course you wouldn’t know just by looking at it. some of them are pretty hard to find.

    What is dead code?

    The code that is no longer needed and no longer used by any function or procedure is a dead code.

    The Tool

    The easiest way to identify the dead code is to use a tool that will save thousands of milliseconds by providing some indication that the code is no longer used. And I am talking about none other than Resharper, the awesome tool that solve lot of our problem and yet so cheap!. Of course I am talking about only Microsoft Platform, eclipse has its owe set of cool feature that have all the refactor and code notation. But if you are a “IntelliJ IDEA” user, then probably you have all the necessary help you need.

    Simple dead code

    The bellow screen shot shows some portion of the code file and the red rectangles are dead code. that’s the simple version I would say. If you have Resharper plugin with your VS2010 IDE this tool would grayed out the dead to let user know what are the dead codes.

    image

    Here is possible things that can be dead code.

    • private variables
    • private methods
    • private properties
    • redundant quantifier
    • redundant directives
    • public methods
    • public properties
    • Even a class
    • Unused method parameter
    • Unused local variable

    Remove dead code Techniques

    #1 :  Use the full cleanup .

    Just right click on your code file and select full cleanup from context menu. This would remove lots of problem from your code.

    image

    image

    Yet you have some dead code after full clean up. you can follow the bar of VS code window and click imageon each item to remove the problems. to remove the dead field or method just put the cursor on the gray text and then hit key ctrl+Enter this will pop up a tool window with some option menu with it. Select Remove unused method to get rid of the method.

    image

     

    #2 : Find the uses of the method

    This is a hard part and can lead to a chain where you would get rid of a couple of methods Tree. How do you quickly identify that the procedure is unused. Its pretty easy to identify the private methods and fields as its been grayed out by the reharper. but what about public method and public properties or fields.

    Generally when you get rid of a dead code for instance a private method, any method that is been used in that private method also become dead if the method is not been used some where else, luckily reharper automatically detects it and gray out the methods in turn.

    Now lets talk about public methods, first check if the method has a reference or not. To do that simply right click on a method and select “find usages” if it shows that it does not have any uses, you can probably get rid of the public method,field and property.

    image

    One easy way to use the modifier, change the modifier from public to private. if you don’t see any compile error it means this method would be grayed out. so no uses. But this fails in case of WCF Service interfaces. Since the service is been uses some where else and all methods that we want to expose needs to be public. But that is the whole idea of a WCF service, so we would expose only necessary methods That would be used.

    After find Usages if any usages found it would be listed on a tool window. Just click on the item. It would take the user to the method or line of code where its been used. Check again if that method has any Usage. And so on , I think you got the idea what needs to be done here to identify where is the dead code.

    image

    A common problem of our code is that is the class is been used anywhere? One simple technique is simply exclude the class from the project and then build the project see if it creates any compile error or not. but we can also use find usage technique with the class file also, to find if its been used some where or not.

    Always use “Safe Delete”

    While removing class or methods from project always use safe delete operation of reharper, this would help us not to create unnecessary problem for our codes. To use safe delete simple right click on the object you want to delete, i.e. parameter, variable , method and class. Then select Safe Delete from context menu, or you can also use Ctrl+R, D to lunch the safe delete window.

    image

    When the window pop’s up you can also check enable undo if you don’t feel safe. Then click next to get rid of the code. If the code that we ware trying to delete has some reference some where it would not perform the operation rather it would show a error window showing where the usage is.

    image

    There are many more thing to discuss about dead code. And couldn’t be completed in a single post. I would try to put together different platform in future.

    Thursday, February 9, 2012

    Common Refactoring Techniques

    Here are some examples of code refactoring; some of these may only apply to certain languages or language types. A longer list can be found in Fowler's Refactoring book and on Fowler's Refactoring Website.

    •     Techniques that allow for more abstraction
      • Encapsulate Field – force code to access the field with getter and setter methods
      • Generalize Type – create more general types to allow for more code sharing
      • Replace type-checking code with State/Strategy
      • Replace conditional with polymorphism
    • Techniques for breaking code apart into more logical pieces
      • Extract Method, to turn part of a larger method into a new method. By breaking down code in smaller pieces, it is more easily understandable. This is also applicable to functions.
      • Extract Class moves part of the code from an existing class into a new class.
    • Techniques for improving names and location of code
      • Move Method or Move Field – move to a more appropriate Class or source file
      • Rename Method or Rename Field – changing the name into a new one that better reveals its purpose
      • Pull Up – in OOP, move to a superclass
      • Push Down – in OOP, move to a subclass

    Tools that can be used to enhance refactoring technique with visual studio 2005~20xx

    Among the above tools Resharper is most popular.

    References:

    1. http://en.wikipedia.org/wiki/Code_refactoring
    2. http://msdn.microsoft.com/en-us/library/ms379618%28VS.80%29.aspx

    Friday, February 3, 2012

    Snoop for WPF, a WPF Spy.

    Life would be much easier, if we had a tool like firebug for WPF, where we can see the anatomy of the application which is running. Luckily some of the WPF geeks are not as much lazy as we are and created a awesome tool for us named Snoop for WPF.

    Here is what you can do with it,

    • Can see the visual tree
    • Change property at runtime
    • Visual layers as 3d View.

    image

    Download and experience this tool from http://snoopwpf.codeplex.com/.

    May the force be with you!.