Archive for June, 2010
NDepend: My new hammer for code reviews
Welcome a new member in my toolbox: NDepend. If you haven’t seen it yet: it is a tool for static code analysis for .NET code – static code analysis meaning that it looks at a set of assemblies and source code and tries to figure out how it all hangs together, and tells you something about its inner characteristics. It can for example tell you which are the most referenced or the most complex classes in your project, and if the way that your project is really structured is similar to the way you think it is. NDepend also sports the most busy, dense, chaotic and against-any-expectation user interface that I have ever seen, and you too.
I have been using NDepend once in a while for some while, but more on an on and off basis. I basically used it as a means of due diligence on code that I do not know, but have to get my feet into. NDepend can give you an overview of the structure of a large piece of software, and some of its problems, in 15 minutes. Now, I found a reason to use it on an at least weekly basis.
I’m currently involved in a project where multiple SCRUM teams are working on the same code base. When a Sprint (that is a project step in a time box of four weeks, but you should really know that) ends, the team runs a demo with the product owner (“PO”), who decides if the result are acceptable. Each Sprint is on its own code line. Now after the PO gives her OK, the code can go to the MAIN code line: the content of the product that is going out to the customer (it’s not that simple, but you get the picture).
Now the PO does not really care about code quality, unit test coverage and quality, or the completeness of test plans. So before the code that comes out of a Sprint is released to MAIN, it is reviewed by an independent group of developers, and this is where I’m involved. Not all code from the Sprint is reviewed, about 10% are normally sufficient to see if a team has put enough emphasis on clean and structured work. But how do you find out what has been changed, and how do you pick the 10% that are most relevant?
The approach until now is to get the list of changed files from the source control system, and pick the files to review by making a guess. The problem is that the list of changed files does not tell you how much has changed – often, there has been just an adjustment in the code formatting or the correction of a typo that caused the checkin.
And this is where NDepend is hugely helpful. It can not only run an analysis on a single project, it can get you the difference between two analysis results. In order to determine what has changed in the Sprint, you run an analysis on the state of the MAIN code line, and take that as the baseline for a differential analysis of the Sprint code line. What you get immediately is a ranking of types and methods that have been added or changed, in the order of the magnitude of the change, across multiple assemblies. That alone make a huge difference in the quality of the code review, and the time you need to get there.
And that is only the start. The most powerful feature of NDepend is that you can run queries in a SQL-style lingo against the code analysis. This for example creates a warning when you’ve got methods with too many (logical) lines of code:
WARN IF Count > 0 IN SELECT TOP 10 METHODS WHERE NbLinesOfCode > 30 ORDER BY NbLinesOfCode DESC
NDepend comes with a good set of these predefined queries, and you can write your own. Let’s say we want to get the classes with little comments, but only in code that has been added or modified within the Sprint:
SELECT TOP 50 METHODS WHERE
(WasAdded OR CommentsWereChanged ) // Only modified and new
AND (
NbLinesOfCode > 20 // A lot of code
OR CyclomaticComplexity > 15 // A lot of paths
OR NbVariables > 8 // Many variables
)
AND !IsGeneratedByCompiler // Exclude anonymous types
ORDER BY NbLinesOfCode DESC
In addition, you can actively search for things that you do not wish to see, like
- References to MVVM ViewModels
- References on concrete classes rather than on interfaces
- FxCop suppression attributes
- Usage of NotImplementedException
- Low unit test coverage
The only real limitation I’ve ssen is that a query can either run against types, methods, namespaces, or fields, but not against a mixture. You can for example not search for property setters that do not use the PropertyChanged event in types that implement INotifyPropertyChanged.
NDepend is free for limited usage for evaluation, academic, and open source projects; for commercial projects or the full feature set you’ll have to buy reasonably priced licenses.
Oh yes, and: NDepend can be targeted at Silverlight 3 and Silverlight 4 as well as normal .NET code.
Turning little robots into iPods (Part 1)
Posted by Christian in Uncategorized on June 22, 2010
I got myself an nice new Android phone (an HTC Desire, precisely). And what should I say? I love the thing. Great display, great apps; alright, there’s no good games, and the iPhone may look better (i.e. not be brown) – it’s an unbelievable device, I can install a great and free SDK and run my own software on it, and I can run it on a 10€ per month plan. Enough said? – well, I’m trying to replace my iPod with it, and that is kind of harder than it looks.
I’ve got about 2609 tracks in my library: a medium-sized one, I’d say. I have been using an iPod nano 1st gen, then an iPod Touch 1st gen, and I’ve bought about a third of the music on iTunes. A lot was already DRM-free, some was still old DRM-infested AAC files. So the first investment in using the Android as the primary music player was to get everything in non-DRMed format – it took me first about three evenings of burning music to disc and then import it again, then 30€ for Apple to upgrade some of the rest because I got tired of it, and then I just decided I wouldn’t be hearing most of the rest anyway and just let it go.
The Library
I did not want to use iTunes any more. It has become so slow. So unbelievably, nerve-twisting, nail-chewing slow. The machine I use is a Intel CULV on 1.3 GHz – not a race horse, but I can everything on it from Visual Studio to Eclipse to games like Portal, and a web server, and a database server, and some cheese on top. ITunes however just crawls. The shop is totally unusable. And yes, I run it on Windows, but I know from trustworthy sources that it’s just not any better on MacOS.
However: iTunes needed to be replaced by something. Here’s what I tried:
Songbird
Songbird is a free OS media library and player that builds on top of the Mozilla UI framework.
I loved Songbird from the first second on. Or better: I tried to. Because the version (1.5, I think) that did not support Windows 7 (in May 2010!). And that weren’t empty threats, it was really crashing left, right, and center. There was a beta version that is now final, it had some problems as well, but was generally OK. That version was just lovely: solid functionality, and a variety of plug-ins that integrate smoothly into all kinds of cloud services. For example, a sidebar that docks on the right gets the lyrics of each song that plays. A bar on the bottom gets information and pictures for the band. And of course, each played song is submitted (“scrobbled”) to Last.FM so it adds to your profile there. And: plug-ins to generate playlist based on similarity to the song you’re just listening to (aka “Genius”). So sweet.
Until you plug in a phone that is not explicitly on the list of supported devices. Like the Motorola Droid, or the Nexus One. But not the HTC Desire. Although that is of course a media player with exactly the same supported formats. But as long as Songbird does not recognize the exact model, it assumes the device can only do MP3. In order to be still able to get the music onto the phone, Songbird will encode each AAC or OGG encoded song into MP3. Remember I’m not running on a fast computer? That started to take a long, long time. And my relationship with Songbird severely suffered. Why did it not just copy the files across? To me, that looks like a stupid piece of over-engineering. Instead of assuming that the device can play most anything, and then dealing with the exceptions, Songbird assumes that the device can’t do anything until it is presented an exactly matching and badly documented file that needs to be present on the SD card. That was probably a good strategy five years ago, but right now, players can seriously deal with formats.
DoubleTwist
DoubleTwist is a free beta of a commercial product.
DoubleTwist has received some very good press as being polished and compatible to a lot of devices, so I thankfully installed it after some days of figuring out how to get Songbird to sync the phone. DoubleTwist is in fact polished, easy to use, and it works with the HTC Desire. What it lacks is: any feature. No rules-based (“smart”) playlists. No Genius. No Last.FM. If you’ve got a collection of up to 300 songs, that may all be OK. For everyone else, you can’t help but smile at such an feeble little application.
Amarok
Amarok is an OS media player/library for Linux.
Everything about Amarok looks so great – except the walk-through to installing it on Windows. And that due to length and windedness of that description, no-one else would have tested it. Amarok had me wishing to install Linux for the first time in years (but I resisted).
MediaMonkey
MediaMonkey a media player/library for Windows. There is a freeware version, and a approx.20€ paid “Gold” version.
I don’t really like MediaMonkey. It is one of these skinned applications where you wish it would just show a niced standard Windows UI. The buttons are small. There are about 5000 options and features of which you’ll use 10. There are no good community-supported plug-ins. It does scrobble to Last.FM, but that’s as good as it gets. But what should I say? MediaMonkey _works_. It’s _fast_. It has great support for tagging files. Knows all formats. Sorts files into the right folders. Is stable. Does not mind when you put more than a few songs into it.
And: MediaMonkey does a great job in supporting devices. Per device, you can say what exactly should go to the device in which format. Configuring that is not really a job for my mother – she’d be better off with DoubleTwist – but you don’t have to any obscure XML files either (looking at you, Songbird).
The free version of MediaMonkey is quite OK for trying it out, but you won’t stay with it. Mostly every advanced organization feature does not work, and there’s an ugly nag banner on the UI. But hey, I’ve just spent 450€ on a phone.
So, MediaMonkey it is. In the second part, I’m going to deal with the player app on the phone itself.
NUnit for Silverlight, now with data driven tests

Here’s the next drop of my inofficial port of the NUnit unit test framework for Silverlight. It supports NUnit 2.5.5 on Silverlight 3 and 4, and I’ve added support for Jeff Wilcox’ port of the April version of the Silverlight testing framework on Silverlight 3.
And man, does that April version make a difference! Compare to the November 2009 release of the Silverlight unit testing framework, it’s not only nicer to look at – it runs a whole lot faster. If you’re already on Silverlight 4, you’re already using it anyway; but if you’ve still got a SIlverlight 3 project, I’d highly recommend you try the upgraded verion. Rewriting any of your test is not necessary, of course.
About this release
This release of the NUnit port consists of three Visual Studio projects for the different targeted platforms:
- Silverlight 3, with the unit testing framework from the November 2009 release of the Silverlight toolkit
- Silverlight 3, with Jeff Wilcox’s port of the unit test framework from the April 2010 of the Silverlight toolkit (which targets Silverlight 4 only)
- Silverlight 4, with the unit testing framework from the April 2010 of the Silverlight toolkit.
The actual NUnit code is shared between the projects, only the unit test framework specific code is different between the branches. In order to compile your own version, download the source code and find the version of your choice. If you have a version of the Silverlight toolkit installed that does not match the one you wish to compile, you might need to set the reference path inside the Visual Studio project for the MetaData and NUnit.Framework.Tests projects to the “lib” directory that contains the dependency assemblies.
The license of the project is a little bit of a mess: Yes, it’s all open source. But. NUnit has its own license, which also applies to the ported version, of course. The NUnit provider by Jeff Wilcox is however published under Ms-PL. I’ve got a strong feelign that these licenses are compatible and it could be possible to publish this port under just one license, but I’m no expert with this kind of problem, and need to get some advice first. In fact, this is the reason why I’m not putting the project up to Google Code or Codeplex – these platforms demand that you decide the license, and for the moment, I’d say there are two.
The version numbers have been changed to Year.Month.Counter; the current version is of all assemblies is “2010.6.1″. This version is neither strong-named nor signed; I’ll be changing that soonish, promised.
Data driven tests
One thing I wanted to include into the ported version of the NUnit framework were the tests included with the NUnit source code. In the source code for this version, you find one Silverlight application that implements these tests. Porting them, I found out about quite an important oversight: with NUnit 2.5, a powerful way of defining data driven tests was added; the ported version however did only support the [Test] attribute for standard tests.
The simplest way to define a data driven test for NUnit is to decorate a test with the [TestCase] attribute instead of (or, optionally, in addition to) the [Test] attribute. This attribute lets you define parameters and an expected result for a test. By decorating the same test with multiple [TestCase] attributes, you can cover a lot of test cases without having to write and maintain repetetive test code – you find the full documentation here. The SL3 and SL4 ports for the April 2010 version of the Silverlight toolkit now supports the [TestCase] attributes; the version for the November 2009 Silverlight toolkit does not.
NUnit also offers some attributes for more advanced data driven tests, these are not supported.
NUnit unit tests
I’ve ported the unit tests for the NUnit.Framework assembly to SIlverlight; however, not all tests did make it. The main reasons for a test being excluded are:
- The test refers to something in NUnit that could not be ported – like the assertions on serialization and directories.
- The syntax tests rely on compiling code as part of the test – Silverlight won’t let you do that; it clashes with the security restrictions (you don’t want an app runnign inside a browser to modify itself).
- The test is data driven and uses an attribute that is not supported in this version of the port – in fact, the April 2010 version included more tests because the [TestCase] attribute is supported there.
Download
So: happy NUnit testing!
Please download from nunit-silverlight on Google Code.
My tester sense is tingling
A long time ago Voltaire said
Common sense is not so common.
and nothing much has changed. However, one guy’s common sense in the next guy’s idiocy. Common sense, for everyone, is very subjective really unless you put it into perspective by calculating the average of everybody’s idea of common sense, which can be tricky. Common sense is not about democracy.
C.E. Stowe said
Common sense is the knack of seeing things as they are, and doing things as they ought to be done.
Which adds a bit of precision to the definition: common sense is about facts. Facts will allow you to make the right decision – given of course, that your facts are straight and you know what the best decision is. But that’s a different story.
Testing is a lot about common sense; all the techniques are tools of the mind, unlike the practical tools used for programming. It does not matter how a system works, or if you test an application, a document, a process or a sandwich. The approach is the same: get all the facts, define the frame of reference, inspect and compare the expected or desired result with the actual result. Sounds easy. It only works if you get it all right, though. You need all the facts, you need to know the frame of reference exactly. You use logic to get there.
It gets harder if the edges of your frame are blurred. If your facts have multiple variables, possibly all interdependent. You might need to look into the future. This is where common sense comes in.
Don’t mix up common sense with experience. Common sense can be gained and improved by experience, but hardly taught. And remember, common sense is subjective; it means something else for you than it means to Bob. Boundary value analysis may come natural to you. You might believe as the highly experienced programmer Bob is, he would know everything about it as well. Hey, you’re assuming here! Isn’t the tester’s mantra Never To Assume?
As much as you trust your “tester instincts”, as precise as your estimates might be, as trustworthy your risk analysis turns out – common sense will not save you, not more than pure logic will. Because common sense is not so common, because logic and people do not always mix. Measure, step back, use your common sense and measure again. It’s about both, facts and sense. If you neglect any, you will either end up deadlocked or eviscerated.
—
I realize my posts have been somewhat esoteric and elusive lately. I will try to focus on more practical things after the summer break, promised. I will also explain what I meant with my other post about sacrificing quality which came across wrong, apparently.
Is the patient alive?
For over ten years now (and think about what a time span that is in Internet time!) Flash has been the primary platform for developing highly interactive web content and applications. Flash delivers things that were plainly impossible otherwise: animation, perfect support for mouse input, vector graphics, video, streaming; and all of that could be developed in a single, intuitive, and widely adopted development environment that enables a huge community of designers and developers to shine. Without much of a doubt, the Web would have been a duller, less inspiring, and less useful place. When Silverlight was announced, its main proposition was to be able to do the same kind of things that Flash delivers with full leverage of your skills as a .NET developer. In whole, Silverlight was a reverence to the success of Flash.
It’s a commonplace that Flash is in trouble. It’s not only that Apple is aggressively banning it from their mobile platforms – a lot of its feature set that has been unique to Flash is now being covered by HTML5. With the recent improvements in the rendering capabilities – vectors, rotations, animations – and the upcoming codecs for streaming video playback, there is hardly and more reason to use Flash. Embedding a Flash part into a web site is now considered outdated; it limits the reach of the site, adds a second technology to your skill matrix, adds friction to the process of production. A lot of the use cases for the technology just evaporate.
Of course, the same holds true for Silverlight. It is time to unite it with its anchestors on the graveyard of deas MS web technologies – ActiveX, ASP, the DNA architecture? I’d say it depends.
Let’s take a look at where the differences lie between an HTML5-based application, an RIA application that runs in Flash or Silverlight. In theory, there aren’t that many.
The application is not installed on the client prior to use. The runtime environment is on the client. It is an environemt with limited, and not always known, abilities. Access to local resources is not possible, or heavily restricted and managed.
In both models, you have a markup language that defines the content. There are visual tools to define that content. Content and style are largely separated into different compartments so that domain experts can modify them independently. Code is used to add behavior. The content can heavily be modified by code. In order to retrieve resources that are not available on the client, the application can reach out to servers in the cloud, and process that data in its own runtime.
Of course there are differences. HTML applications do not have a defined runtime environment – they need to cope with more uncertainty about the browser they run in. Script may be disabled. There is no access to external resources. The technologies used by the app are separate; cohesion is not a framework feature, it is something that developers need to provide.
RIA applications on the other hand have a more defined runtime environment, a coherent framework, and some more access to local resources like disk space, the microphone, or the printer. On the down side, there is a problem with reach – the correct version of the runtime plug-in needs to be installed, and it may altogether be unavailable for an OS or form factor (especially if vendors start acting as a format police corps).
For a wide range of use cases, both an HTML5 and a RIA application are a viable solution. But the specific challenges of the platforms influence the price that you have to pay to get your solution to work. When the framework has more to offer, there’s less for you to write. On the Silverlight platform, we can argue if we prefer MVP or MVVM, a pure DI framework like Unity, or MEF, and if we like to talk to servers using SOAP, POX, or raw TCP. This is a level of design opportunities for larger applications that HTML5 won’t offer for quite a time.
I’m trying to predict the future now. And that’s only a few days after finishing The Black Swan. At least I may add that I’m probably over-confident because I’ve been right before, and biased because my own stakes lie within the RIA field.
The areas where HTML or RIA applications are going to be used are defined by complexity. Right now (so pre-HTML5), one may draw a graph that weights complexity against the usage of a type of application (HTML, RIA, native):

HTML applicatioms will be able to take a significant bite out of what has been the domain of RIAs. But on the other hand, we’re going to see that RIAs have quite a significant piece of the cake to take: where you needed to write a native desktop application, an RIA will often be the weapon of choice. If you do not need to have really unlimited access to the client machine, why would you need to perform a heavy-weight installation? I’d guess you won’t run an installer that often any more.

The second use case for Flash and Silverlight style applications are mobile devices. For the upcoming Windows 7 Mobile platform, Silverlight is going to be the one and only application format. It’s likewise on Android: as a Silverlight developer, you’ll find yourself extremely familiar in the structure of applications and the style of programming (except that Java always feels to me like C# with the handbrake on). And it makes sense: a phone is a runtime enviroment much like a browser plug-in. It’s a specific, defined platform, and you have access to local resources in a controlled way; the reach of HTML does not play any role here.
And in the long run, I’d think that the differences between HTML and RIA style of applications disappear further – and the direction will be that the browser becomes more of a platform that the Flash or Silverlight runtime environment is right now. With a more coherent programming model for content, style, and code; with the ability to discover and use local resources, great network access, and support for multiple programming languages.

If you’re an open-minded Silverlight developer today, you won’t have much of a problem in that environment.