A Funny Java Flavoured Look at the World

Monday, October 09, 2006

The importance of method stubs and skeleton code

I have read a couple of articles recently stating the importance of using stubs and creating skeleton code. There were slightly old articles. I have also read this in the Test Driven development coding ethos, write you code have it return the correct type and correct value.

Another article was talking about mock objects, start off splitting your the code you want to write into layers, work out an interface for each layer or object, return the correct type and a successful value e.g. true or a agreed value and then connect up all the code.

Although I think I picked up this idea by reading articles like the ones above I do it naturally now without thinking about, as soon as I am writing a method or class I get a test up to make sure I am connecting to it correctly.

Maybe with the progression of IDE's it doesn't happen so much these days but I remember coding and everyone do different bits and then all hell breaks lose as soon as you try and use each others code and connect/put them all together.

When working by yourself it's not to bad because you can steps to combat this. The reason I think it's important to connect to all your methods and objects before you start writing the code for them is so you know you are connecting to the correct piece of code. Once you know you are connecting to the code you think you are connecting to you can build the other code up which connects to the stub and also build the stub up, safe in the knowledge that it won't break anything and everyone's code can currently compile.

Another advantage of building a stub method is that you can quickly find out if you the variables passed in and returned are the variables you need. Earlier this week I was writing a method and when writing the stub I realized I needed another variable to calculate the variable returned from the method.

Obviously I could have spent more time designing the method but writing the stub made me think about the method in more detail, which in this case effected some other methods in the class which I needed to change to get the extra variable to pass into my stub method.

So the stub methods benefits are

1. You can make sure all the code is connecting to the correct methods and it is returning the correct value
2. All you code compiles
3. You can code around the stub method because it is returning a correct value
4. It's a good platform to build on
5. It helps you spend a bit of extra time thinking about the design of your methods and how it is all going to fit together, you could put a whole skeleton code of the project you are doing and slowly fill in the stubs. This gives you a holistic view of the project and allows you to see if you think it is going to work or if there are any areas you think there could be trouble in.
6. Stub methods are a great place to leave comments for yourself or others who will be working on the method/class. You could leave rough plans of how you think you are going to code the method.

0 Comments:

Post a Comment

<< Home