Wednesday, March 7, 2012

Technical Debt: Avoid it at any cost, other wise it would take all your properties.

Here is a fact that I learned from a real life problem, on of my relative took a huge loan from a bank, the risk was totally calculated but its like he is been paying the interest and principle forever, and its a never ending story, Same thing happens to software if you do technical dept. Here is what wikipedia has to say about technical dept.

“Technical debt (also known as design debt or code debt) is a neologistic metaphor referring to the eventual consequences of poor software architecture and software development within a codebase. The debt can be thought of as work that needs to be done before a particular job can be considered complete. As a change is started on a codebase, there is often the need to make other coordinated changes at the same time in other parts of the codebase or documentation. The other required, but uncompleted changes, are considered debt that must be paid at some point in the future.”

In short, while doing coding we faced a problem and we do a quick and dirty way to solved it and give a on time delivery. Later you are coming back to the same place again and again to write more dirty code to over come the scenarios its failing.

What a stupid thing to do, if only your higher stakeholders known, that, the two hour job eventually will take huge amount of development time, they would have hired people who would be monitoring so that no technical debt is been introduced in the code base.

if you want to know more about this topic please go to http://en.wikipedia.org/wiki/Technical_debt. There is a significant amount of information is there to help you out.

Suggestions

What to do?

Never ever introduce technical debt.

If a problem takes good amount to time and a significant amount of architectural change talk to your higher authorities, make them understand that this would take time and a quick and dirty solution will lead them more development effort in future.

Take calculated risk

Always follow 80/20 principle if the feature is been used by only 20 % of the users and its unlikely that this would cause any immediate breakdown.  But don’t forget to get back to it quickly and then refactor it, other wise you would soon forget about it.

General Suggestion

  • Refactor as you go deep in to the code.
  • Try to use design pattern
  • Be communicative with the team
  • Always follow good process of development (For Example : Agile: Scrum, Kanban)
  • Most important try to have some test coverage.

In an ideal development world there is no technical debt, but still there is in real world. so we got handle it correctly, until next time happy coding.

Friday, March 2, 2012

JustDecompile, how to get .net decompiled in a flash.

Alright, today we have a new point of focus, the target topic is JustDecompile, it’s a free decompile tool provided by none other then Telerik, thanks to them we have yet another awesome decompile tool in the community, most importantly, its completely free.

Anyone can download it from Telerik site http://www.telerik.com/products/decompiler.aspx. When you download the installer, it only downloads a web installer, kind of web platform installer thing.

image

After download, when you lunch the application, you would be given a choices to install wide range of tools, all that have telerik in there product line. Our focus is only “JustDecompile”. So choosing the right checkbox. You can also try some other productivity tool if you want to give it a try.

image

This is the nagging part, it ask for registration or username and password if you already have one. Since the product is free, it Should be totally free of any kind of registration. Unlike us telerik has different idea about the information collection policy.

image]

After login you the real installation will begin. Its going to download about 25 Megabyte of data. And going to take about a 5 minute or less depending on your internet bandwidth.

image

After installation lunch the application, the User interface is really good. And the tool is super simple. Just open any dll it will be added to the left side tree view, you can then open any class of your choice, one very interesting feature that catch my eye is that you can change the language, i.e. you can choose in which language you want to view the code.

image

This can be a good conversion product also.

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!.