Sunday, February 05, 2006

Me, .NET and FxCOP

Well its been long since i posted my first blog, probably about how to approach BeanShell and PMD. Well to add another fact , i received a complimentary copy of PMD Applied and guys this book is really cool and one cannot ask for a better and simple book. PMD is an open source tool to test Java code for consistency and coding standards based on some pre defined rules. We have a similar tool for .NET Platform called the FXCop which i have just downloaded. I have to try out yet and i shall try it out on my final year project. Its high time i start my project and i hope to finish it at the earliest.

Coming to FXCop, it has a cool but outdated GUI look(i checked out 1.32) it appears like some old good tools - anyway the functionality is what matters more. I wish it has some project import option and can check the entire project of VS.NET 2003 Studio. For those who doesnt know how to go about with FxCop, you just need to create a nw project in FxCop and add your assemblies to the to the project. Assemblies is a cool term and it really makes me feel wow when i last heard about its definition. Well i dont want to recall that and type that over here but simply put - an assembly is a building block on .NET applications.(well its not the definition, atleast for you all).
Once you add the assemblies, you can use the tree explorer to your left to check out the rules.

Just for your information, based on the documentation of FxCop - it uses MSIL Decompilation, and other such ways to test if the rules are applied. PMD works a bit different over here. It is a static code testing tool, it doesnt use any memory profiler, just raw interpretation of Java Code in its original form. I havent tested it out yet of bulkier code, will test it out soon.

I am now going to test the rules against my assemblies and i am using only the Remote Machine part of my project(there is another administrator part) If everything goes well i shall add this information in my project report on how my application has passed the FxCop Test. When we are ready to test, we just need to click the "Analyze" button on the top, with a play icon. It really performs quickly - i expected something around 5 mins for some code :) Hey wait , i have deleted one assembly from the Project and it raised an exception. :-? Not well debugged. Well this is usually the case with excellent community products. I hope it has been rectified in the .NET 2.0 Version. I shall check that out very soon. Well let me start the tool again and check this time for rule violations in my code only. This time i shall let you know even the time it takes to verify the assemblies. Well it takes only matter of 5 to 6 seconds, but what is irritating is that it is giving some information about the violations in the code which you havent written. So what i did is that i have unchecked all the rules in the rules tab and then i have checked only the "Performance Rules" and you can really expand the performance rules and check only that are applicable and which makes sense. I would like to test if my application performs well, i am least bothered about auto generated code- i cannot change it can i? There is a rule "Mark Method as static if "this" or "ME" is not used inside the method" well, what if it accesses the members of the same class to which its currently member of? So the rules doesnt appeal as much to me. So i uncheck that rule for a while, because when designing my application i have made sure that the methods are made static wherever necessary, but my app deals with Remoting and Static Methods in Remoting are big pain, instead i already use Singleton objects. Am i correct here?

The cool aspect of using such features is that we get to know the performance issues and we should really read the ruleset properly and it improves our coding techniques. Let me explain. When i tested my code with FxCop for only Performance and i have unchecked "mark methods as static", there is one rule violation that has caught my eye. It says "Test for empty strings using string length" Seeing that i clicked on the rule violation info displayed and to the bottom i can see the description - the bad part is that the description is in IL :)) Well i normally test for empty string using Equals() method this way:
If string1.Equals("")
End if

There is clearly performance problem which i havent noticed till now - We create an empty string object, call a function, so there is lots of process being performed at the back. Instead I could have done it with simple string1.Length property as
If string1.Length=0
End if

There is no creation of new object, no stack operations are performed and this definitely improves the code performance. Good! I learnt a new thing which i shall keep in my in the near future. This is why i said such tools and their rulesets are always interesting.

One thing with FxCop is that it tests for autogenerated code, i guess. And it annoys me. I wish there is a way where it tests only for my code. I shall have to check out the options.
At this point i would also like to tell about "Channels" in Remoting. Having played with .NET Remoting for my application, which involves sending image across the channel. I have discovered two things:
1. Images cannot be sent directly like other objects. Instead, create a Stream object and save your image in the stream and send the stream instead. Later you can use Image.FromHStream method to extract the Image.
2. There is helluva difference between the performance of HttpChannel and TcpChannel. When i used a HttpChannel to send the image capsuled in a stream object, it took reasonable amount of time. Later i replaced HttpChannel with a TcpChannel and it has improved the performance significantly.

My project requirement also includes "communication over encrypted and compressed channel". I have no clue as of now, how to implement such a channel of communication in my application. But just for a feature i have mentioned it and i can remove it anytime, once i see a performance degradation cos of those features. Atleast i have a reasonable answer if question is raised.

There is a lot of scope for one to research in .NET Remoting and i am worried before i learn something in Remoting, it would be replaced soon by WCF(I think its for distributed computing) I have had my friend download the latest Windows Vista SDK. Got to check that out very soon. .NET is fun and Microsoft software development tools are better than any other tools in the market. That is for sure. ( i am not a java antagonist, just that i love .net more, cos it makes me feel wanted :)) )

RAC Life is not as smooth as it was earlier. I just took up a C# to Vb.NET Converssion project and it removed the hell out of me. Well it was really fun and i really learnt some cool things which i wasnt aware earlier. Well that is all the work i had at RAC, though it appears pending projects are "4", 3 of them i have reported work complete. There are some good buyers out here at RAC and i really enjoy working for them. Ezra, Levent(my java tips boss) and now Uwe are now the most cool guys i met at RAC. Special thansk to Uwe who gave me an extended deadline for C# to VB Conversion project. Anyway always thankful to one person on this earth without whom my life would have been differently painful.

Just for those who wish to try out software development on Linux, you can get hold of different Linux LiveCD which doesnt need any installation to be done. You have good set of cds and heres the list.
Recently i downloaded Monoppix to try out Mono.NET on Linux platform and later i shall download Pollix for Java Development. Mono-Live i heard is cool, but it was 700 mb odd n i wanted to download as early as possible so decided to go for Monioppix which is 470mb odd.

Well guys, see you soon again with .NET fundas.


No comments: