Quickie on programming equality tests

Just noticed this come up on my Facebook news feed – how to code assignment statements when programming (not in T-SQL):

  1. if (foo == 4) then …
  2. if (4 == foo) then …

#1 is the most natural way to write the conditional expression, but has a *massive* potential for introducing bugs that are very, very hard to find. If the second '=' is omitted, the conditional expression suddenly becomes an assignment statement and evaluates to true. Nasty.

#2 is the best way to avoid such problems, even though it seems unnatural. If the second '=' is omitted, it becomes an assignment statement but the compiler will barf, because you can't assign a value to a constant. (Some languages and data types may be immune to this – so this isn't a blanket statement – it's certainly a problem in C++.)

#1 was the cause of several bugs that took me a while to find while working on the Storage Engine code – all C++. I always use #2.

PS if (foo = 4) won't compile in C# if foo is an int, but you can come up with weird cases that do compile and break. Who uses managed code anyway?!? ;-) (ok, no hate mail please)

Photos: amazing wall paintings from the Imperial Palace in Bangkok

In February this year we stopped off in Bangkok on the way to and from teaching in Hyderabad. On one of the days we did some sight-seeing, including the Imperial Palace in Bangkok. I took a bunch of photos of the wall paintings in the outer courtyard and I've posted an album up on Facebook (easier than a big, bloated blog post here). You can get to the album at http://www.facebook.com/album.php?aid=2020899&id=1293146061&l=5c4c559fa5 (even if you're not on Facebook).

Here's an example:

Enjoy!

Weekly survey: large-value character storage

(And it's official – this is blog post 39 this month, making this my most prolific month yet for blogging. An arguably dubious achievement…)

In this week's survey, I'm interested in your views on the best way to store large-value character data. I'll report on the results next week (around 4/3/09).


Thanks!