Programming in C#, Java, and god knows what not

Version Control

Over time I played with quite a few Version control systems. Here I will explain why I hate each one. :)

WinRAR

My first source control consisted of nothing more than compressing project’s files in different archives. It became one source control system by which I will judge every other. It was probably best $35 that I ever spent on any software.

Microsoft SourceSafe

First one that I ever saw was Source Safe. I kind of liked it but this was time of simpler (Visual Basic 6.0) projects. I never used it at home since it was simply too expensive for me.

IBM ClearCase

First real contact with version control was in form of ClearCase. To put it in context, it felt like I was little kid getting introduced to gravity via drop from 1000 meters (more than 3200 feet for those SI-challenged).

We had one guy in company who’s sole responsibility was handling views and other black magic. After some time I did became a sort of expert for client side installations and I sorted more problems with it than I care to remember. I figured that version control needs to be complicated on both client and server level.

Not to be misunderstood, in those rare occasions when moon and sun were in just right position and as long as you sticked to Unix command line instead of Windows client, it did work as expected.

SourceGear Vault

As I was playing with ClearCase at work, I started to desire something for my personal projects also. Answer was in SourceGear’s Vault. It was in-place replacement for SourceSafe and it was free for single developer.

Trouble with it was that I just got too used to ClearCase and it’s control of everything in directory. With Vault I had only source under control. Documents, installations and other files that my projects contained were still under mercy of WinRAR.

Microsoft Team Foundation Server 2005

After I parted my ways with Vault, I missed source control. I missed it enough to install Team Foundation Server. Installation was made in hell. It had bunch of requirements and each requirement had it’s own requirement. It was mission impossible. I did install it at least but I decided that cost and pains that it puts me through are just not worth it. I never actually used it’s source control. And I never will.

SVN

Since ClearCase was getting too expensive and too painful to maintain, my team switched to SVN. It took some time to get used to work without checkouts but we mostly managed it. Since we had something like 20 people working for first time with same repository, we probably did every single thing you should not do.

Experience with it was actually good most of times. Although, I would like to kill idiot who decided that “.svn” folder in EVERY directory is appropriate way of storing information…

Microsoft Team Foundation Server 2010

I simply could not believe that same team did both 2005 and 2010 edition. Those two products could not be more different. Installation of Team Foundation Server 2010 went without issues. If I forget whole issue with Visual Studio 2008, whole product just worked.

Files outside solution were issue - it was annoyingly hard to track any file that appeared outside of project. As I started to work with stuff that isn’t in Visual Studio 2010, two of us just got parted. I still miss integrated work item handling.

If I ever went back to Microsoft-only world, I would use this.

Mercurial

I have no idea how I came to try Mercurial but I am glad I did. It uses file based management (like SVN) but “.hg” folder is only place where it stores everything of it’s own. Installation is dead simple, usage even better (TortoiseHg). It has plenty of rough edges but it feels good so far.

I love the most simplicity of administration and low system requirements. My home server has total of 256 MB and Mercurial can work on it.

I used it only for couple of months now (and that is actually less than any other system excluding TFS 2005) but I feel like it was made for me. It is my current choice. Will it remain, only time will tell.

And it is only source control system where backup can be done as easy as with WinRAR. Actually, I do it with WinRAR… :)

P.S. This text represents nothing else than my personal and highly subjective views. As it comes, I do care a lot about them and I consider everything written here to be absolutely correct. :)

P.P.S. Yes, I know that WinRAR was not intended to be version control system.

Merging SQL Server Tables

In one of my existing databases I had to switch from integer key to GUID. Annoying aspect of it is handling referential integrity issues and, as soon as there are two tables, you can bet that there will be issues.

Process was half manual and simple one: just create new field (e.g. Guid) and give it default of NEWID. Database itself will do the rest for that field’s value.

In order to sort out references, you can go in similar fashion. For every foreign key field just create one with GUID (E.g. ItemId gets it’s friend ItemGuid). After that just synchronize fields with simple SQL:

UPDATE Codes
SET ItemGuid = (
    SELECT Guid
    FROM Items
    WHERE Items.Id = Codes.ItemId
)

In this case, we had referential link between Codes.ItemId and Items.Id. Our goal was to switch that to Codes.ItemGuid and Items.Guid. Where statement ensured just that.

Async and Await

On Microsoft’s PDC Anders Hejlsberg gave a talk about two new keywords in C# - async and await.

I will not get into them to much - those interested in more can start with taking a look at actual presentation. In essence, they give you new way of dealing with asynchronous tasks. You just point to system where asynchronous operation might occur and C# (or VB.NET) compiler will build all background code that it needs to handle this gracefully. For me it does same revolutionary thing as yield did for enumeration. Seeing this, I got pissed at Java.

Part of my time I spend as Java developer. And I get pissed at it all the time. Whether it is half-assed implementation of generics, absence of in-language support for yield, and in a year-or-so, I will extend this to absence of async and await.

True Java believer would say that all those things are just syntactic sugar - there is nothing magic in them that could not be written by hand. I consider this irrelevant. It is not point whether something can be written, to me most important is point how easily. If I use e.g. yield, there is almost no chance I will mess it up in three lines it takes to do it. When I write code for it in Java, this expands to few tenths of lines needed for state machine. Error chance and debugging time increases exponentially.

I value Java a lot. It is beautiful language at it’s core and it gave huge boost to development of all managed languages. However, it seems as Latin language to me. Nice and beautiful but there is just no significant development of it’s syntax. It takes more and more effort for me to switch between new modern languages (where I would include C#) and Java. I always find something missing…

P.S. Yes, this post is full of exaggeration, but I do not consider it too much off mark.

Infinity Is Not an Exception

From time to time I find some behavior that I cannot really neither explain as correct nor as incorrect. Best description would be peculiar.

Let’s take simple code:

static void Main() {
    int x = (int)double.PositiveInfinity;
    Debug.WriteLine(x);
}

This will cause compile error “Constant value ‘1.#INF’ cannot be converted to a ‘int’ (use ‘unchecked’ syntax to override)” and personally I view this as correct behavior.

Let’s complicate things a little:

static void Main() {
    double posinf = double.PositiveInfinity;
    double neginf = double.NegativeInfinity;
    int x = (int)posinf;
    int y = (int)neginf;
    Debug.WriteLine(x);
    Debug.WriteLine(y);
}

Here I expected one nice runtime exception. However, I was greeted with -2147483648 as a result for both positive and negative infinity. This I did not expect.

My personal opinion here is that this operation should throw exception. I cannot see any sound reasoning for converting infinity to any finite number. It is called infinity for a reason!

However, I do notice that most of languages choose to have this conversion pass. Unfortunately for C# they (e.g. Java) opted for slightly different behavior.

Java converts negative infinity in same manner C# does but positive infinity gets converted to 2147483647. This may not seem like much, but this at least enables positive infinity to be larger than zero which seems mathematically sound to me (if we ignore all that infinity thing :)).

My personal opinion here is that exception should be thrown. Only thing that this conversion can lead to is data corruption - and this is not a good thing.

P.S. I reported this as an issue to Microsoft. I am really interested how they view this situation.

[2010-12-30: I got answer. It is by design.]

LogCat Does Not Show Log Messages

LogCat is quite good thing to look at when program goes haywire since it usually shows both system and developer’s own log messages.

If you are trying to debug your Android program inside of Eclipse and you cannot see your custom log message, fixing it might be as simple as going to DDMS perspective and selecting your current device (whether real or simulated).

LogCat window displays only messages from device in focus and that might not be device you are currently debugging.

Visual Studio 2010 Patches

I consider Visual Studio 2010 an improvement to Visual Studio 2008. However, it had few annoying issues.

One was making find box wider and wider and that was solved few weeks ago via patch.

Another one was need to scroll context menu although there was enough place on screen. Finally patch for that issue is here. For this patch to work properly you also need another WPF patch.

This is quite a collection of patches - 1, 2 and 3. Once these are installed Visual Studio 2010 suddenly becomes even better environment.

How to Gently Kill a Thread

Creating your own thread is not something that I do lightly. There are so many alternatives these days where framework does “dirty job” for you.

However, sometime making your own thread is way to go. I found that mostly I use same code in order to cancel it.

Idea is creating ManualResetEvent that we can check fairly quick from thread loop. Once that event switches it’s value to true, our thread should terminate.

private ManualResetEvent _cancelEvent;
private Thread _thread;

public void Start() {
    _cancelEvent = new ManualResetEvent(false);
    _thread = new Thread(Run);
    _thread.IsBackground = true;
    _thread.Name = "EntranceBarrier";
    _thread.Priority = ThreadPriority.AboveNormal;
    _thread.Start();
}

public void Stop() {
    _cancelEvent.Set();
    while (_thread.IsAlive) { Thread.Sleep(10); } //wait until it is really stopped
}

bool IsCanceled {
    get { return _cancelEvent.WaitOne(0, false); }
}

void Run() {
    while (!IsCanceled) {
        //some code
        if (this.IsCanceled) { return; }
        //some code
    }
}

P.S. This will terminate thread gently and it only works if event is checked often. If you have code that is waiting for system event, this is not solution for you.

GPS Should Stop

One error that creeps quite often in Android applications is not handling being in background gracefully. It is error that is really easy to make since your application works as it normally would. However, it does use more resources than it is needed and resources usually map to battery life.

One example of bad behavior that comes to mind is updating location non-stop. Most often application needs to update their location only when user is looking at it. Once application is in background GPS should be turned off (although there are some application that have good reason to leave it going).

Easiest way to do it is just handling all GPS “subscriptions” in onResume and onPause methods. Those methods get called each time application comes to foreground (onResume) or leaves it (onPause).

Generic solution would look something like this:

private LocationManager thisLocationManager;

@Override
public void onCreate(Bundle savedInstanceState) {
   ...
   thisLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
}

@Override
protected void onResume() {
    thisLocationManager.requestLocationUpdates("gps", 0, 0, gpsLocationListener);
    super.onResume();
}

@Override
protected void onPause() {
    thisLocationManager.removeUpdates(gpsLocationListener);
    super.onPause();
}

private LocationListener gpsLocationListener = new LocationListener() {
   ...
};

P.S. Do note that this code is not optimal in any way (e.g. hard-coding “gps” as provider name). Intention is just to illustrate concept.

Getting GUID Value After SQL Insert

Whoever used identity columns in SQL Server probably used SCOPE_IDENTITY in order to retrieve newest identity value after insert.

For example, if we have two columns, one named Id and declared as identity column and other named Test with text inside, we would use something like this in order to insert new row:

INSERT INTO Example(Test) VALUES('test'); SELECT SCOPE_IDENTITY();

Once we use ExecuteScalar, we would get newly inserted identity value (in column Id).

Once we enter world of Guids, we cannot do that. Guid is not considered identity value and same rules do not apply to it. In order to have similar example we shall have also two columns here. However, this time Id column will be of uniqueidentifier type and it will have it’s default set to NEWID(). This way client behavior is same. Once we insert new row (without specifying Id value) we will get Id assigned to us.

In order to retrieve value for Id column, we need to change SQL a little:

INSERT INTO Example(Test) OUTPUT INSERTED.Id VALUES('test');

Visual Basic and Windows Phone 7

One thing that C# could do and Visual Basic could not was development for Windows Phone 7. I will not get too much into whether this is even an issue, but I will notice that it is no longer true.

Visual Basic developers can now download Microsoft Visual Basic CTP for Windows® Phone Developer Tools.

CTP stands for Community Technical Preview and it is closest to something in alpha stage. It will not be usable for production environment and it will take a while for final version to come. However, it is a big step forward.