Code-golfing is incredibly fun to do, trying to solve the given problem is one thing but the real challenge is getting your code as short as possible.

Some languages are perfect for golfing because of their short commands, sadly Delphi isn't one of them.

I'm looking for tips that are useful for golfing in Delphi.
Tips like "Use 1 letter variables" don't really count in my opinion since that is pretty obvious.

Tips I learned working with Delphi or found in other peoples answers so far are:

  • Semicolon isn't always required (Example 1)
  • Use int8 or int16 as datatype rather than integer (saves 3 or 2 characters)
  • Combine string manipulations(Example 2)

Example 1:

Procedure Example1(i:int16);
var x:int16;
  for x:=0to 99do
    if i=1then
      inc(i)//Semicolon not allowed because of "else"
     i:=i+3//No semicolon required 
end;//Semicolon required

Example 2

function WrongExample2(myString:string):string;
  f:string='The result of your manipulation is: %s';
  myString:= StringReplace(s,'x', '_', [RfReplaceAll]);
  Result := Format(f,[myString])

function CorrectExample2(myString:string):string;
  f:string='The result of your manipulation is: %s';
  Result := Format(f,[StringReplace(s,'x', '_', [RfReplaceAll])])
  • 1
    \$\begingroup\$ Actually i:=i+3 is shorter then inc(i,3). \$\endgroup\$
    – manatwork
    Feb 11, 2014 at 11:37
  • \$\begingroup\$ Touche ;-) i usually do that lol, dont know why I didnt do it now. Ill edit that example. \$\endgroup\$
    – Teun Pronk
    Feb 11, 2014 at 11:38
  • 1
    \$\begingroup\$ Usually const can automatically resolve types, in your case :string is not required. \$\endgroup\$
    – mniip
    Feb 11, 2014 at 11:48
  • 2
    \$\begingroup\$ Honestly I believe you picked a wrong language for golfing :P \$\endgroup\$
    – mniip
    Feb 11, 2014 at 12:26
  • 1
    \$\begingroup\$ @mniip well im pretty new to delphi and im affraid youre right, but this way I can have fun and still learn new things which is usefull for work ^.^ Besides that you hardly see delphi on here (it became obvious why lol) so its fun to use something different for a change :) \$\endgroup\$
    – Teun Pronk
    Feb 11, 2014 at 12:49

1 Answer 1


Group definitions by keyword

This will save you from having to retype the keyword.

type WD=(Su,M,Tu,W,Th,F,Sa);M=(Ja,F,Mr,Ap,My,Jn,Jl,Ag,S,O,N,D);
const A=1;B=2;
var X,Y,Z:string;

Use function name instead of Result for holding function return value

Functions' return value can be referenced by the function name. Unless the function is required to be a particular name it will usually be shorter than Result.

function F:Int8;

Using the Exit short hand (Delphi 2009 or newer) is shorter than assigning to Result also:

function F:Int8;

Use comma-separated variable declarations

procedure Z(A,B,C: string);
var A,B,C:string;

Constants usually require fewer characters than variable declarations

With the exception of the above mentioned comma separated variable declarations constant declarations use fewer characters than variable declarations when initialization is taken into consideration.

const A=1; // True constant = 10 chars
const A:Int8=1; // Typed constant = 15 chars
var A:Int8; // Variable = 11 chars
  A:=1; // + 5 chars for initialization = 16 chars

Use set data types and their operators

If ordering isn't important, then use sets instead of arrays or lists for more concise statements. Unfortunately you are limited to a maximum of 256 elements in a set

+  The union of two sets
*  The intersection of two sets
-  The difference of two sets
=  Tests for identical sets
<> Tests for non-identical sets
>= Is one set a subset of another
<= Is one set a superset of another
in Test for set membership of a single element

The in operator is particularly useful when testing if a variable equals any one of possible values. Silly example using in and .. (the range operator):

// expensive:
if ( A = 1 ) or ( A = 2 ) or ( A = 3 ) or ( A = 6 ) then ...;
if ( A > 0 ) and ( A < 4 ) or ( A = 6 ) then ...;

// better:
if A in [ 1 .. 3, 6 ] then ...;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.