Acknowledging progress

I quite like this idea:
When projects seem like they are going on forever… something like this could be quite a good motivator for the team. I probably wouldn’t put in the direct eye of clients as it is pretty messy (but in my experience most coding environments are anyway…)

Open Source list

Jeff Atwood is pushing to help reward some of the .Net OSS community by donating $10k to the most deserving application (or the group that runs it). That’s pretty nice but I am much happier that there is a pretty comprehensive list of GOOD .Net OSS application/frameworks/tools here:

http://spreadsheets.google.com/pub?key=pKxDW35algYebfs8nssTjIQ

I don’t know if this link will work forever… perhaps just for the length of the “competition”.

Shot Jeff!

SubSonic

Like a lot of people, writing Data Access Layers is not something I really want to spend a lot of time doing. Thankfully there are a vast number of ways that you can improve the process, ORM’s and code generators are the two basic methods of assisting in this fashion. Many will be familiar with Hibernate (and its .Net equivalent), possibly the most popular ORM on the market. I quiet like it, but it’s fiddly and config heavy in terms of mapping files. You can Generate these thanks to product like MyGeneration but it often requires tweaking. once its up and running it really is a nice way to deal with persistence for very OO based applications. The other option I have used is Codesmith with it NetTiers templates. Well, up until finding Subsonic. It (so far) has been great.
What I like:

  • Can have the benefits of SP’s and ORM
  • Can set it to generate the code at build time, so you know every time you run it it is synched with the data base
  • If you don’t like the generate on build option you can easily generate the class files for the DAL as well as the create scripts for DB including data, all from with in VS (great for Source control!)
  • Author(s) seem to be very proactive in their support and maintenance
  • Can be used with in VS as an external tool, meaning one click generation from within VS.
  • Minimal additional config settings
  • Video showing you how to do it.
  • Its pretty fast

I last ran into Subsonic when it was ActionPack. Being happy with Codesmith I didn’t really follow it up. However, as I no longer have a license for CodeSmith, SubSonic being OSS is looking a lot nicer 🙂
No doubt as I continue to play with it I will find features and bugs, but so far so good.

"Just one test!"

I have read in a couple of places now (amongst other generalities) that a unit test per method is the standard “rule of thumb”. I have been surprised at this. I understand that at least one test per real method (not one line getter/setters) is a minimum but to generalise it to only one is a bit odd.
This was kinda sparked off by this post http://blog.sidu.in/2007/06/pre-run-tasks-unit-testing-anti-pattern.html. I am not coming down on the poster at all and I may have over reacted to an aspect of the post that was certainly not the main point of the thread.
But just one test per method? It got me thinking.
I generally have a test to cover each of the paths in the method (to an extent).
I never really thought about it but I guess complete coverage* would come from tests that match each of the paths and therefore the cyclomatic complexity of that method. If you have an increasing number of tests for a method you may want to reconsider the functionality of the method. Can it be refactored?
This I believe is another benefit of TDD. It forces you to think about function design first not just start the torrent of code and hope that what comes out works. This is also a reason to write the tests first. I find it amusing that some still write the test after they write the code. I mean really, why not define what you want then build it.

* no im not a coverage nazi :p

ICloneable = mess

I have never really taken it consideration of the reasons of why not to use ICloneable. I know not to do it, so have never had any issues. However the current project I am working has object inheriting from a certain base object that implements ICloneable (why? I don’t know).
Now the fact that none (or very few) of the ICloneable methods returned anything other than null, could have been the first sign that it might not be the best interface to implement. Then there are the deep versus shallow clone issues… who know’s what is doing what.. and then the returning of type object. Excellent. 😦
Unfortunately it was a Microsoft consultant that apparently suggested using it.

Now we have a non standard clone implementation that some time does deep and sometime does a kind of shallow clone. Yuk. At least some of it is now documented.
As the classes are shared across other projects a cant just go an remove the interface… not that it looks like any one is actually using the method; as it is returning null everywhere… weird.

Back to asp.net – PageMethods

A little bolt-in that I found a while back and have never used is finally getting a test drive.
PageMethods basically helps you “strong type” parameters for aspx pages, meaning you get such benefits as intellisense & compile type errors.
Again, I’m a sucker for little helpers like this, because errors like mispelt paramtars tend to really peeve me off. 😉

NHibernate gotcha for Generated files

To save yourself repeated banging of you head make sure you HBM.XML file are embedded resources in the nhibernate project, otherwise how is nhibernate going to find these mappers….d’oh!
This highlights a point: Its nice to generate what you can. Hell, if you have already written the database tables, why not Gen the majority of your data access? You still however, have to know what is going on under the hood, whether its nhibernate, other ORM or straight ADO.Net. Otherwise how fix you fix lower level problems when you run in to them…

TDD still not with the masses?

Recently I have been a contractor on a project that I have been given I little bit of grief (perhaps light heart, perhaps not) regarding the number of unit tests I have written in comparison to the production code I have submitted. Upon talking to some external mates in the development field there still seems to be some confusion as to why to test, how to test and when to test. I will happily admit that I am no TDD guru and am relatively new to the having only really being diligent about unit tests over the last year or so.

I did want to share some of my reasons, that may not be so text book, for using TDD. I will start with aspects of TDD, that on my most recent project, I have directly benefited from:

Define the Functionality of the Module
In the last project requirements were somewhat lose, so writing the unit test help the guy with the business know how and myself knuckle out what exactly we wanted these blocks of code to do. The people that will have to maintain my code as I move to the next project will probably find this module easier to maintain because of the reasonably well named and documented tests that are in place, meaning reduced ongoing cost of the code base.

Ability to Refactor with Confidence
As I was receiving information about the requirements in a piecemeal fashion, I was forced to write code for the functionality as I got it. This quickly became quite a large amount of code which later we found had aspects replicated across multiple classes. Refactoring these classes to strategies that inherited from a base strategy which inturn implemented an interface with the real business logic held in a handful of delegates meant we reduced the lines of code by 73%. As we refactored we made sure the entire block of unit tests still passed. The code is now a fraction of the size, very easy to read and maintain, fast and still fulfils the requirement set forth by the business. Refactoring all that code that took weeks to write was done in a day, in confidence because the unit tests were written up front.

Reduction of “Gold Plating”
In some case I get carried away and start writing things that I haven’t got a test for, because they may be needed in the future. This is by and large a bad habit of mine, I don’t get paid to gold plate, I get paid to provide a quality solution to a defined problem, even if that problem is not very well defined!
How has TDD helped? If it was not being covered by a unit test, it either needs to be or it gets removed. 9/10 it get removed as the tests are written first and they define the functionality.

Code Confidence
Although test don’t prove you code is good (see TDD Anti Patterns) it odes show that I have thought about what I am doing and give other developers or interested parties a direct view at what my code does. Most of my test methods are named so it is pretty obvious what they do, sure some get pretty long but I can find a unit for a module that I wrote months ago pretty bloody quick and figure out with in seconds the exact functionality. This is especially helpful if you have no BA support or business requirement documentation (yes it happens). Not only am I more confident in my code but I am more confident in other code if it is unit tested.

Encourages Correct Separation of Concerns
Especially when using mocks it becomes apparent when projects are starting to take on more functionality than they should.

Up-to-date Code
As I am writing tests for my code, as I go, I find that I am more likely to be within minutes of a previous compiling working solution. Formerly I used to go on tangents and write pages of code which may have been in a state far removed from a submittable state. If for some reason I was asked to shift on to other functionality I would have to shelve changes roll back and then work on something else. If I ever cmame back to that code I often had no idea where I was up to. Now I know exactly where I am up to… the method that breaks the unit tests! It is also more likely that I wouldn’t have to shelve as it is probably only a minute away from being in a compilable stae that I could check in to source control.

Easier to Make Changes
There have been some instances where the business requirements, or my understanding of them have been (a little bit) off. Having the test there means I can modify the test to assert correct assumption run the tests, see it fails and then make an isolated change in the code. As the test marry up nicely to the code the place where the change is required is very easily found and fix. ..Run test…. Pass test… Check in.

So how do i go about it? Well my”why” is covered above. For more reason check out http://www.testdriven.com or any other of the plethora of sites and books dedicated to TDD… as for the when and how…

When to test?
Before you have even written the functionality! Define what you want the method to achieve. You will probably find your self writing multiple tests per method and test fixtures are generally a lot larger than the class they test, bu the time you save in reduced debugging time and easier maintenance is well worth it.

How to test?
There are heaps of tools to use and can be a personal thing. Currently I am using TestDriven.Net with NCover and NUnit as well as ReSharper & Rhino mocks. As ReSharper is the only tool here I have paid for there is no reason not to get these widely used tools (I probably should throw some money at Testdriven.net).
Try to get a test project for each business project and a test fixtures should closely marry up with the projects classes. I don’t test minor thing like properties… there should be reason for the test!

Most frameworks now are very open to TDD, so there is very little reason not to use it. Right up to UI patterns, all but the view are very easy to test and mock. And as the view is completely stupid it should not have a lot scope for error.
So start testing; the initial growing pains are well worth it.

RhysC

Win form UI dev

I suck at Win forms dev, the only time I use it is for test rigs and for uber basic (in terms of UI) CE apps

I have been trying to set 3 total values to right align at the decimal point in a panel on a WinFom app. It proves to be not as trivial as I expected.

I have to place each of them in a panel and then dock these labels to the right side of the each holder panel. What over kill! Why can’t the AutoSize and Anchor functionality do this for me? Oh well…