Saturday, 15 August 2009

Taking a new direction

I've been having a bit of a crisis lately (a little different to the usual ones), I've been programming in C# on the .Net framework for about 4 years professionally now and a little longer personally as I was fortunate enough to convince my employer at the time when they wanted to shift to a different technology that C# and .Net was the way to go. I still think that was the right decision as it didn't have a very steep learning curve for everyone on the development team, who already had experience in similar languages, and most of the internal users who were the intended customers already had the framework installed. And I can't really knock my own decision to learn the language as it's been paying my bills for a number of years now which is always a good thing. So what could this crisis possibly be?

As I've mentioned in a number of previous posts I've been working on C++ apps lately at work and shockingly I've been quite enjoying it, I wasn't expecting that as my previous endeavours with the language have been painful to say the least, looking back though it would seem that most of this pain can be attributed to poor learning materials but with good references such as Learn C++ and the this has issue has been resolved. So why is this a crisis, well it's made me look at C# and .Net again and where the technology is heading and I can't say that I'm overly happy.

One of the biggest complaints I have and that I've heard from a number of fellow programmers is that the technology is moving to quickly, by the time a new feature has been introduced and you've had barely sufficient time to learn how to use it and apply it effectively a new release is out and there is something new to pick up. I've been on a few courses and to a couple of events recently and had the opportunity to speak to developers who are still working with version 1.1 of the framework because they either don't want to or simply can't keep on upgrading their apps to newer version each time they come out. I can see their point of view although I probably would try updating to a slightly newer version by now.

Another problem is with the way the language is developing, back when C# 3 came out the "var" keyword was introduced, this was a way of saying I don't know yet what this variable is going to be but figure it out for me at compile time. A number of people were wary of this wondering if this statically typed language was beginning to get lofty ambitions of trying to become a dynamically typed language, but they were calmed and told that this wasn't happening, and then with C# 4 we get the dynamic operator, hands up who didn't see that one coming!

This is really why C++ has thrown a bit of a curve ball my way, sure it does some things a funny way and it has the opposite problem of taking an age to progress technically unless you're willing to use the boost libraries, but it has a very clear identity. Python is much the same, it progresses at a good pace, letting people catch up, learn and implement new features and it has a very clear identity, it knows it's a dynamic language and doesn't pretend or attempt to be something else. All of this has prompted me to look at the languages I know, I'm going to keep C# under my belt because I've invested a lot of my time with it and Mono is starting to show some real promise (yes I am a Mono lover) plus I'm sure I'll need it to keep on paying the bills for a while yet. But I've started looking at Java as well, again it has it's quirks (type erasure and lack of unsigned types being the biggest ones) but it moves at a fairly decent pace and again knows what it is, plus it's a fairly easy migration from C# as it borrows heavily from Java. In fact I have a number of books covering things like design patterns and object oriented design which have all of their code examples in Java but I bought these as a C# developer and the converted code is almost identical.

So recently I've been running through the Sun Java tutorial book on-line and although it's hard going in some places, as the explanations of some principles aren't that well written, it's been reasonably productive. I'm not using a full blown IDE at the moment even though I have Eclipse installed because I've always thought that using a programmers text editor when learning a new language is more useful as you learn the language properly instead of relying on things like intellisense to do the work for you. And I've got Bazaar set up to version control my sample code, which has been working brilliantly I've got to say, if you're looking into version control at the moment I would strongly recommend you look at this one. For personal code its so easy to set up and use, I've got Olive installed for a GUI front-end but I tend to find myself using the command line more because it's just as easy.

I should probably also mention that another reason for choosing Java here is because I have a Google Android phone and Java is the language of choice if you want to develop apps for it, and with Ubuntu looking at bring Android apps to the desktop it means that Java apps can reach a larger audience. So I was looking at learning the language anyway but the issues I discussed earlier still just gave me that little bit more of a push.

So I'm going to keep on pushing with Java, lets see how it goes.

Tuesday, 30 June 2009

Contrary to popular belief...

Well it's been over a month since my last post so I thought I'd better just put up a quick note to say that I am still here and that I haven't forgotten the blog. Unfortunately lately my better half decided that maybe we should spend some time doing the gardening so instead of investigating version control systems, playing with Mono and Python and attempting to master (or at least comprehend) C++ I've been having my arms gauged open by plants which have decided that they're not going without a fight!

I have had a brief amount of time to have a look at Bazaar and will hopefully be able to put something up about that soon, it's looking good so far and is making me wonder why we're still using cvs at work. I've also had a quick look at developing apps for the Android mobile platform seeing as I now have an Android phone, I stopped when it looked like I was going to have to invest some time with Java as I didn't want to get carried away with another language, however since then I've discovered something called ASE, the Android Scripting Environment which looks as though it allows you to write apps for Android in Python - something which is of real interest to me - so I may have to spend some time looking into that.

Anyway, keep watching for more posts shortly (honest)

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.

Wednesday, 14 January 2009

So as promised I finally got around to putting up the code I spoke about in my last post, unfortunately after an initial attempt to post it here and a noticeable FAIL I decided to post it up to a new Google Code project called monoalgorithm, so now I have a project I can share and build up a collection of algorithms in :)

Anyway, it's under a new style BSD license and compiles and runs under Mono 1.9.1 so feel free to have a look and modify if you want. Of course now I have to learn how to use subversion as well as learning C++, Python etc... Obviously I must really like pain.

Monday, 12 January 2009


I never realised the finished formatting for a simple bit of HTML would look so bad in Blogger! I'll try and do something about that when I publish the full code tomorrow and in future posts.

Back over to .Net for a bit

Never completely happy with sticking to one language for any longer than a month (I get way to distracted) I've been playing around with C# over the last couple of weeks, specifically generics and lambda expressions with use in algorithms. The reason for this is as per usual something I came across on Project Euler, as I've done with all other problems I implemented the solution in Python - and I'm not likely to change that any time soon - but the initial solution came from C++ and the standard template library.

The problem involved determining lexicographical permutations which is a fairly common task when you want to test code. The reason for this is if you want to test a method with a range of values you can produce permutations of an original list and pass in each one, lexicographical simply means they are in order. For example, the list [0, 1, 2] can be arranged in the following permutations:

0 1 2
0 2 1
1 0 2
1 2 0
2 0 1
2 1 0

Initially I struggled to come up with a solution to this spending ages trying to figure out how to determine the next permutation in order. The answer came from the elegantly titled "Algorithm L". To summarise what it does I'll try and show an example in action, so first we start with our initial permutation.

1 2 3 4 5

First we start from the second to last index (in this case index 3 because indexes are zero based and the number there is 4) and work our way backwards until we find an index where it's value is less than the one after it which in this case is the same index we started at.


So we assign this index as j (j = 3), next we start from the last index and work backwards to j until we find an index whose value is larger than the one at j, in this case it's nice and easy because it's the last index, and we set that index as m. If we had not found a value for j then there would be no further permutations.


After we have found j and m we swap the two values at the indexes and then sort all the values after j, so in this instance we end up with:

1 2 3 5 4

If we repeated the process again from this permutation we would do something like this:





Implementing this in Python was easy but I wanted to play with Generics in C# so I decided to implement it in that language.

Generics are a way in which we can write a class to work on different data types without implementing the code with 1 certain type, for instance if we create a method that sorted arrays we could write something like:

public void Sort(int[] arrayOfValues) { ... }

Which would work great but if we then wanted to sort an array of doubles or characters we would have to re-implement the method for different types so we end up with SortInt(), SortDouble() and so forth. Whilst this may be okay for a couple of types if we wanted to sort other types and custom types then re-writing this code becomes a lot of work, makes maintenance harder and introduces a greater chance of error. This is where generics comes in, instead of the method signature used above we can instead write it as follows:

public void Sort(T[] arrayOfValues) { ... }

When we call this method we then define the type of data we are working like so:

int[] myIntArray = new int[] { 1, 2, 3, 4 };
char[] myCharArray = new char[] { 'a', 'b', 'c', 'd' };

As you can see the method call is identical except that we are specifying the data type we intend to work with, the magic part here is the <T> after the method name, this indicates that we are using a generic type called T and we can use it in the method like we would a normal type.

public void PrintValues(T[] arrayOfValues)
foreach (T value in arrayOfValues)
Now one problem we will encounter with the Sort() method we described above is that we cannot compare generic types as we can't guarantee that they will be comparable, to get around this we add a constraint to the method like so:

public void Sort(T[] arrayOfValues) where T: IComparable { ... }

This instructs the system to only accept types which implement IComparable, with this guarantee in place we can now compare values in the method:

public void Sort(T[] arrayOfValues) where T: IComparable
if (arrayOfValues[a].CompareTo(arrayOfValues[b]) > 0)
T holder = arrayOfValues[a];
arrayOfValues[a] = arrayOfValues[b];
arrayOfValues[b] = holder;

Using this we can implement our next_permutation algorithm in C# and enable it to work with any types (restricted to comparable types of course) we choose. Unfortunately I don't have the code to hand at the moment so I'll post it up for all to see and criticise (be kind please) tomorrow.

This has taken up quite a lot of space for now so I'll save the post on Linq for another day, but not to long away I promise as I want to post about it before I go for the next big C++ push.