Switching languages – Common mistakes

These days I generally work with C#, VB.Net, JavaScript and SQL. Switching between the different languages with their constructs has caught me a few times with subtle bugs. I thought I’d post a few simple little “gotchas” I’ve encountered.

VB.Net Nothing is not C# null

Most of my development experience is in C#, so this one was a little strange when I encountered it. Nothing is NOT the same as null. Nothing in VB.Net is actually the same as default(T) in C#.

'This is perfectly legal in VB.Net
Dim myGuid As Guid = Nothing
//Whilst this will not complie in C#
Guid myGuid = null;

// The actual conversion of the Nothing to C# is
Guid myGuid = default(Guid); // Which is the same as Guid.Empty

So here’s the gotcha that caught me:

Dim myGuid As Guid = Nothing
If myGuid = Nothing Then
'// This is True
End If
If IsNothing(myGuid) Then
'// This is False, careful!
End If

The first check = Nothing checks if myGuid = default(Guid), which it does. The one that caught me was IsNothing at first glance should do the same, but it doesn’t. That’s because IsNothing method is only intended for reference types. So IsNothing is probably the closest equivalent to the C# == null check

From MSDN:

MSDN: IsNothing is intended to work on reference types. A value type cannot hold a value of Nothing and reverts to its default value if you assign Nothing to it. If you supply a value type in Expression, IsNothing always returns False.

JavaScript boolean comparison

A simple gotcha that has unfortunately caught me more than once is a boolean comparison with a string value. Let’s say we read a hidden input field value which holds a boolean value and do a simple check on it:

var isValid = $("#hiddenValid").val();
if(isValid) {
   // ALWAYS TRUE, as this actually checks if isValid is defined

if(isValid == true) {
   // ALWAYS FALSE, as we're comparing string with bool

if(isValid == "true") {

The is actually a very obvious mistake as we’re comparing different types, but isn’t so easy to track down once it’s in code. Working mostly in C#, the first 2 methods look perfectly correct at first glance.

SQL Server Not Equals

A few months ago, whilst debugging a report, I stumbled on this one. Once again, it’s a relatively simple little gotchas, but not at all obvious to track down. Lets say we have an table called Employee with the following data:

We have 7 empoyees. Steve and Adam are new interns and will only officially get a position once their 3 month probation is over (until then their position is null).


We must produce a report listing all employees except the CEO. Sounds easy enough:

--This is wrong
SELECT * FROM Employee
WHERE Position <> 'CEO'

--This is right
SELECT * FROM Employee
WHERE (Position IS NULL OR Position <> 'CEO')

-- This is also right
SELECT * FROM Employee
WHERE ISNULL(Position, '') <> 'CEO'

The first SQL query looks perfectly fine. Get everyone where not equal to ‘CEO’. However since the <> in SQL actually checks greater than and less than and since NULL mean no value exists, we cannot do any type of comparison against a non-existant value, so it will ignore the NULL values.

This isn’t a SQL specific thing, it applies to all languages that have nullable types. But in C# for instance, a design decision was made to include null items in comparisons for ease of use (even though it’s semantically not really correct).

That’s it

Are there any simple gotchas or traps you’ve run into switching between languages? If so, please feel free to post in the comments.


One thought on “Switching languages – Common mistakes

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s