Sunday, 24 May 2020

May the "Default" be with you

- or bit of housekeeping in some very old code.

Since we are still in the month of May - I though I could get away with that title - but this small post is not about calendar months or Star Wars.

I have not been very active here for a while - but I wanted to write a bit on the lack of use of StrToXDef, TryStrToX and overloaded constructors that now have been in the RTL for many years.

Just preparing an old Delphi project for the upcoming 10.4 release (real soon now) - and I stumbled upon some horrid old code like this:

S := Character.Properties[ 'Distance' ];
  if S = '' then
    iDistance := 175
    iDistance := StrToInt( S );
  iDistance := 175;

and this:

S := LowerCase( Character.Properties[ 'PlaySFXOther' ] );
  if S = '' then
    bPlaySFXOther := false
  else if S = 'true' then
    bPlaySFXOther := true
    bPlaySFXOther := False;
  bPlaySFXOther := false;

Which can in later versions be written like:

iDistance := StrToIntDef( Character.Properties[ 'Distance' ], 175 );

and this:

bPlaySFXOther := StrToBoolDef( Character.Properties[ 'PlaySFXOther' ], False );

The above code is from the Siege Of Avalon game - originally written in Delphi 4, and it illustrates that non-maintained code rots over time, and that one should also utilize "helper" functions that improves readability, or even better keep up with the language and RTL changes introduced in newer versions.

Another example is the TStringList constructor - originally you needed to do this for a sorted case-insensitive list that ignores duplicates:

  strListOld :TStringList;
  strListOld := TStringList.Create;
  strListOld.Duplicates := dupIgnore;
  strListOld.Sorted := True;
  strListOld.CaseSensitive := False;

compared to one of the overloaded constructors, and inline variables from 10.3 onward:

var strListNew: TStringList := TStringList.Create(dupIgnore, True, False);

Faster to write and a lot more readable. There are many example like this where either overloaded or class helpers makes code more writable and readable.

I have been given permission to talk about 10.4 - and having now worked a bit with it - it is great to be able to use code- and error insight together with the new language features from the last version without getting squiggly red lines - all now thanks to the async Delphi LSP process. The "insights" now see what the compiler sees.

The speed improvements in the IDE and generated code is also noticeable, and I look forward to refactor a lot more old code - so that it actually is a joy to work with. I am very pleased about where this is going.

Which finally brings me to the point of this post: Keep you code fresh and updated! This goes for all languages and applications.

I would rather work with "old" maintained and tested code - than getting frustrated with new and untested code.

The worst yet is unmaintained old code, which brings bad reputation to both the application and the language.

My fork of the Siege Of Avalon game engine can be found on

No comments:

Post a Comment