FxCop & .NET Best Practices

If you do .NET development, you owe it to yourself to useFxCop. This toolevaluates your assemblies (it analyzes the IL assembly, rather thanthe sourcecode itself), and flags possible deviations from bestpractices with varying severity levels. You can also easilycreate your own modules for FxCop, imposing team ororganizational specific guidelines. All in all it’s a very usefultool, and it’s free. Even if you don’t plan on following all of theframework guidelines, it’s still interesting in that it points outnuances that you probably didn’t know about.

This tool has been out for quite a long time, but I’m stillamazed how many .NET developers have never heard of it, let aloneused it, so I thought I’d drop some props for it.

Trackbacks and Comments

Several people have written to ask why I don’t allow comments ortrackbacks. The answer is not censorship, but rather it’s due tothe fact that I desired a totally static “blog”, so the use ofexternal comments and trackback servers is necessitated.Unfortunately the Radio Userland default ones areunbelievably slow, adding significantly to thetime taken to load and render the page. I’m considering options,and if anyone has any suggestions please email me (the link is onthe sidebar). Alternately, given that it’s an open API, I mightjust write my own. Sounds like fun.

 

.NET, Garbage Collection, and Reference Counting

Garbagecollection in .NET has always rubbed me the wrong way. As aquick recap, garbage collection in .NET (as in Java) works bybasically halting the application and scanning all references fromthe root reference on. It then looks on its heap to determine thatevery object has someone else pointing to it, and if not the objectis freed (through a long process). The heap memory is thencompacted and any references are rebased. The program then restartsuntil garbage collection happens again at some point in the future.This means, for instance, that if you create a System.IO.Fileobject in a short method that opens a file in exclusivemode, and you fail to use the Dispose pattern or explicitlycall Dispose (Dispose being a sort of “garbage collection has somegaps, so here’s something that you can do to expedite at least partof the process”), the file will be locked until some unpredictablepoint in the future that garbage collection runs. You can, ofcourse, force garbage collection, but that throws off the entirelifecycle management and can cause other resource managementissues.

Ultimately it seems like the sort of solution that worksfor relatively small or isolated applications (where itworks admirably – for web apps and web services, isolatedservices, and relatively small Windows Forms apps .NET is afantastic technology, primarily insofar as it reducesdevelopment time), but not as a technology that scales up tolarge scale, highly responsive systems (where you want the loading,resource usage and response times to be predictable andconsistent). This case seemed to be somewhat proven by many of thedelays and issues with Longhorn (Windows Vista), and the backtrackingand reduced reliance on .NET as a system pervasive technology (TheRegister isn’t the most credible source, but it’s just a referenceto the sort of thing I’ve heard throughout the industry). Entirelypredictable.

One change that I would like to see added to .NET – optionalreference counted references, with a second heap specifically forclassic, fragmeted allocation. Reference counting, the oft malignedtechnology behind COM (mostly because people didn’t know how to use it properly),is a completely reliable and extremely predictable and usefultechnology in a completely managed environment for most uses (thereare exceptions where reference counting breaks, but you don’t haveto throw out the baby to clean the bathwater). Python, forinstance, works largely based upon reference counting.

Some might note that Visual C++ 2005 has added“stack” reference types. This really is a bit of syntactical sugar- basically it’s just a variant of the Dispose pattern that, whencompiled, adds an automatic call to dispose when the object leavesthe scope. Not the same thing at all.

 

Success Through Communities

Over the past couple of days I’ve noticed hundreds upon hundredsof hits in my logs coming from www.skyscrapercity.com. Aftersome analysis I determined that a user there rather rudely embeddedan image on this site – a rather large picture of the Scotia Bankoffice tower in Toronto – in a discussion thread. Quite apart fromthe fact that the picture is being used unattributed (if it’s goodenough to use, then it’s good enough to attribute), it’s basicallysilently stealing my bandwidth quota. Very rude.

When people have done this in the past I’ve surprized them withdelightful and entertaining image alterations, but in this case I’mjust going to ignore it and let the thread die down. After lookingat the source of the traffic, however, I’ve been reminded of themost common, and most successful, pure-.com internet play – put upa site about some sort of fly-by information (for instanceskyscraper diagrams), and then add discussion links. Soon enoughyou’ll have a robust community ofusers who share that interest, spending hours a day debatingwhether Chicago isa better looking city than Dubai. It seems like a prettytenuous foundation for a community, but there it is.

Unintuitive number sequences

char *string_value = new char[32];

In computer science we’re quite accustomed to using powers of 2whenever a numerical limit is required. e.g. The string can be 32characters long, the filename can be 64 characters, while thenumber of entries in the listbox can number 1024.

These uses seldom require powers of 2 (e.g. while it makes sensefor an ASCII string to be multiples of 4 bytes if it’s long alignedand you care about that, it could just as efficiently be 28 or 36characters long), but nonetheless it’s ingrained into mostdevelopers’ minds.

I chuckled seeing the commercial for some overpricedtimed-interval airfreshener. It allows you to select 9, 18, or 36 minuteintervals between sprays. While not exactly compliant (I’ll betthat it was originally 8, 16 or 32 minutes, but they added some lagto the minute counter to avoid it seeming computeresque), and inthis case I can understand why the microcontroller developer chosepowers, the spirit of the power of 2 lives on.