September 2008 Entries
What do you listen to while developing?

Question has been asked on some mailing lists recently, but I'm curious as to what music/thing (if at all) do you listen to while developing? As I'm asking the question, I'll give the first reply: nothing.

Years ago, I used to listen to a variety of things, including Phantom of the Opera, Guns'n'Roses, Pearl Jam, Pink Floyd, Pink Floyd, Pink Floyd, Roger Waters and some early Pink Floyd. For the past couple of years however, unless I'm doing mechanical coding, I don't listen to music, or anything for that matter. I find, especially when I'm thinking about design, that it distracts me. I've even tried music with no lyrics (except Kenny G.)

Maybe I'm just getting old...

2 Comments Filed Under [ Productivity ]
Visual Studio will ship with jQuery

This is awesome news

2 Comments Filed Under [ Various Visual Studio ]
Don't always take what you're offered

20 minutes left in my session on Continuous Integration. Thinking great! More than enough time to go thru a demo with my CI server. Switch over to my VM machine that I booted up in the background (unfortunately the room was not "freed up" by the previous speaker until 4 minutes before mine started), and get faced with this:

Crash

WTF!?#@#@~!

I did what any good computer user would do, reboot and hope for the best. And it worked! Imagine if I had pressed "R".

Add Comment Filed Under [ Various Doh! ]
Error creating network connections under XP...unable to create connection. Insufficient Memory or disk space

This one is more of a personal reminder. I've just tried to connect to the office VPN and noticed my username/password is blank. I fill it in and try to connect. It won't even dial. Gives some weird error about not being able to save or delete an existing password. I decide after several attempts to delete the connection. WRONG! I can't create ANY type of new connection under Windows XP Network Connections.

It seems my RAS is corrupt and my phonebook is missing. Solution is to re-install it. I'm traveling right now and don't have the CD of XP with me (I know, my error). Luckily I've found another solution. IF you run RASPHONE, you can create a new VPN connection and dial it from there. And it works. It creates the icon for you in the Network connections also. Problem is, it's unusable. It won't disconnect or connect from there. You still get errors. But if you run RASPHONE again, you can pick the connection from the combo and hang up.

I'll figure out what messed up later, but for now it works.

 

image

Add Comment Filed Under [ Various ]
Preventing Outlook marking days with appointments as Bold: A Doh! Moment

I was setting up a recurring appointment today in Outlook that has "no end". I hate recurring appointments that are more than once a month because when I look at the thumbnail of the month calendar it shows many days in bold. Sometimes the appointment doesn't justify that. I just realized that if I mark the event Show As "Free", I don't get the "boldiness"

 

image

 

I've been using Outlook for many years now. Boy do I feel stupid! I think this post deserves a new category/tag

2 Comments Filed Under [ Doh! ]
Thought for the day...be a productive keyboard user

Become a more productive [computer user] (insert Developer, Excel Programmer, Word User, Internet Surfer....).

Every time you want to do something that involves taking your hands off of the keyboard and going for that damn mouse, see if what you're about to do has a keyboard shortcut. Use the mouse if you have to to find the shortcut (sometimes they are displayed next to the item in the menu entry), but don't use the mouse to perform the action, use the shortcut instead. Next time you want to do the same thing, you'll remember the shortcut instead of the reaching for the mouse.

Result: you'll be much more productive without the mouse.

It's not a question of getting 3 minutes more out of the working day. It's about not breaking your flow of concentration and at the same time improving your memory, and none of us are getting younger...

Add Comment Filed Under [ Various Productivity ]
The Bus number isn't agile, it's common sense

I was having a conversation with a developer the other day about working on different areas of a project. The subject came up after I made a comment about how the person that breaks a build should be the one to fix it. His immediate reaction was that he shouldn't be responsible for fixing something that wasn't his "area". And that it would be much more productive if the developer working on that specific area were to fix the problems. This isn't the first time I've had this conversation. In fact I've had it many times with many developers.

There is a general tendency in believing that distributing up a team of developers and have each one focus on one area and only one area during the lifetime of the project is a good thing, that it is more productive. If Jack learns everything there is to know about WPF and handles all the user interface during the whole project, whereas Joe works on all the back-end, that is good. Each know their part inside out and can fix a bug before someone else from a different area can even reproduce it.

I mean it makes sense right? It's pretty reasonable thinking right? Well not everything is black and white.

Productivity can vary based on surrounding circumstances. Let's say a project is delivered to a customer and after three months, a bug is reported. Jack can fix it fast. Productivity is high since Jack works fast. However, Jack is on holiday. Customer is waiting for a fix. It's hurting his business. Jack will be away for another two weeks. There is only Joe left, and now needs to get up to speed on what Jack's been working on and try and fix a bug. Productivity has dropped to an all-time low.

In principle, that doesn't seem that bad. The productivity ratio can still be better than having everyone know all the system from day one. If a bug comes in and Jack is away, Joe will learn. And once he's learnt, he'll know it. If it ever happens again, he's already learnt it. Sure, it takes maybe 2 weeks instead of 2 hours to deliver a fix to the customer, but it's still ok. Problem is that it's not that simple. We're looking at a two-man product with two areas. As the system grows, the numbers grow.

The problem gets worse when we're not talking about a bug fix, but delivering a project. That's where the bus number commonly comes in: how many members of your team would have to be run over by a bus, for your project to fail? The higher the number, the safer your project is [This is a metaphor, not some cynical sick project manager that cares more about the project than his team members being run over].

I've been on both sides of the fence. I've been the stakeholder behind a project that has a very low bus number, and I've been a developer on a project with a low bus number. And I can tell you, neither of them is a good thing.

From a stakeholder's perspective, it's risky. If you have a project with three guys, and each has his/her area of speciality, you can suffer and your business can suffer if one of those guys decides to leave.

From a developer's perspective, it's bad also, and the longer the project, the worse it is. Working on one area and/or with one technology can cause stagnation, which in turn can lead to lack of motivation.

 

Coming back to the initial paragraph, as developers we should not be afraid to work on any part of a project. If you are working on a user interface screen and you need a certain piece of functionality that does not work or is not implemented yet, you should be able to work in it. If you don't have enough knowledge of the project/l.o.b. to be able to work on it, that is another problem waiting to surface. Solve it. The later you leave things, the worse it will be, both for you, the team and the project.

I can understand that people that are not "software orientated" find it hard to understand. They assume it's like anything else. In most industries, if one of the members of the team leave, and they are specialized in a certain area, it's not hard to find someone else to replace them. Soldering steel, fitting steering wheels, etc. have pretty much non-existent or very low learning curves (you can pick up how to fit a steering wheel in a Ford in a very short time if you've done it for a VW or a Toyota). With software it's different. You not only need to know the technology, you need to get learn the domain of the project and get up to speed with the architecture. It's not as simple as pulling one guy out and putting another one in his/her place.

Knowing this, the best thing we can do on a project is have the ability to temporarily replace one person with another. It isn't so much about being agile, but about having common sense.

One Comment Filed Under [ Various Productivity ]
Naming tests, once again

I'm coming across a lot of tests with names like this:

AddEmployee_Should_Pass_When_Not_Duplicate() {.....}

AddEmployee_Should_Fail_When_Duplicate() {....}

For the purpose of this post, I don't care about anything other than what's in bold and red.

The word Pass in there is completely irrelevant and useless. The word Fail is just mind-blowing. What should fail? The test? What if the test passes? Does that mean it has really failed? Or when it fails it means it passes?

Now we all know what Fail means here. It means the test will pass but the call will fail. But it doesn't indicate to us what failure consists of and therefore it's not adding information. Someone looking at the test cannot validate it.

When naming, indicate what the test is trying to validate, not what the outcome is; the outcome is already known: pass or failure.

Something along the lines of Add_Employee_Should_Throw_Exception_When_Employee_Already_Exists would add substantial information for the person reading your test to validate the functionality. But more importantly, when the test fails, they would know what has failed and have valuable information for debugging. 

Build Notification Tool, Visual Studio 2008 Fails

The Build Notification Tool for Visual Studio 2008 stops working if you install Visual Studio SP1. It sort of explains why it was working for everyone else except me, until they decided to update to SP1 also.

Integration Tests

Two teams work on two different components of a project. Team A works on component A, and to keep things simple, Team B works on component B. The system needs both components to function.

There's an additional catch. Team A needs component B to test A. Team B needs component A to test B.

So what to do? Well they would do what any good team would. Team A goes on holiday while Team B finishes and then Team B relaxes. Unfortunately that isn't always possible. 

Instead, they would use mocks, whether they create their own stubs/fakes or use an existing framework. To verify that component A works, Team A uses mocks to replace component B's functionality. Team B uses mocks to replace component A's functionality. All tests pass.

The next step would be to integrate the two subsystem and put it in production. I mean surely if all subsystem tests pass, it should work......the reality is it probably won't.

 

The moral of the story: Two halves don't necessarily make a whole. Don't skip your integration tests. It's not a waste of time. And integration tests aren't just about running all your unit tests on a continuous integration server. They are integration tests, not isolated unit tests.

Google Chrome views: Pragmatic, not Dogmatic

I've been using Google Chrome for over a week now. What I like about it...

- The tab completion for search engines. Awesome

- The preview feature with the most recent history

- The history search

- The "auto showing" status bar. This is very cool. It's a really simple idea, but very cool. Think about it,  why take up space for a status bar that only displays info when loading a page or displaying link information?

- It's FAST! I mean REALLY fast. It's instant to start up and surfing seems faster

- Its compatibility with the sites I visit most.

- Its support for saving HTTP authentication correctly. Something that Firefox nor Safari could handle properly. It can connect to Sharepoint and save credentials, which comes in very handy. With Safari I kept having to type them in despite indicating to save them.

 

What I don't like

- Doesn't work with some of the sites I visit most

- It's lack of proper support for certain technologies like Silverlight. Sorry, if you are releasing a browser this late in the game, make sure it supports something as important as Silverlight (I'm not getting into political motivations here...I'm evaluating the browser as a TOOL in my toolbox)

- Zooming by pressing CTRL and the Mouse wheel works opposite to all other browsers. Forwards makes the text larger. Is this me only? A bug? Or a feature?

- It's lack of support for importing bookmarks from Safari

- Not being able to sync my bookmarks with my iPhone. Sure, this isn't Google's fault, but I'm not playing the blame game.

- I miss Firebug. I missed it in Safari and miss it here

 

It's the first beta. If it improves in the next release, I'll use it. If it doesn't, I'll use something else.

2 Comments Filed Under [ Various ]
Test your containers

IoC containers are great. They take away the burden of having to create dependencies before using things, and when your dependencies start to become complex and you have a whole graph of them, it can get ugly if you're not using one. So to the rescue comes Mr. Unity, Mr. StructureMap, Don AutoFac or the Hero Ninject. Take your pick.

But all this greatness can come back and bite you on the tip of your pinky finger (the one on the right hand). Why is that?

Let's assume you have the following class:

   1: public class CustomerServices    
   2: {   
   3:    private readonly ICustomerRepository _customerRepository;    
   4:  
   5:    public CustomerServices(ICustomerRepository repository) 
   6:    {
   7:  
   8:      _customerRepository = repository;   
   9:  
  10:    }  
  11:  
  12: ...

 

and you decide to introduce a new dependency

   1: public class CustomerServices    
   2: {   
   3:    private readonly ICustomerRepository _customerRepository;    
   4:    private readonly IAuditingServices _auditingServices;
   5:  
   6:    public CustomerServices(ICustomerRepository repository, IAuditingServices auditingServices) 
   7:    {
   8:  
   9:      _customerRepository = repository;   
  10:      _auditingServices = auditingServices;
  11:  
  12:    }  
  13:  
  14: ...

 

That code will compile, as will any other code throughout the project that is using CustomerServices. It's only then when the container tries to resolve CustomerServices, will it see that you have not registered any type for IAuditingServices. And this happens at runtime.

So it's important to create configuration tests for your containers.

Naming again

I can't stress how important naming is, in all aspects, not only tests. The first thing someone looks at when viewing code is the name, be it a class, variable or method. Naming even plays a more important role when isolating functionality. If you are following the MVP pattern to separate the view from the presenter, don't go using method names that reference any sort of visual control. Don't make a presenter method called SetActiveTab.  

It's not just a name. It's important.

User Interfaces and TDD

I was having a conversation with a colleague of mine the other day. We were talking about user interfaces and how a sketch of what the interface should look like would help him have a clearer picture of the design. This is not that odd. Many use the same technique to show customers what a possible interface of the end application could look like. The customer can then evaluate whether he/she likes it or prefers to change some detail (and it's never just a color). 

For this purpose, some use designer tools to sketch out a user interface, while others work with development tools hoping that they don't have to throw out the prototype they printed on paper to show the customer.

image

A few decide to make hand-made sketches because they believe showing the customer a WinForm/WPF/PNG can mislead them into believing that the work is nearly finished. If that is the case, educate the customer, don't blame the "messenger" (be it whatever electronic format).

image

 

Irrelevant of the technique used, many see this as a natural process; it's not peculiar or odd to show a customer what the user interface might look like, despite the fact that behind that user interface there is no functionality.

 

 

Take the previous search screen. It was displayed to Joe (a fictitious customer) and he decided he didn't  like it . He said he preferred to have to click on a Search Records button which then would pop up another screen where he would fill in the data and then hit Search (Joe was having a bad day).

image

 

The developer took the feedback into account and designed this particular functionality (not what Joe wanted but what the developer thought Joe really needed). Is there anything wrong with that approach? No. It is perfectly valid. Customer feedback is a good thing, especially when it's really taken into account.

 

Now let's change roles. Let's make the customer be the developer and the user interface be a specific class. What is the developers's user interface to that class? It is the API of the class. It's the properties and method calls. The requirement of class, much in line with that of the form, is to search for particular customers based on a series of fields and return a list of them.

The developer starts out by designing the API like so:

   1: public class CustomerSearch
   2:     {
   3:         public ICollection<Customer> Search(string name, string surname) {
   4:             
   5:             // Call database
   6:             // Add search conditions
   7:             // Filter records
   8:             // return records
   9:  
  10:             return new Collection<Customer>();
  11:         }
  12:         
  13:  
  14:     }

[At this point, you must be wondering WTF is this all about? To be honest, I've lost track myself, but let's hold the thought for a moment...]

The developer, Jack, then starts to use the interface and as he's a good developer, his first real usage of the interface is a test. As he's writing the test, he realizes that he's made a mistake. He's limited the Search method to two fields, and the customer record has five fields. He then goes back to the class and changes the method to use specifications.

   1: public class CustomerSearch
   2: {
   3:         
   4:     public ICollection<Customer> Search(Criteria criteria) {
   5:     
   6:         // Call database
   7:         // Add search conditions
   8:         // Filter records
   9:         // return records
  10:  
  11:         return new Collection<Customer>();
  12:     }
  13:     
  14:  
  15: }

He then changes the whole code to implement the new functionality. Once he's happy with the result, he then returns to the test and starts to write the test, happy that he's accomplished his objective. Problem is, he threw away 2 hours of work by not thoroughly thinking through the requirements.

But you must be thinking why he hadn't thought of this beforehand. I mean it is pretty obvious, specially if you've done this before. The reason is that you don't know it all up front. It's only when you start using something that it "hits" you. Just like customers don't know everything up front, and they ask for changes as they start to use the program, as developers we don't know it all up front either. As much design as we try to do on paper, it's not until we actually start to use our code that we find we need to change our design.

 

 

Jack could have skipped the first implementation altogether. He could have started using the interface before implementing it completely. Wait a second. How could he use the class before it existed? Well let's think back. What was Jack's usage? He started writing his test like so...

   1: [Test]
   2: public void CustomerSearch_When_Name_Is_Joe_Should_Return_All_Customers_Named_Joe()
   3: {
   4:     CustomerSearch customerSearch = new CustomerSearch();
   5:     
   6:     customerSearch.Search(....    
   7: }

It's when he started writing Search when he realized that he only had two fields and that a customer could have up to five fields. This is where he realized he needed more fields, and the initial design of passing in field names was not very scalable.

 

Welcome to Test Driven Development

If Jack had started to use his own code before implementing it, he would have realized his shortcomings. Here is where TDD comes into play. TDD is not about writing your unit test first, it's about designing your code. It's about you, as the user, defining your user interface. Much in the same way we sketch out a user interface for the customer to give his feedback before we implement the functionality, writing tests is defining the calls you need for your classes before implementing them. As an added value, you end up with a test so that as you re-factor your design to suit your needs, you can validate the functionality.

 

I know this is old news to many, but still to date, it amazes me at the amount of developers that still think that Test Driven Development is about writing tests first. It is, but not to have your test, but to define your usage.

Making mock tests less volatile

In a follow-up to my previous post on mocking, here is the same code re-factored so that it does not depend on the internal behavior of GetAllEmployeesByCompanyId

   1: [TestMethod]
   2: public void GetAllEmployeesByCompanyId_When_Employees_Are_Marked_Returns_Non_Marked_Employees()
   3: {
   4:     IEmployeeRepository employeeRepository = MockRepository.GenerateMock<IEmployeeRepository>();
   5:  
   6:     User user = new User { Id = Guid.NewGuid()};
   7:  
   8:     Employee employee = new Employee {Id = Guid.NewGuid()};
   9:  
  10:     Employee deletedEmployee = new Employee { Id = Guid.NewGuid(), DeletedBy = user};
  11:  
  12:     employeeRepository.Stub(x => x.GetEmployeesByCompanyId<Employee>(Guid.Empty)).Return(new List<Employee>
  13:                                                                                              {
  14:                                                                                                  employee,
  15:                                                                                                  deletedEmployee
  16:                                                                                              });
  17:  
  18:     var EmployeeServices = new EmployeeServices(employeeRepository);
  19:  
  20:     var employees = EmployeeServices.GetAllEmployeesByCompanyId(Guid.Empty);
  21:  
  22:     Assert.AreEqual(1, employees.Count);
  23: }

I've done quite a bit of re-factoring, taking advantage of some of the C# 3.0 language features. I'm also using RhinoMocks 3.5 which also makes use of extension methods. In general the test is much cleaner, less noise. In fact, apart from two "strange" calls (lines 4 and 12), you wouldn't even notice it's using mocks.

The important change is that its no longer brittle. If you recall correctly, from my previous post, the purpose of the test was to validate that when GetAllEmployeesByCompanyId, only records not marked for deletion were returned. It was not about interaction or the internal behavior of the call. As such, we've removed this tight coupling to the actual implementation by

a) Replacing the strict mock with a dynamic mock [Line 4].
In version 3.5 of RhinoMocks, you no longer need to create a mock repository if you don't want to. As such, you have static methods on the MockRepository class that generate mocks for you. By default, the mock generated is Dynamic. Therefore, the call MockRepository.GenerateMock is a dynamic mock.

b) Removing the call to VerifyAll.
Since we do not set any expectations, we do not really need or want to verify that any specific calls were made. Again, that's not the purpose of this particular test.

In line 12, we've stubbed the call to GetEmployeesByCompanyId of EmployeeRepository so that it returns a list with two employees. If the repository call were to not return a value, we wouldn't even need to explicitly stub it.

As you can see, we are still using mocks, yet we are not making our test volatile to change. We are using mocks to mock out non existent functionality. This doesn't mean to say that we shouldn't use mocks for verifying behavior. But we should only do that if the purpose of the test is that. In this case it isn't.

RhinoMocks 3.5 is pretty cool. It allows a lot of the noise surrounding mocking to be eliminated in tests. Hopefully I'll blog more about it as I use this version more, but be sure to check it out (Don't be lazy...Just Google it!)

Mocks can be your friend, or your worst nightmare

Take a look at the following test:

   1: public void EmployeeServicesTest_GetAllEmployeesByCompanyId_When_Employees_Are_Marked_Returns_Non_Marked_Employees()
   2:        {
   3:            var guid = Guid.NewGuid();
   4:            var guid2 = Guid.NewGuid();
   5:            var guidComp= Guid.NewGuid();
   6:  
   7:            MockRepository mocks = new MockRepository();
   8:  
   9:            IEmployeeRepository mockRepository = (IEmployeeRepository)mocks.StrictMock(typeof(IEmployeeRepository));
  10:  
  11:  
  12:            User user = new User();
  13:            user.Id = Guid.NewGuid();
  14:  
  15:            Employee deletedEmployee = new Employee();
  16:            deletedEmployee.Id = guid;
  17:            deletedEmployee.DeletedBy = user;
  18:  
  19:            Employee validEmployee = new Employee();
  20:            validEmployee.Id = guid;
  21:  
  22:            Expect.Call(mockRepository.GetEmployeesByCompanyId<Employee>(guidComp)).Return(new List<Employee>() { deletedEmployee, validEmployee });
  23:  
  24:            mocks.ReplayAll();
  25:  
  26:            var EmployeeServices = new EmployeeServices(mockRepository);
  27:  
  28:            var testList = EmployeeServices.GetAllEmployeesByCompanyId(guidComp);
  29:  
  30:            Assert.AreEqual(1, testList.Count);
  31:  
  32:            mocks.VerifyAll();
  33:        }

 

I won't get into the naming conventions since I've covered those in previous blog entries. This test has another problem. It's trying to validate that when you ask for a list of employees, only those that are not marked as deleted are returned. To simulate the data returned, it is making use of a mock repository (RhinoMocks).

Before looking at the issue, let's decide what the purpose of the test is. We want to test that when a system has two employees, one of whom is marked for deletion, a call to GetAllEmployeeesByCompanyId returns only one employee. Therefore the SUT is that method call.

In line 9, a mock of the repository is created, of type StrictMock. This is a particular call of RhinoMocks, but pretty much all mocking frameworks have the same feature. A strict mock indicates that every expected call should be made. Therefore, when on line 32 a call to VerifyAll is made, this test will fail if mockRepository.GetEmployeesByCompanyId has not been called with those exact arguments. In our case, the test passes and all is fine. Or is it?

The problem however if we change the internal functionality of EmployeeServices.GetAllEmployeesByCompanyId. If some reason or other we make a second call to EmployeeRepository, that doesn't change the outcome of the method, this test will fail.

Why is that? The reason isn't so much that we are using StrictMock as opposed to DynamicMock. The problem is that we are trying to test too many things in the same test. In actual fact, in this particular case, only ONE thing should be tested: retrieving one employee out of two. However the introduction of a StrictMock and the VerifyAll call has had the side-effect of introducing an interaction test, something we are not interested in.

Mocks can be great and very powerful. But use them wisely. If you start creating strict mocks (which should only be used for interaction/behavioural testing), then you're just making your tests very brittle. The slightest change in behaviour, even though it's encapsulated inside the method being tested can break your test. This pretty much defeats the whole advantage of unit tests. We create tests to isolate functionality and test that functionality in isolation. As soon as we tightly couple our tests to a particular implementation, and when the system under test is not trying to validate that implementation, the whole thing blows up. In another blog post, I'll re-factor the test to make it much simpler and less volatile.

R# 4.1 out

Get it here: http://www.jetbrains.com/resharper/download/?41nl

They've been working on improving performance.