Category: Programming

Working with the Twitter API in C++

Recently I’ve been playing the Twitter API through C++. I read through the Getting Started Guide and discovered that I had cURL installed on my system. It was so easy to grab my profile that I couldn’t resist having a play around to see if I could get timelines and update my status.

cURL has a an API available for a variety of languages that is really simple to use and it’s been really easy to implement some basic Twitter functionality into a command-line client. Using TinyXml to parse the results of HTTP requests I can grab posts and user information. Once it’s setup you can make a simple status update with a single method call.

The problem I have now is how to parse the temporal information. It’s one thing to grab simple text but there is a lot of numeric-data embedded in it that is quite useful. I’m looking at Boost.Date to see if that can help, the next thing to do is to be able to say how old a post is and filter results by time. Got any tips for doing this sort of thing?

I’m going to keep playing with this and see if I can come up with something useful for a proper app.

New Years Resolution: Fewer Code Comments!

Clean Code: A Handbook of Agile Software Craftsmanship

Recently I’ve been reading Clean Code: A Handbook of Agile Software Craftsmanship (Robert C. Martin). This is a must read, and I’d recommend it to programmers of all skill levels. It has reminded me of many things that I’ve mistakenly unlearned over the past few years as I’ve struggled to battle against tighter and tighter deadlines and push out as much functionality as possible.

One of the main points that I’ve taken from the book is that comments make programmers lazy. For some reason we seem to think we can name our variables anyway we like as long as we give them a nice descriptive comment. Likewise, instead of refactoring code into nice methods with expressive names that follow the Single Responsibility Principle, we tend to create long functions that do many things and again litter them with comments describing what we are trying to do. This leads to problems, as our comments can be misleading, without actually reading and understanding the code we don’t know what is really going on.

My new years resolution is to comment less. In fact, I plan to write as few comments as I can. Instead I will concentrate on writing expressive code that speaks for itself. This will lead to some quite lengthy function and variable names, but I’m a fast typer. So do you think comments make you lazy? Or are they a necessary evil or the limited expressiveness of our high level languages?

Compiling Boost on Mac OS X for 64bit Builds

Since I have a bit of spare time on my hands at the moment, I thought I’d get back into playing around with Boost. Since my Simple Guide to Installing Boost on Mac OS X article was published we have had a few new releases of Boost, and more importantly a whole new version of Mac OS X. So does my guide still work?

It does still work as I described in my original article, but I have found one slight problem. When I try to compile a project using one of the dylib libraries I get link errors. It seems XCode projects are now defaulting to 32/64 bit combined binaries. The original build instructions only compile pure 32bit binaries so we can either switch our XCode projects to pure 32 bit or we need to compile Boost with the 64bit architecture.

The fix for this is simple, when you compile your binaries with the bjam command simply use the following command line:-

./bjam architecture=x86 address-model=32_64

This will give you a pure x86 (No PowerPC) set of binaries that are both 32bit and 64bit build compatible. You could use:-

./bjam architecture=combined address-model=32_64

To get both Intel and PowerPC compatible binaries if you need them. Once you’ve done this you can carry on using Boost as usual.

Using BOOST_FOREACH for Simpler Iteration

I’m working on a little C++ project at the moment. As part of it I’m trying to learn more about the Boost libraries and do clever things with the STL. I have found myself writing a lot of simple loops that iterate over containers of objects and do simple things like call a method on each object. I get tired of writing iterator code like this, it’s something we end up doing a lot as C++ programmers. The syntax for defining and using iterators can be a little long and it can be a right pain to debug if you make a simple typo.

Enter the BOOST_FOREACH macro. I am aware of the STL for_each() function that uses some fancy binding objects to do all sorts of clever stuff. Boost Bindings extend this and create a really powerful set of tools. The problem with this stuff is that it looks a bit weird and isn’t that easy to understand. While I was reading tutorials I came across BOOST_FOREACH. This simple macro gives us something very similar to the foreach keyword in C#.

Imagine you have a container of objects of class Foo, and you want to call the Bar method on each instance of Foo. Usually I’d write something like this:-

for( vector<Foo>::iterator f = container.begin();
                          f != container.end(); f++ )
{
    f->Bar();
}

The BOOST_FOREACH macro can cut out some of the donkey work by doing all the iterator stuff for us. So here’s the above example using BOOST_FOREACH.

BOOST_FOREACH( Foo &f, container )
{
    f.Bar();
}

I like this alternative because it’s a lot cleaner than the classic version and it will work the same with both STL containers and standard C arrays. You also end up typing less and so inevitably will make less mistakes. It’s not going to work in all circumstances, any code where you want to manipulate the container itself is probably a bit risky, but it makes doing simple stuff a bit easier and leads to better code without having to get bogged down with templates and std::for_each.

The Effect of Meetings on a Developer’s Productivity

I came across this article titled Maker’s Schedule, Manager’s Schedule. It makes a very interesting point about the effects of meetings on a Software Developer’s productivity. I have to agree with everything in it. My personal experience is that a meeting completely blows at least half a days development. I need long periods of time to focus on a task, find my zen and really get to grips with solving the problems I am working.

Since I became a team leader, I have tried to avoid organising unnecessary meetings, especially when it is obvious that the team have their minds on what is important to us all, their code.

What do you think? Do you find breaking up your day with meetings means you get less real work done?

Game Programming with C++ and Gosu

I’ve always had a desire to make computer games. It obviously stems from my misspent youth playing Nintendo and getting a serious case of SAD. Sadly, I’ve grown up now and spend my days writing code instead of playing games. I’ve not realised my dreams of becoming a professional games designer but it’s something I’d like to do as a hobby.

I’ve been doing a fair bit of hunting round to find a decent set of tools to get me started. Coding everything from scratch doesn’t really appeal to me so I want something that does all the donkey work of setting up windows, graphics devices, sound and controller input, without being a full blown Game Engine. I also don’t fancy learning another programming language, so ideally it will be a C++ library. It must also be cross-platform as I use both Mac and PC. During my search I’ve looked at SDL, Allegro and Gosu and have decided that Gosu is definitely the way to go.

Gosu is pure object-oriented C++ goodness (Ruby bindings are also available), the API is neat as it is made up of only a small collection of classes. It is primarily for 2D games but it has support for mixing in OpenGL if you are brave enough to do some 3D. It is built on top of the Boost libraries (See my previous post on Installing Boost), so if you need advanced stuff like threads you can get those without too much hassle. It’s also cross-platform as there are binaries for Windows, Mac OS X and Linux. One of the real bonuses is that it is actively being developed and has some fairly comprehensive online documentation.

The getting started guides on the Wiki are great, and walk you through all the basics from installing and setting up a project, to writing a simple game. After I’d got everything installed properly it took me about an hour to code up a really simple Snake game clone from scratch. I’m going to play with it some more and see if I can knock up something a bit more advanced. Next stop Space Invaders!

Simple Guide to Installing Boost on Mac OS X

Boost

Do you want to get the Boost C++ libraries compiled for use with XCode on Mac OS X? I struggled to get it working so I’ve decided to write this mini-guide so I don’t forget. A little word of warning, the installation process requires a bit of command line kung-fu, if you are not comfortable using the terminal then you should read up on that first. This guide is correct at the time of writing using Boost version 1.39.0 on Mac OS 10.5.7. Are you ready? Then let’s go…

  1. The first thing to do is to download the Boost distribution from the Boost downloads page.
  2. Unpack the archive somewhere sensible, I’d suggest the desktop. Don’t do it where the absolute folder path will have any spaces in the name e.g. “/users/matt/code/bad folder/”, this will prevent the install script from working later. I found this out the hard way.
  3. Next open a terminal window and navigate to the Boost root folder you just unpacked, it will be called “boost_1_39_0″ if you are using the current version. If it’s on your desktop you will need to use “cd /users/username/desktop/boost_1_39_0“. We refer to this path in future as the $BOOST_ROOT
  4. Next run the bootstrap script by typing “./bootstrap.sh“. This configures the install tools for your environment. Check it for error messages, this is where I found out my path was bad.
  5. If you run the “ls” command you should now have an executable called bjam.
  6. To compile Boost, type “./bjam architecture=combined” and press enter, this will build Universal (x86 and PowerPC compatible) libraries.
  7. Make a cup of coffee and wait while the libraries build.

That’s all there is to it. You should end up with a load of .dylib and .a library files at the path $BOOST_ROOT/stage/lib. There’s a more detailed explanation of all this on the Boost Getting Started on Unix Variants page if you get stuck or something goes wrong. Your next job is to configure an XCode project to build with Boost, which is outside the scope of this article. I may do a post on it in the future.

Update, Saturday, 12 December 2009: The latest version of Boost is 1.41 and it installs on Snow Leopard without any problems following the instructions in my guide. Thanks.

Guide to Network Programming

Many moons ago I did a Systems Programming course a University. It wasn’t the best run course in the world and it left me completely confused about IP, Sockets, Streams and other related scary Unix technologies that seemed so foreign back in the late 90’s.

Recently my interest in this area has grown and I thought I’d have little look for a decent guide. I came across Beej’s Guide to Network Programming, and I heartily recommend it. If you’re interested in an introduction to BSD socket programming it’s a must read, and it’s also fairly entertaining in a geeky humor kind of way.

If you’re out there, my old System’s Programming tutor, you should definitely read it, you need the help :-P

Image | WordPress Themes