Saturday, June 16, 2012

Object Oriented C Reboot

As is normally the case I started something and then put it down and did lots of other things instead. In a rare bout of refocusing though I'm picking up the object oriented C (OCode) stuff again.

Previously I got very side tracked into setting up a perfect development environment as I would like to use at work. Something that would be compatible with automated build systems. Furthermore, I started trying to port the subversion externals pattern that I like to use for shared code to Git on GitHub - I quickly discovered that I'm still very much a Git newbie. This was not very lean of me...

So i'm rebooting. This time around I will focus on the task at hand which is to create a simple templating and/or preprocessing system for generating class and interface boiler plate for use in OpenTV applications which are exclusively written in C.

Let's start with a description of the pattern I currently use to implement classes. This part is simple and the boiler plate is not so bad.

The header:
The implementation:
Excuse any obvious errors, I just typed that out without trying to run or compile it, but i think you can get the idea - nicely encapsulated, right?

So that's all good. A little boiler plate in exposing opaque types and constructors/destructors but not so bad. The problems/challenges(/opportunities ;)) start when I try to extend this pattern with interfaces. Let's extend the example with that pattern.

The interface:
Now we can see some really obvious complexity. Just look at the length of it and it doesn't even do anything really. Immediately apparent is how much work it would be to add a new method to the interface.

  1. Add a new typedef for the method prototype
  2. Add a field to the interface structure
  3. Add an argument to the interface constructor
  4. Add a redirector method to allow the implementation to be called

It's fiddly work and potentially error prone (and this stuff can be hard to debug). The good news is that interfaces tend to be fairly stable once done as they don't contain business logic (although they may represent it I guess).

We're not finished though. The interface has to be implemented by our class for it to be useful.

Here's the new header for our class:

Notice the addition of a method to get an instance as an instance of MyInterface this is our casting convention.

Here's the new implementation of our class:

That wasn't so bad, we:

  1. Added a field to the class structure to store the interface instance
  2. Implemented the interface method
  3. Constructed an interface instance in the class constructor
  4. Destroyed the interface instance in the class destructor
  5. Added the method to implement our casting convention
Again it was fiddly though and remembering that we have to do these things in every class that implements the interface we are now exposed to following types of errors:
  • Memory leaks due to forgetting to destroy the interface
  • Strange affects from casting incorrectly in method implementations (doesn't seem likely until you remember that casts are a nightmare for hiding copy paste errors)

So let's review. We now have 3 quite complicated files that are quite hard to maintain. Particularly, changes in interfaces result in a large quantity of refactoring radiating out all over the place. Plus we have to be careful whenever we implement a new instance of an interface. And remember that this example only implements 1 interface method!

This is a barrier to using the pattern which I would like to overcome. Just writing it up took longer than I expected and I have to go out now so I guess there will be a part 2 where I actually get started on how I would like it to look and work :)

...
...
...

As an aside the work I have been doing to make OpenTV application generating and testing makefiles using make function implementations will likely be the subject of a future post. As will figuring out how to share code and resources across Git repositories/projects.

Friday, June 15, 2012

Required watching!


http://jamesshore.com/Blog/Lets-Play/

Well it might be if it weren't 197 episodes and counting but this series of videos illustrating test driven development (TDD) in Java and Eclipse are really excellent. I'm currently up to episode 25 (since yesterday... addicted!) and already I have learnt so much about:

  • writing tests up front
  • emergent design
  • AND just how magical eclipse is when it comes to generating code and refactoring in Java

I really recommend watching some of these. It starts with a completely blank application so I would go from there.

I really want some of these IDE features in the OCode development we do too. And I can see that there may be some mileage in that.

Now i can't wait for James' new series that I gladly helped fund on Kickstarter.

http://www.letscodejavascript.com/

2 things that have got me excited recently in one package - TDD and server side Javascript... Yay, Node.js!

Thursday, June 7, 2012

Error handling in C


An interesting and somewhat familiar set of macros for error handling in C:

http://c.learncodethehardway.org/book/learn-c-the-hard-waych21.html

A possible coding standard. Not with the debug flags (I hate debug builds!) but interesting all the same. I can see this approach getting rid of some of the over indentation caused by checking return values. As a coding standard though, I am resistant to always using the return value of a function as an error code... maybe I shouldn't be.

The most interesting part is that it looks and behaves like exception handling to an extent. A problem which is altogether too difficult to mess around with in C. However by stopping short of trying to automatically pass you back up the stack perhaps this does capture some of the essence (read value). A good, lean application of the 80/20 rule.

Still, to retrofit this stuff would likely fall short of being lean. Maybe I can mix it into the the Object Oriented C stuff I started working on.

Saturday, June 2, 2012

Object Oriented C

I'm waiting for virus updates to complete and the new windows GitHub client to install - why is my computer so slow today!?

Anyway a chance to write a blog post then.

I'm becoming increasingly agile :) ... no not like a cat :s

Recently I completed my certified Scrum Master and Product Owner training and we're making great progress at work putting it all into practice. It really has reinvigorated my interest in software development and once again I am reading voraciously on subjects that actually excite me (you'll maybe shake your head when you realise what those subjects are ;)). Can't remember having this feeling since I first started coding back in University.

So what's on my mind today?

Well, I've been reading Michael Feather's "Working Effectively With Legacy Code" and once again I have found an author who really does reflect, distill and reinforce what I only really knew at the back of my mind. This is great as it gives me more confidence that I'm on the right path but also provides a glimpse of where that path leads.

One place that it leads is to greater use of object oriented principles in our code.

Some background... We write software for the OpenTV middleware in C to run on digital TV set top boxes. This is not object oriented. It isn't even really C, we can't use standard libraries and have to write pretty much everything from scratch. This has included basic libraries and a lot of tools and frameworks. Our main focuses at the moment are


  • Reduce build times and complexity
  • Use more off the shelf and advanced development/debugging tools
    • There is a freely available OpenTV IDE based on Eclipse which is very useful as it also includes a set top box emulator allowing us to step through code (http://community.opentv.com)
  • Automate builds / implement continuous integration (Jenkins)
    • This conflicts with the eclipse IDE which is not good with headless builds, we've worked around this though, it just means we can't take advantage of standard OpenTV project templates. No biggy, we know what we're doing :)
  • Add a unit testing framework and increase test coverage of legacy code
  • Test driven development
    • We have the framework so we have to do this to really reap the benefits in my opinion
  • Automate tests
    • Very much a TODO - the set top box emulator does not like to run on our continuous integration server unless it is not running as a service and some one is logged on. It uses DirectX :s
  • Move away from specification documents to specification by tests.
    • We are looking at integrating Fit and FitNesse to capture business rules and automate acceptance testing

So why more OO?

Simple really. We need units to test. We need those units to be independent so that we can develop and verify them quickly. The approaches of TDD and the means of dealing with legacy code to make it more maintainable/manageable require great discipline and effort. We need to make the task less onerous. A major part of doing that is to use interfaces to break dependencies between libraries. There are other options in C (preprocessor for example) and we have to be careful about performance on our platform. But with careful application I am convinced that we will get the best mileage out of a consistent application of OO interfaces.

So today I am going to figure out a good standard pattern for implementing classes and interfaces in C. Probably using the preprocessor to do some of the hard work. Previous attempts have always resulted in a lot of unwieldy repetition that I sometimes think only I understand (making it instant legacy code!).

Ok, installs completed :) New git repository created. I wonder if I'll just end up reinventing Objective C.

Wednesday, April 4, 2012

I'm back! Let's see how long for :)

Wow, we're well into 2012 already and I haven't posted anything since 2010!

So what's changed?

I guess I was too busy doing stuff to blog about it for a while but yeah, quite a bit has changed. I have a lovely girlfriend now (which is probably another reason I haven't posted) :)

I quit drinking just over a year ago and haven't looked back on that. Just before my exam for the WSET advanced certificate in wines and spirits (yeah that helped - fail!)

I have taken up tea drinking in quite a serious way (and am now a very fussy tea snob ;))

I took up Ashtanga Yoga, but have lapsed already :o (although still cycling, yay!)

I have a renewed interest in work mostly due to discovering Scrum.

So now my plan is to get back into blogging so perhaps we can expect some stuff about tea and agile development :)

Tuesday, November 2, 2010

Gerrie Knetemann Classic

Still catching up on blog posts, I've now settled in with a block of stilton, crackers and a pot of honey. Mmm.... not the classic combination of stilton, digestive biscuits and a glass of port but recently I've developed a real taste for honey with cheese. I guess most people know that honey works well with goat's cheese. However, for me a real revelation was tasting a nice mature pecorino with honey blended with truffle oil at my favourite local, Italian wine bar, Wijnbar Divino.

Anyway, I digress. This post is about cycling (a little). So some friends and I decided to enter a team into the Gerrie Knetemann Classic this year. A nice 75km tour around the green heart of Holland. Naturally this meant I needed a new race bike and all the accoutrements, yay :)

So after a bit of research and shopping around I organise a test ride on a Stevens Aspin way out on the other side of the water behind centraal station. Believe it or not, this is the first time I've taken a ferry over the Ij behind the train station (I've lived in Amsterdam for 8 years now!).

And look what's docked at the ferry terminal on the other side!
Submarine!
An old Russian submarine... cool! I think it's been there a while too. It's been tagged by a local artist :)
Underwater art?
Anyway, even though it's raining and it takes some convincing to get the shop to actually let me go out on the bike I make the purchase and get some new cycling gear so i look the part. Just have to pick up the bike the next day from the Haarlemmerstraat shop (couldn't take it right away as I was on my city bike - but i can walk to their other shop).

A little excited, I head up the next morning on foot and see the cutest thing in a shop window on the way.
Awwww :)
So sweet, a cat curled up, asleep on top of a big, fluffy teddy bear... in a shop window!! It's another gloomy, rainy day but it's hard not to smile at the world sometimes. I snapped a few photos for good measure and headed on to the bike shop.

All's good and I have the first proper go on my new ride on the 20km commute to the office. Definitely grinning now. For the rest of the week my commutes constitute training for race day.

I'd love to say that it went perfectly but the route was confusingly marked in some places and due to some wildly differing paces our team got a bit split up. A couple of us, thus, ended up taking a couple of wrong turns. At one point following the 100km course for a few kilometres. At the end we'd cycled 80km of a 75km race, oops!
Just about to start :)
Next up a great trip to the UK...

Monday, November 1, 2010

Catching up! Food fest and an important find on a treasure hunt.

Been a while since I posted anything here so figured it was time I got my finger out. It's been a few months of parties, sporting activities and general out-and-about-ness. I'd love to give a full rundown but I think its best to stick to the stuff that's safe for public consumption ;)

So for this post lets just cover Saturday September 4th. The day of the Haarlemmerstraat Food Festival and the Amsterdam Meetin group treasure hunt. A fine sunny day, I enjoyed a few tasty treats at the food festival in the morning. Some gourmet pizza, some tasty noodles from a Tibetan restaurant's stall and to top off my lunch a nice, light Pina Colada served in a fresh coconut.
Mmm, tasty!
So i'm walking home with my cocktail when I run into a colleague out with his kids - seriously I'm not usually wandering around amsterdam carrying cocktails with umbrellas and I'm sure this can't help with my reputation in the office!

The afternoon was fun too, catching up with amsterdam meetin group for a treasure hunt. Good folks and we came second in the final results - winning me a t-shirt. But for me a highlight was coming across this hotel entrance.
A sensible precaution.
Back in February, I was skiing with friends in Switzerland and staying in a very nice hotel between Gstaad and Saanen, the Alpine Lodge. We had a great stay with great food and wine (most of which we brought with us - but the hotel's cellar was also very good) and the half in-door, half out-door pool was a great way to relax after a day on the slopes. Less of a highlight though was when I walked into a glass partition in their games room, cutting my eyebrow resulting in some profuse bleeding and a quickly developing black eye. I was concerned that I might need to go to the hospital for stitches and headed for reception to seek a second opinion and a first aid kit. On explaining what had happened the first thing I was asked was, "Is the window damaged?"... Huh? No it wasn't! Anyway I shall send this picture to them with a note regarding correct health and safety procedures ;)

By the way I have a friend who works at Hotel V and by all accounts it's a very cool place!

So, next up is the Gerrie Knetemann Classic cycle race (ok, more of a tour than a race).