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 CPlusPlus.com 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.
Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts
Saturday, 15 August 2009
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.
http://code.google.com/p/monoalgorithm/source/browse/trunk/Algorithm.cs
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.
http://code.google.com/p/monoalgorithm/source/browse/trunk/Algorithm.cs
Monday, 12 January 2009
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:
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:
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.
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.
| Index | 0 | 1 | 2 | 3 | 4 |
| Value | 1 | 2 | 3 | 4 | 5 |
| ^ |
| Index | 0 | 1 | 2 | 3 | 4 |
| Value | 1 | 2 | 3 | 4 | 5 |
| ^ |
1 2 3 5 4
If we repeated the process again from this permutation we would do something like this:
| Index | 0 | 1 | 2 | 3 | 4 |
| Value | 1 | 2 | 3 | 5 | 4 |
| j | m |
| Index | 0 | 1 | 2 | 3 | 4 |
| Value | 1 | 2 | 4 | 5 | 3 |
| Index | 0 | 1 | 2 | 3 | 4 |
| Value | 1 | 2 | 4 | 3 | 5 |
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
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' };
Sort(myIntArray);
Sort(myCharArray);
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 PrintValuesNow 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:(T[] arrayOfValues)
{
foreach (T value in arrayOfValues)
{
Console.WriteLine(value);
}
}
public void Sort
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.
Subscribe to:
Posts (Atom)