Refactoring, tools, Web

Removing unused UserControls with NDepend

Untitled2

NDepend is an amazing analysis tool for .NET code. It gives you deep insight into your code and let you manage its quality easier. It lets the user execute LINQ queries on code, generates dependency graphs, metric view, check rules and many many more. In this post I’ll show a very simple trick to detect unused user controls that can be removed from our project.

NDepend was recommended by Scott Hanselman, Greg Young and others. I tried it some time ago in my small project but I couldn’t benefit from the tool. After some time I had to do some cleaning in our legacy system. Then I came back to the the tool and I have to admit that I fell in love with it.

Some time ago I wrote about Finding unused private and public methods with Resharper. It is of course very nice way of doing code clean-up but in rather smaller systems. In my case Resharper found around 2.5 thousand issues and it’s not an amount that I could face in one or two iterations.

Some issues are specific to particular parts of the solution. In WebForms project the highest level of abstraction is Page and Control. If the user removes/refactors page and decide to not use some user control it becomes dead source of other dependencies.

In the first iteration I decided to get rid of unused pages with help of google analytics report from the last few month. The next one was about dead user controls. Resharper doesn’t allow us to find unused types deriving from specific base type. Here is the place for NDepend.

In “Queries and Rules” section I run the following query:

Untitled3

As a result I got the list of around 30 user controls. Unfortunately this is just an input for further filtering because it lists controls that are not used in code, but not saying anything about markup. All in all checking this is not that hard, one solution is to go through all of them and check what compiler says or find all occurences of “<%@ Register Src="~/Path/MyUserControl.ascx" TagPrefix="uc" TagName="MyControl" %>” for each *.ascx file.

Web

Chrome tips part 1 –selectors

 

Howdy! After some time I’d like to start with some JavaScript stuff.

Today’s browsers give us excellent opportunity to debug our pages with the one and only Web language which is JavaScript. Best example of that tool is Chrome console which gives us more than could be described in any post. As probably each of us knows, the console opens after pressing F12.

image

$$ alias

The most common usage for the console is jQuery programming – we can easily traverse the page searching appropriate element or just testing selectors before scripting. The scenario complicates when there is no jQuery included.

Google Chrome console treats $ as an alias for document.getElementById and the result of such queries are far from the desired outcome. Fortunately there is a nice alternative for it which is “$$” alias.

$$ is alias to document.querySelectorAll function that does pretty similar work as jQuery.find method. So everytime I feel the lack of jQuery I use this thing with success. Remember that it doesn’t implement all the selectors that jQuery has (like :first, :even) for argument/classes queries it is enough.

image

As jQuery.find method replacement we can also use “querySelectorAll” method.

$x alias

Another great tool traversing the document to find some element is $x method. It uses XPath istead of css selectors. It’s good new especially for people who can do more with XPath than with jQuery.

image

How about ability to deal with iframe’s?

$$ and $x does not search for the content of “iframe” element. To do it you have to switch the search context. To do it with $$ once more you have to use the full name of querySelectorAll method but in context of specific “iframe” document:

image

In case of $x it’s is slightly different – you have to pass context as a parameter:

image

You have to remember, that this kind of scripting have to adhere crossframe-scripting policy – domain of the frame should be the same as parent context’s domain.