4
\$\begingroup\$

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;
cat
6,0502 gold badges27 silver badges45 bronze badges
asked Feb 11, 2014 at 11:31
\$\endgroup\$
9
  • 1
    \$\begingroup\$ Actually i:=i+3 is shorter then inc(i,3). \$\endgroup\$ Commented 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\$ Commented Feb 11, 2014 at 11:38
  • 1
    \$\begingroup\$ Usually const can automatically resolve types, in your case :string is not required. \$\endgroup\$ Commented Feb 11, 2014 at 11:48
  • 2
    \$\begingroup\$ Honestly I believe you picked a wrong language for golfing :P \$\endgroup\$ Commented 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\$ Commented Feb 11, 2014 at 12:49

1 Answer 1

3
\$\begingroup\$

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 ...;
Wheat Wizard
103k23 gold badges299 silver badges697 bronze badges
answered Mar 18, 2014 at 19:14
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.