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
int8orint16as 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;
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
The function's 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 shorthand (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; // genuine constant = 10 characters
const A:Int8=1; // typed constant = 15 characters
var A:Int8; // variable = 11 characters
begin
A:=1; // +5 characters for initialization = 16 characters
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.
| operator symbol | operator description |
|---|---|
+ |
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 bounds separator):
// 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 ...;
i:=i+3is shorter theninc(i,3). \$\endgroup\$constcan automatically resolve types, in your case:stringis not required. \$\endgroup\$