Some time ago, Nick and Malcolm drew our attention on a site which promote an anti-if campaign. Well, I don’t know how much that campaign is interesting for us, because, sincerely, I don’t see any time soon the
if construction removed from programming languages.
But I will propose another campaign which I think is much more stringent for Delphi developers… It is anti
Case campaign. And to be very clear, is against Case structure in the form in which is implemented now.
The idea struck when, having my previous experience with Class Signatures, I saw Jolyon’s post about using of
absolute keyword. The first reaction was “Hey, my solution is better than this (safer – checks at compile time, much clearer code etc.) but it is a language improvement (requires support from the compiler)”. But because for us, mere mortals, this isn’t so easy to get, I tried to make Jolyon’s code cleaner. I was very frustrated (again) because in a perfect world, one should just write:
procedure Foo(aObject: TObject); begin case aObject.ClassType of TEdit: TMemo: TLabel: else //... end; end;
procedure Foo(aObject: TObject); begin case aObject of myEdit: myMemo: myLabel: else //... end; end;
In fact there are integers, no? (An ‘ordinal’ type…)
…and of course, let us just skip over the advantages of the above code compared with the spaghetti code generated by the
Pushing things further,
case, even if its very fast, nowadays is quite limited for the programming needs of a programmer in the winter of 2009 (yeah, I’m looking a little bit forward, but just a little, you know).
No strings, no objects, no classes. No floating-point, no records, no variants. No interfaces, no custom types, no nothing. Did I miss something?
In short, I propose to enhance the actual ‘Case’ structure to support every entity which has the
= (equal) operator defined.
Easy! Vote & Comment!