The aim of this post is to gather all the golfing tips that can be easily applied to <all languages>
rather than a specific one.
Only post answers that its logic can be applied to the majority of the languages
Please, one tip per answer
You can usually merge two consequent loops, or two nested loops, into one.
Before:
for (i=0; i<a; i++) foo();
for (i=0; i<b; i++) bar();
After:
for (i=0; i<a+b; i++) i<a?foo():bar();
foo
is called a
times, bar
is called b
times. This is because in "after", the loop runs a+b
times, the first a
call foo
, the next ones call bar
.
\$\endgroup\$
for(y=0;y<Y;++y)for(x=0;x<X;++x)
can often become for(i=0;i<X*Y;++i)
with x
replaced by i%X
and y
replaced by i/X
.
\$\endgroup\$
Just to mention the obvious:
When golfing (especially harder problems that result in longer programs) all too often you might stick to the path you first chosen without trying other fundamental options. Of course, you may micro-golf one or a few lines at a time or a part of the overall idea, but often not try a totally different solution.
This was especially noticeable in Hitting 495 (Kaprekar) where deviating from the actual algorithm and looking for patterns you can apply to get to the same result was shorter in many languages (just not J).
The downside is that you possibly solve the same thing half a dozen times. But it works in really all languages except HQ9+ (where finding another way to output Hello World would be slightly futile).
If the code must handle various inputs, then write comprehensive tests and make it easy to run them all very quickly. This allows you to try risky transforms one baby step at a time. Golfing then becomes like refactoring with perverse intent.
For example, if A
and B
are booleans and your language treats booleans like numbers to some extent, A and (not B)
and A>B
are equivalent. For example in Python
if A and not B:
foo()
is the same as:
if A>B:
foo()
Mind that more care is required if A
and B
simply behave similar to booleans but are of another type. Also, in many situations you can optimise this further, but that’s beyond the point of this tip.
B>A or foo()
would be an even shorter way to express this, take advantage of lazy evaluation of boolean expressions to ensure it only calculates things when it needs to.
\$\endgroup\$
B>A or foo
would evaluate foo
if B==A
which is not what we want. (Right?)
\$\endgroup\$
Writing an explanation forces you to thoroughly look at each part of your code again and to make your thoughts and choices in writing a certain passage explicit. In doing so, you might find that different approaches are possible which may save some bytes, or that you subconsciously made assumptions which don't necessarily hold.
This tip is similar to Question your choice of algorithm and try something entirely new; however, I have found that the step of actually writing down how each part is supposed to work is sometimes crucial for becoming aware of alternatives.
As a bonus, answers including an explanation are more interesting for other users and are hence more likely to be upvoted.
Instead of x=1
, try to look for something that already equals 1.
For example, a function's return value: printf("..");x=0;
-> x=!printf("..");
.
It's easiest with 0, because you can always negate, or when all you need is the right truth value (and don't care if it's 1 or 19).
~
for x+1
and x-1
This trick applies to languages that have a unary bitwise negation operator ~
and a unary regular negation operator -
.
If your program, by chance, contains the expression -x-1
, you can replace it with ~x
to save bytes. This doesn't occur all too often, but watch what happens if we negate (-
) both expressions: x+1
equals -~x
! Similarly, x-1
equals ~-x
. (Think of which way the tilde points: right is +
, left is -
.)
This is useful, because in all languages I can think of that have these operators, they have higher precedence than most operators. This allows you to save on parentheses. Watch how we save four bytes here:
(x+1)*(y-1) ==> -~x*~-y
Know the rules for whitespace in your language. Some punctuation marks, or other characters, might not need any surrounding whitespace. Consider this Bourne shell function:
f () { echo a; echo b; }
In Bourne shell, ();
are metacharacters, and do not need surrounding whitespace. However, {}
are words and need whitespace unless they are next to metacharacters. We can golf away 4 spaces next to ();
, but must keep the space between {
and echo
.
f(){ echo a;echo b;}
In Common Lisp and PicoLisp, ()
are metacharacters. Consider this code to find the average of two numbers:
(/ (+ a b) 2)
We can golf away 2 spaces.
(/(+ a b)2)
Some languages have strange and subtle rules for whitespace. Consider this Ruby program, which prints the sum and product of a line of integers.
#!ruby -an
i=$F.map &:to_i
puts"#{i.reduce &:+} #{i.reduce &:*}"
Each &
needs a space before itself. In Ruby, i=$F.map &:to_i
means i=$F.map(&:to_i)
where &
passes a block parameter. But, i=$F.map&:to_i
means i=$F.map.&(:to_i)
where &
is a binary operator.
This weirdness happens in languages, like Perl or Ruby, that use ambiguous punctuation. If in doubt, use a REPL or write short programs to test the whitespace rules.
x = SomeLongFunctionName
x(somedata)
x(somemoredata)
etc
x
.
\$\endgroup\$
Commented
Mar 25, 2019 at 18:40
You have 52 of them; use them all! Don't be afraid to try different approaches and compare lengths. Know the language and the specific shortcuts/library functions available.
$
and _
can be used as identifiers.
\$\endgroup\$
@
is a valid variable name in T-SQL, use it instead of @a
.
\$\endgroup\$
A conditional operator
bool ? condition_true : condition_false
is more beneficial, character wise, than an IF statement.
if(a>b){r=a;}else{r=b;}
can be written as
r=a>b?a:b;
a&&b||c
instead. Slightly longer, but still shorter than an if
.
\$\endgroup\$
Commented
Mar 28, 2012 at 9:43
Iff
, although it's a function, so subject to evaluation of all arguments.
\$\endgroup\$
a&&b||c
can return c
when a
is true iff b
is false, a little edge case, but we shouldn't forget that ^^
\$\endgroup\$
a == 1 && b == 0
, then both b
and c
will be evaluated. Here is the expression, with parens: ((a) && (b)) || (c)
. a&&b
's result will be 0
in this case, so c
will be evaluated, because the ||
gate will check for the second operand if the first one is 0
.
\$\endgroup\$
Commented
Sep 11, 2016 at 9:47
-
instead of !=
for numeric comparisons:
If a equals b, a-b
results in 0
, which is falsy. Anything else than 0
is truthy; so
if used in a boolean context, a-b
<=> a!=b
If you use it with if/else
or with the ternary operator, this can also save you one byte for equality:
a==b?c:d
<=> a-b?d:c
Code golfing is as much about understanding the question (what is asked and what is not asked, even though it would be implied in any other setting) as producing code that (may) only satisfy what is asked.
Any input other than what is explicitly asked for need not be handled. If there are some test cases and no generic requirement, your code may only work in those cases. Etc.
e.g. if the question says "print the prime numbers from 1 to 100 inclusive", the largest prime printed will be 97 and you can change your loop end condition from 100
to 97
and save 1 byte. The question does not say you need to test 98, 99, 100 for primality, so don't do that.
e.g. 2. if the question says "print these numbers" then it does not say your answer must calculate the numbers. It might be shorter to store the expected output and decode and print it, than to write a calculator for it, e.g. storing 97 98 99 100 as a string of ASCII characters 'abcd'
.
Sounds like a no-brainer, but by being careful you might be able to "save" a few characters by not actually doing anything!
If you're using Windows, you may be inputting \r\n
instead of just \r
or \n
when you hit Return - adding an extra byte per line! Turn control characters just to double check you're not doing this.
In Notepad++ you can convert all \r\n
line endings to just \r
by going to Edit > EOL Conversion > UNIX/OSX Format
.
Also make sure you don't include any trailing whitespace in your character count! The line feed on the bottom line in your code is also inconsequential, so that won't need to be counted either.
When checking against hard-coded integer values, use >
and <
instead of >=
and <=
where possible. For example, using
if(x>24&&x<51)
Is 2 bytes shorter than using
if(x>=25&&x<=50)
<1
instead of ==0
as zero-check (or >0
instead of !=0
for the mirrored check).
\$\endgroup\$
Commented
Jun 21, 2016 at 11:28
x
being an integer?
\$\endgroup\$
Might be a bit of an edge case, but it could come in handy sometimes. It relies on the fact that all numbers to which m=2n-1 applies have the rightmost n bits set to 1.
So, 710 == 000001112, 1510 == 000011112, 3110 == 000111112 and so on.
The trick is x&~m
. This will return true whenever x
is not between 0 and m
(inclusive), and false otherwise. It saves 6 bytes from the next shortest equivalent expression: x>=0&&x<=m
, but obviously only works when m
satisfies 2n-1.
In addition to being fun, if you examine other people's code, you can sometimes discover a good algorithm that you didn't think about, or a trick (sometimes an obvious one) that you overlook.
Sometimes there is an existing answer that you can translate to another language, and benefit from the other language's goodies.
Greater/Less than to save a digit:
//use:
if(n>9){A}else{B}
//instead of:
if(n<10){B}else{A}
Just remember to swap the code from if
to the else
and they will do exactly the same thing (or switch the sides of the inequality)!
Note: this can be applied with any power of 10 and their negatives: ...-100, -10, 10, 100...
if(n>99999)
vs if(n<1e5)
\$\endgroup\$
Reuse function parameters instead of new variables
main(i){...
you now have a variable with the value of 1 without having to do any assignments. 2 chars saved there..
\$\endgroup\$
If running through a loop to check for 1 or more instances of a boolean check, it might make for a more efficient program to exit the loop on the first true value. However, removing the break and looping through all iterations allows for shorter code.
int main() {
bool m = false;
int n = 1000;
for (int i = 0; i < n; i++) {
if (i >= 100) {
m = true;
break; // remove this line
}
}
return 0;
}
if
statement away in these cases: m|=i>=100
. (And you can also simplify the i>=100
to i>99
in this case but that's not very relevant here)
\$\endgroup\$
~
for a-b-1
and a+b+1
In addition to @Lynn's suggestions regarding x+1
→ -~x
; and x-1
→ ~-x
, you can also golf a-b-1
and a+b+1
.
a-b-1 // 5 bytes
a+~b // 4 bytes
a+b+1 // 5 bytes
a-~b // 4 bytes
It might look like a tip you won't use all that often, kinda like using ~x
instead of -x-1
doesn't happen often, but I've used it enough times to see it as a useful tip here. Especially with array-indexing you might use these above in some cases.
Whenever You combine several expressions, check the operator precedence table for your language to see if you can reorder stuff to save parentheses.
Examples:
(a&b)&&c
needs no parentheses: a&b&&c
just as (a*b)+c
does not.a+(b<<c)
can be rewritten as a+b*2**c
.c
is a small integer literal (<14).a<b&&c<d
with a<b&c<d
(unless you need the short circuit evaluation)Most languages have a way to split a string into an array of strings around a token of some kind. This will inevitably be shorter than an array literal once the length reaches a language-dependent threshold, because the extra overhead per string will be one copy of a one-char token rather than (at least) two string delimiters.
E.g. in GolfScript
["Foo""Bar""Baz""Quux"] # 23 chars
becomes
"Foo
Bar
Baz
Quux"n/ # 20 chars
For some languages, the threshold is as low as one string. E.g. in Java,
new String[]{"Foo"} // 19 chars
becomes
"Foo".split("~") // 16 chars
%w{Foo Bar Baz Quux}
.
\$\endgroup\$
Commented
Dec 21, 2014 at 11:39
qw(Foo Bar Baz Quux)
becomes a list of strings.
\$\endgroup\$
Commented
Dec 16, 2016 at 1:17
qw
, using barewords that aren't existing functions as strings, or using glob operations, either of which saves even the qw
, e.g. (Foo,Bar,Baz,Quux)
or <Foo Bar Baz Quux>
\$\endgroup\$
Commented
Mar 16, 2023 at 22:15
If you have X
statements {
inside}
your for-loop, you can move X-1
statements (
inside)
the for-loop after the second semicolon for(blah;blah;HERE)
to save 3 bytes. (separate the statements by using a comma ,
)
Instead of
for(int i=0;i<9;){s+=s.length();println(i++);}
you can move one of the statements into the for-loop's (
braces)
while leaving the other out
for(int i=0;i<9;println(i++))s+=s.length();
and save 3 bytes (saved 1 more byte thanks to @ETHProductions)
Put simply,
instead of
for(blah;blah;){blah 1;blah 2;...;blah X}
move the statements around so you end up with this
for(blah;blah;blah 2,...,blah X)blah 1;
and save 3 bytes
for
is the final statement, the ;
becomes optional
\$\endgroup\$
Commented
Mar 25, 2019 at 18:46
You spend your time looking to get rid of O(n^2) algorithms and bring runtime and memory use down; golf turns that around - spend freely of CPU and Memory to bring code size down. Use nested loops, calculate 10x too much data instead of a complex exit condition, call sort()
every loop iteration instead of storing the result.
Some questions have a runtime limit to push you to write a more efficient answer, even then knowing how fast your language and code runs can give you room to take liberties and still stay within it.
Maybe somewhat obvious but...
Keep in mind that the assignment operator returns a value!
For example, if you want to add y to x and then check if x is greater than something, you can do
if(25<x+=y)
instead of
x+=y;if(x>25)
Or maybe you want to find the length of a string after trimming it:
strlen(s=trim(s))
Rather than
s=trim(s);strlen(s)
a = (b=c)+1;
sets b
to c
, and then sets a
to b+1
.
\$\endgroup\$
=
operator a higher precedence on the left than the right, so 1+x=2
is valid and evaluates to 3
\$\endgroup\$
Be sure to know which optimisations are guaranteed by the compiler and at which optimisation levels, and use them liberally. And even if performance isn't a concern requirement, you can still test with optimisations on, and then only discount one character because your code is still technically valid without the compiler flag.
Consider the following Haskell function to compute 2^n (ignoring the fact that Haskell already has a built-in exponentiation operator or three) (23 characters):
p 0=1;p x=p(x-1)+p(x-1)
The problem is - it's horrendously slow, it runs in exponential time. This might make your code untestable or to fail any performance constraints given by the question. You might be tempted to use a temporary variable or an immediately invoked function literal to avoid repeated function calls (25 characters):
p 0=1;p x=(\y->y+y)$p$x-1
But the compiler can already do that for you, you just need set -O
as a compiler flag! Instead of spending few extra characters per site to eliminate common subexpressions manually, just tell the compiler to do basic optimisations for you.
p(x-1)*2
?
\$\endgroup\$
Many questions on codegolf.SE ask you to "write a program or function"; this gives you some freedom to play to the strengths of your language and runtime based on how it handles command line parameters, how much boilerplate it needs for defining functions and their parameters, for defining full programs, for reading user input in a running program.
e.g. for a question asking you to write a program or function which takes a number as input, multiplies it by 10 and prints the result:
in Python it might go this way:
print(10*int(input())) # full program
lambda x:print(10*x) # function
in APL, the other way:
⎕←10×⎕ # full program
{⎕←10×⍵} # function
In languages with a lot of code to get a whole program to run such as Java, a function could be much shorter. In more dynamic scripting languages such as Perl with many command line options to change how the code acts, a full program with a command line switch could be the shortest. Taking input from an implicit $args
might be shorter than input()
, or defining a function with a parameter might be shorter than System.Console.ReadLine();
Simple trick I've come up with when trying to squeeze a long streak of conditions chained by ands (or ors, in this case just substitute 'all' with 'any').
Eg:
if a>0 and a<10 and a+b==4 and a+3<1:
Becomes
if all([a>0,a<10,a+b==4,a+3<1]):
all(array-of-Booleans)
built-in?
\$\endgroup\$
Commented
Sep 11, 2014 at 13:50
[a>0,a<10,a+b==4,a+3<1].all?
\$\endgroup\$
if 10>a>0 and a+b==4>1>a+3:
\$\endgroup\$
if 10>a>0>a+2 and a+b==4:
because a+3<1
is a+2<0
. which on second thought, isn't possible, so if 0:
. XP
\$\endgroup\$
Commented
Jul 28, 2015 at 23:40
This is especially useful for polyglots, but can be applied to other challenges. Sometimes, a compiler bug can golf off a byte, a implementation bug can allow you to save a few chars, or a really bleeding-edge feature can improve your score.
<all languages>
... \$\endgroup\$