June 2017

Why a "case <string> of" statement in Delphi is a dangerous idea

Over and over, people ask the case .. of statement to be expanded to allow strings, i.e.

case S of
  'String one': DoSomething;
  'String two': DoSomethingElse;
end;

Looks nice, doesn't it? Sure, but the devil is in the details. Strings are complex data types, and comparing strings is not a straightforward tasks. If you use just a single, simple encoding, and a simple language like English, a comparison may be as simple as comparing bytes. But there are different encodings, and there are many different languages.