Tuesday, 21 April 2009

Thank the stars for the STL

The C++ Standard Template Library is something which I've been taking for granted lately but every once in a while something happens that makes you stand back and appreciate the functionality it provides. Today was one of those days.

For reasons I'll not go into I encountered the situation today where I needed to take a hexadecimal value representing a 32 bit value and convert that into an IEEE 754 type single precision value. Fortunately after some looking around I found the entry on wikipedia which explained how a single precision value is represented in a 32bit binary value, at that point however I was having nightmares about bit shifting and XOR'ing with values to determine which bits were set!

Enter the STL, which allowed me to implement a solution which although maybe not as compact or efficient as other methods is at least easy to follow and maintain/comment. And it was all thanks to the bitset class which takes an unsigned long and represents it as an array of bits. From this point it becomes trivial to determine the sign bit, the 8 exponent bits and the 23 (plus 1 implicit) mantissa bits. So after a little reading and some coding I ended up with a method which reliably transforms hexadecimal strings (i.e. DEADBEEF) into single precision values.

Maybe not the most interesting of posts but I felt the need to share :)

Thursday, 16 April 2009

The D Programming Language

For some reason last night I decided to have a quick look at the D programming language, it's something I've looked at briefly in the past but only in passing and never really with much enthusiasm, not because it's a lousy language but because I just didn't commit the time to having a proper look.

As I had some time off today I figured I would have a slightly more in-depth look at the D language and set up my laptop so I could compile applications. D has two compilers available, the compiler from Digital Mars and the GNU D compiler, as only the latter is in the Ubuntu repositories I opted for installing this which was a simple case of opening a terminal and typing in:

sudo apt-get install gdc

All other dependencies were taken care of and a minute later I can compile D code. Next up was an editor, I wasn't to bothered about a fully fledged IDE as I don't like to use them when I'm learning a language as I prefer to make mistakes and learn from them, but syntax highlighting would be useful as it lets me know I'm on the right lines. To my surprise my favourite Linux text editor, Geany, already comes with D support which is great because Geany also has compile/build/run options from the editor which makes it a very easy editor to work with as you can compile your code without building it and identify errors without leaving the editor. It's probably also worth noting that after a very quick investigation it also turns out the GEdit (the Gnome text editor) has syntax highlighting for D as well and Code::Blocks also has built in support.

Now for the downer, unfortunately because D is still a fairly immature language there are very few tutorials or examples out on the web so it looks like if you want to get into it then it's largely a case of studying the language specification and standard library reference. Althought from doing this it's nice to see so much functionallity in the standard library including such wonders as threading, XML processing, networking as well as standard features expected such as generating hashes (via MD5), regular expressions and string manipulation. Also available through the standard library is support for specific windows and Linux calls which makes it easier to write platform specific code.

There is also under active development a seperate library intended as a replacement for the standard library called Tango which looks promising but it is under active development so doesn't necessarily provide stability of the standard library.

So what makes D so appealing as a language? Well for a start there is the C/C++/Java style syntax which makes it easy for programmers to pick up and read. Not convinced then how about memory management? That's right D utilises a garbage collector for managing memory which means that there is less emphasis on the developer to get it right, although that doesn't mean memory is no longer a concern, for instance variables should still be declared only in the scope for which they are required.

Also of interest is design by contract, this is an interesting concept which I'm going to spend a little longer investigating but essentially it means that when you write a method you can specify a contract which can state something like "If you give me a value which meets these conditions then I will guarantee that I will return a value meeting these conditions". So when a program consumes that method it enters into a contract with the method and if either party violates that contract then the call fails, it seems a little odd at first as you can do this by running checks in code on a methods parameters and return value but with the contract you outline the requirements formally, the intention being that the contract is not broken when the program executes if it does then the program fails and fails hard.

So where does D sit in a world with .Net and Java as the two main programming architectures? Is it a niche language, a hobbyist language or something that should be taken seriously? It's certainly to early to tell how life is going go for D but I hope it goes well, at the moment I would say that it falls into the hobbyist category as it certainly doesn't seem as mature as other languages but it is getting there which is why I don't think it will always stay a hobbyist language. So should it be taken seriously? Well, yes, because D is very much C++ done right and there is still a requirement for compiled langauges instead of ones running in a VM, I am sure there will be those who will recoil in horror at the thought of having a garbage collector managing memory but I think that they work and a good garbage collector can do as good a job as a good programmer who is on the ball with memory management.

I think the future looks promising for D, as long as it doesn't slow down in development and stays relevant then in a few years we may start to see it getting some decent commercial use. I for one will be spending a little more time looking into D and getting to know it.

Friday, 10 April 2009

Assignment FAIL!

One of the things I've been having to do lately is make sure that my C++ complies with high integrity C++ rules and guidelines (HICPP) which though interesting has been a massive pain as I've gotten use to doing things in a certain way and now I have to double-check everything I write for compliance. One which takes more getting use to than most is the use of the comparison operator in control statements, normally I (along with most people I think) would do a check something along the lines of "if my value is equal to three then...", or

int x = 3;
if (x == 3) { ... }


Now this is invalid in HICPP because a programmer could accidentally type "if (x = 3)" which of course is assignment instead of comparison, so the rule states that instead the comparison should be written as

int x = 3;
if (3 == x) { ... }


The reason for this is that the former is perfectly valid C++ syntax and will compile and run (but not work), whereas the latter will cause a compiler error because you cannot assign a value to a literal value.

This is one of a few rules which I am likely to carry into my every day coding as it helps to prevent me from making stupid mistakes which as a human I am more than capable of making. Another one which takes only a little more effort but can save until problems is when looping over a collection, the rules state that instead of doing this...

vector values;
// Add values to collection

for (int i = 0; i < values.size(); i++) { ... }


It should be written as...

vector values;
// Add values to collection

int size = values.size()
for (int i = 0; i < size; i++) { ... }


Which again makes sense as with the latter method you avoid problems if the length of your collection changes. Of course this doesn't work so well if the loop is suppose to change the size of the collection, but that's a reasonable exception.

Sunday, 22 March 2009

What a Git!

Unfortunately I've not been able to do much with my mono algorithm project lately since publishing the first piece of code which is a shame, the plan though is to start implementing different algorithms starting with some from the C++ STL library. I don't really have a plan yet of which ones so it will most likely be which ever ones interest me and which I'd like to learn more about such as binary searching and various sorting algorithms.

One thing this has prompted though is version control, and I now realise that I really don't know enough about them. Historically I've been use to commercial systems with nice UI's letting me point-and-click for what I want to do. Now however I've got to think about working (possibly) with more people spread over a larger area and not the few guys sat around me. Because I went with Google Code in the end I've restricted myself to Subversion which isn't to bad as I have at least a little experience with CVS but I still could really do with learning the basics again.

I am a little disappointed however as I've recently been reading about using distributed version control systems such as Git and Bazaar which I really love the idea of using. One of the things I really like is having local branches to work on which have no impact on other peoples work unless they start viewing my repositories. The advantage of this has become clear lately at work where a few of us are working on a project where each section is being developed in a seperate branch in CVS, the problem here is the proliferation of branches and keeping track of them. In a distributed world we would all work independently of each others branches and then commit our changes to a main repository which would hold the final code base.

Anyway, I'll need to pick up Subversion soon but I think I'll be investigating the distributed systems as well as I think that they are the way forward.

Finally in C++ and sucky support

Well the time has finally come and I'm now coding with C++ professionally, depending on your definition of professional! And to ease myself into it I've been given the task of writing the code for CRC-32 and MD5 generation, which has so far been driving me nuts. First of there has been issues with reversing streams of data and then with reversing the bits of the finished hash as well as learning how to generate the hashes in the first place.

Whilst I've been doing this I've been making extensive use of Google for getting answers to some awkward questions and what's been frustrating is the level of support out there. After a while digging through various links re-phrasing the question exactly right and making various blood sacrifices you do eventually find something which gives you the answer you want or at least provides enough information to figure it out yourself. But what I have found in various forums is a set of responses which frankly disgust me, two of my most despised are:

1. We are not doing your homework for you
2. Why are you doing that in the first place it's just stupid

These kind of responses firstly don't help anyone and secondly they can be detrimental to the site they were posted on, whose going to want to re-visit a support site where when you ask for help you just get grief? Certainly in my case they don't help because:

1. I don't have time to spend days researching a problem I've got deadlines to meet
2. Sometimes we don't have a choice to do things in a certain way

I've maintained some apps before where I've seen developers try to manipulate code and algorithms in ways that shouldn't be possible or even legal but I've still got to support it and the company I'm working for at that time may not be willing to pay for me to re-do it properly.

So really if you are on a support site and someone is asking for help, try to help them, by all means offer them advice on how it could be done better but don't expect that they can do things that way and if you are going to reply make it useful, if you don't want to help someone then just don't post a reply and move on to the next item.

Tuesday, 17 February 2009

Update and Fedora Feature

I know I've been quiet lately but it's been with good cause, the project I was due to start using C++ at work has finally started. The coding side hasn't really started yet as we're still working on architecture but I suppose now I find out if the effort I put into picking it back up again has paid off.

Sticking with C++ development for a minute I was intrigued to see that the next version of Fedora is coming with MinGW. This is fantastic news for developers as we can now write and compile applications for Windows on Linux instead of having to rely on a seperate windows installation. It also puts Linux in a good position for gaining further ground in software development companies where they may want to save money by not having to pay for windows licenses but have to if they want to compile their apps.

I'll be interested to see how well it works when Fedora 11 is released.

Sunday, 1 February 2009

Unexpected Surprise

Have you ever been surprised by someone who bought you something completely unexpected? Something which they would not normally get for you as it's uncharacteristic of them?

Well during my recent birthday I got a note through the door whilst I was out from the Royal Mail saying that they had tried to deliver a package but of course no-one was home. So later that day I headed down to the delivery office and picked it up and one top of the items in the box was an Amazon note and a message from my Dad wishing me a happy birthday. Now in the box was one book which really took me by surprise, he'd gone and bought me "Beginning Game Development with Python and PyGame"! To say I was bowled over would be an understatement.

My dad was the person who initially got me into programming, he insisted when I got my first computer that I learn to use it properly and it not be used only for gaming. Whilst I'm sure he meant that I should learn how to write documents and edit graphics with it instead of hacking around with code he encouraged me to continue anyway, but never actually as far as buying me a programming book so this is a first. I know he bought it from my Amazon Wish List but there were other books on there along with music and DVD's so the fact he bought this one is incredible. I'm fairly sure he's not following this blog either as usually just turning a computer on is enough to get him fed up with using them so the idea he spends time browsing the web and reading a blog of mine dedicated to programming is slightly more than unlikely.

But anyway I thought I'd mention as the book is likely to drive some further posts to here and I'll write up a review on the book when I get somewhere useful with it.