# Tips for golfing in Delphi

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;
begin
for x:=0to 99do
if i=1then
inc(i)//Semicolon not allowed because of "else"
else
i:=i+3//No semicolon required
end;//Semicolon required


### Example 2

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

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

• Actually i:=i+3 is shorter then inc(i,3). – manatwork Feb 11 '14 at 11:37
• Touche ;-) i usually do that lol, dont know why I didnt do it now. Ill edit that example. – Teun Pronk Feb 11 '14 at 11:38
• Usually const can automatically resolve types, in your case :string is not required. – mniip Feb 11 '14 at 11:48
• @mniip try putting it as an answer, that way I can upvote it and youll actually gain some rep with it ;-) – Teun Pronk Feb 11 '14 at 11:51
• Honestly I believe you picked a wrong language for golfing :P – mniip Feb 11 '14 at 12:26

### 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;
begin
F:=1
end;


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

function F:Int8;
begin
Exit(1)
end;


### 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
begin
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 ...;