Friday, February 25, 2011

Testable C# or C# TUFs and TUCs

Re-reading Michael Feathers nice article on "Testable Java" I decided to see how the same thing looks in C#. Now, Java and C# aren't too far a part, so the difference isn't huge.

The "Testable Java" article gives a simple rule for how we can write code that is friendly to unit tests. -As the article points out the rule is superflous if we follow TDD stricly. But - being human - we don't always do that. Sometimes we write only integration tests or even acceptance tests here and now, and postposne unit tests. Sometimes we skip the automated tests altogether. In these cases it's an advantage to have code that is at least friendly to unit tests. If so, it's afordable to add the unit tests later.

Feathers' rule is:
"Never hide a TUF within a TUC"
and Feathers goes on to define TUF as a "Test Unfriendly Feature" and TUC as a "Test Unfriendly Construct". Now TUFs are things that we want to mock out in many unit tests, but TUCs are language constructs that are hard to substitute for testing.


Examles, from the article,  of TUFs:

  •   Database access
  •   File system access
  •   Network access
  •   Access to side effecting APIs
  •   Long running computations
  •   Inscrutable computations
  •   Static variable usage

The first three are basically I/O, and in general I/O is test unfriendly because it is slow, and usually assumes things about the outside world - like "the webservice is reachable" or "the robot arm is connected through the serial port".
The next - side effects - is about avoiding tests that changes something and annoys somebody.
Long running computations are a nuisance, and will make you skip test runs.
Inscrutable computations, are things that are just plain hard to understand - maybe due the "clever" code-smell or to essential complexity.
These TUFs are all TUFs in .NET as well.


The article lists a number of Java TUCs. This is my ported list of C# TUCs:

  • Sealed classes
  • Static methods
  • Private methods
  • Static initializers
  • Constructors
  • Static constructors 
  • New expressions

 and these are few more C# TUCs IMHO:

  • Non-virtual methods
  • Internal methods
  • Internal classes  

The point is to remember - even when you skip the TDD strictness - that testability is an important part of maintainability. So if maintainability is quality attribute you want, then keep an eye on the testability. If not, those TUFs inside of TUCs will come back and bite you.

Oh, and the other point: Go read Michael Feathers' article. It's well worth it.

Thursday, February 3, 2011

Random Thoughts on Tiobe Index January 2011

The Tiobe index is an index of the popularity of programming languages (formally defined here).
According to the Tiobe web site "the index can be used to check whether your programming skills are still up to date or to make a strategic decision about what programming language should be adopted when starting to build a new software system."

Some Data to Notice From the Index

As someone who has worked mainly in .NET through the last several years, my interest in the index focuses on .NET languages (and on my favorite language for recreational coding for some time now: Scala). The rankings of .NET languages in the January 2011 index are:
  • F# ranked between 50 and 100 (the index put these 50 in one big pile because differences the in scores are minuscule)
  • VB.NET at no. 49
  • PowerShell at no. 28
  • C# at no. 6

For perspective here are some selected other languages:

  • BETA ranked between 50 and 100
  • AWK at no. 31
  • T-SQL at no. 17
  • Ruby at no. 10
  • VB at no. 7
  • Python at no. 5

And also worth noticing is that Java, C and C++ take the top 3 spots, and have for a decade.

What I Get From That Data

Now what does this mean for a .NET developer?

  1. C# is hugely popular, but still much, much less popular than Java. Looking at the long term trend C# is steadlily gaining ground. So having and maintaining C# skills is a really good idea in terms of making a living.
  2. VB.NET is so unpopular it's almost off the a bad way. But (!) considering how the Tiobe index is done I don't think it distinguishes VB and VB.NET properly. So IMHO it's more fair to add the VB and VB.NET scores which would place VB.NET at 5th place - where VB is now. If this is true having VB.NET skills is a really good idea too, but the long term VB.NET trend is downwards, especially over the last couple of years. So maybe it's time to translate those VB.NET chops to C# chops?
  3. F# doesn't seem to be going anywhere. So don't learn F# for job security. Learn it to open your mind. But then again, if you want to open up your mind, and you're a .NET dev, why not look outside .NET and do Scala or Clojure or Erlang or something completly left field?
  4. PowerShell seems to be becoming important. Maybe it's time to really learn it.
 What does it mean for the .NET community?

  1. Assuming the VB and VB.NET points needs to be added C# and VB.NET combined would weigh in at a 3rd place, meaning that .NET is doing very, very well in terms of popularity. 
  2. Both Ruby and Python are doing very well. So if the Iron* languages are dropped an opportunity for engaging two very large communities is lost. That would be really sad!

Those were my 2 cents. Go check the index out, and make up your own mind. And let me know what you think :-)

Oh, and Scala is at place 50, just within the list proper. Still rooting for it :-)