80
\$\begingroup\$

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

\$\endgroup\$
  • 5
    \$\begingroup\$ "Majority" by what metric? \$\endgroup\$ – ceased to turn counterclockwis Mar 27 '12 at 10:41
  • 2
    \$\begingroup\$ @leftaroundabout by the metric of the same word \$\endgroup\$ – ajax333221 Mar 27 '12 at 22:01
  • 8
    \$\begingroup\$ The problem is that many languages are (often short-lived) experimental ones with very untypical paradigms, for which typical programming expressions don't make any sense at all. So "majority of all languages" it virtually impossible to fulfill. You should restrict it in some way, e.g. to "majority of languages regularly used on codegolf.SE". At the moment, the answers look quite a lot like "the majority of remotely C-derived languages", but those, albeit the vast majority of all written code is written in them, are not the majority of languages. \$\endgroup\$ – ceased to turn counterclockwis Mar 27 '12 at 22:51
  • 3
    \$\begingroup\$ leftroundabout, I guess we all know what they roughly mean. This is about mostly language-independent optimizations, i.e those not only useful in Brainfuck but maybe Python, C, Java and Fortran at once. General ideas that you can apply in many languages that work similarly. I don't think there is a need to be that precise and specific in tips and a CW question. This is about helping others to golf, not about pissing them off. \$\endgroup\$ – Joey Apr 5 '12 at 6:10
  • 14
    \$\begingroup\$ Hopefully nobody creates a language called <all languages>... \$\endgroup\$ – mbomb007 Apr 4 '16 at 21:19

35 Answers 35

71
\$\begingroup\$

Merge Loops

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();
\$\endgroup\$
  • \$\begingroup\$ ugoren the loops are still not the same. @Gaffi is right. \$\endgroup\$ – kaoD Jul 31 '13 at 7:51
  • 5
    \$\begingroup\$ @kaoD, in both cases 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\$ – ugoren Jul 31 '13 at 8:31
  • \$\begingroup\$ I'm looking at this again (much, much later), and I don't understand my own question now. I maybe didn't understand the ternary operation before? The way I see it now, it makes sense. \$\endgroup\$ – Gaffi Jul 31 '13 at 11:42
  • 1
    \$\begingroup\$ Woops, I read that very late in the night. You're right! \$\endgroup\$ – kaoD Jul 31 '13 at 19:12
  • 3
    \$\begingroup\$ Very similar: 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\$ – Lynn Jan 17 '17 at 12:01
62
\$\begingroup\$

Just to mention the obvious:

Question your choice of algorithm and try something entirely new.

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).

\$\endgroup\$
  • 12
    \$\begingroup\$ +1 In addition to being good for golfing, this is good exercise for any programmer in many real-world situations! \$\endgroup\$ – Gaffi Apr 5 '12 at 15:49
51
\$\begingroup\$

Use Test-Driven Development

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.

\$\endgroup\$
  • 4
    \$\begingroup\$ I use a spinoff of this method. Since the problems themselves are usually rather simple, I write a program that does the job. Usually this is "readably golfed", so that it's succinct, but newlines etc. are there. I copy this file to a new location and golf it, checking every now and then that the programs return same values for some chosen inputs. If I ever make a mistake leaving me with a broken program, no memory of what I changed and no understanding of my golfed piece, I have something of a "specification" saved as a reference source. \$\endgroup\$ – shiona Dec 24 '13 at 16:30
  • 2
    \$\begingroup\$ I love this method, which is one reason that I tend to include comprehensive test suites for all problems I write. \$\endgroup\$ – Joey Jul 29 '15 at 8:41
  • \$\begingroup\$ @RubberDuck The Do not repeat yourself principle is often strictly followed. \$\endgroup\$ – Jonathan Frech Feb 23 '18 at 23:57
47
\$\begingroup\$

Try to reduce logical statements

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()
\$\endgroup\$
  • 3
    \$\begingroup\$ I'd never have thought of that. \$\endgroup\$ – cjfaure Aug 8 '14 at 13:14
  • 25
    \$\begingroup\$ 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\$ – scragar Sep 11 '14 at 12:13
  • 5
    \$\begingroup\$ @scragar: Correct, but this is not the point of this tip. (It is a valuable independent tip though.) \$\endgroup\$ – Wrzlprmft Sep 11 '14 at 12:22
  • 3
    \$\begingroup\$ @scragar, B>A or foo would evaluate foo if B==A which is not what we want. (Right?) \$\endgroup\$ – msh210 Jun 19 '16 at 7:54
  • 2
    \$\begingroup\$ Also, if you have long imbricated conditions (say with 5/6 parameters), you can use a Truth table and a Karnaugh map to find the shortest boolean expression for it \$\endgroup\$ – Katenkyo Jul 8 '16 at 7:41
33
\$\begingroup\$

Initialize variables using values you already have.

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).

\$\endgroup\$
32
\$\begingroup\$

Use unary ~ 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
\$\endgroup\$
30
\$\begingroup\$

Squeeze whitespace

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ Why does there need to be a space between "{" and "echo", but not between ";" and "echo"? \$\endgroup\$ – Ryan Aug 8 '14 at 14:43
  • 3
    \$\begingroup\$ I used the terms from the manual for OpenBSD sh(1), which says that "{" is a reserved word and ";" is a meta-character. Because of this, "{echo" is one word but ";echo" is two words. Other manuals might explain this differently. Also, the Z shell zsh has different rules. \$\endgroup\$ – kernigh Aug 8 '14 at 23:47
29
\$\begingroup\$

Single Letter Variable Names

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.

\$\endgroup\$
  • 8
    \$\begingroup\$ 26 for non-case-sensitive languages. :-) \$\endgroup\$ – Gaffi Mar 27 '12 at 21:13
  • 12
    \$\begingroup\$ Often $ and _ can be used as identifiers. \$\endgroup\$ – Griffin Mar 27 '12 at 22:10
  • 4
    \$\begingroup\$ @Gaffi: And more than enough for languages which allow Unicode identifiers, unless the task limits you to ASCII or counts bytes rather than characters. \$\endgroup\$ – hammar Mar 27 '12 at 22:25
  • \$\begingroup\$ If you're counting bytes instead of unicode then using extended ascii might be a way to squeeze another ~120 identifiers if you need them(not that for a golf script you should need more than 26 anyway) \$\endgroup\$ – scragar Sep 11 '14 at 12:11
  • 1
    \$\begingroup\$ @ is a valid variable name in T-SQL, use it instead of @a. \$\endgroup\$ – BradC Jun 16 '17 at 14:42
28
\$\begingroup\$

assign functions new names if used multiple times

x = SomeLongFunctionName
x(somedata)
x(somemoredata)
etc
\$\endgroup\$
  • \$\begingroup\$ Only if we use enough calls to x. \$\endgroup\$ – elipszilon Mar 25 at 18:40
25
\$\begingroup\$

Use the conditional operator.

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;
\$\endgroup\$
  • 25
    \$\begingroup\$ Languages that don't have a ternary can use a&&b||c instead. Slightly longer, but still shorter than an if. \$\endgroup\$ – Michael Kohl Mar 28 '12 at 9:43
  • \$\begingroup\$ Then again, some can't use either option (VBA comes to mind), but both are still good suggestions. :-) \$\endgroup\$ – Gaffi Mar 28 '12 at 14:00
  • 1
    \$\begingroup\$ Gaffi: VBA has Iff, although it's a function, so subject to evaluation of all arguments. \$\endgroup\$ – Joey Apr 5 '12 at 6:11
  • \$\begingroup\$ Using ternary in if statements can also be very helpful if(a ? b : c) \$\endgroup\$ – Jojodmo Dec 24 '15 at 19:49
  • 4
    \$\begingroup\$ @MichaelKohl note that 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\$ – Katenkyo Jul 8 '16 at 7:45
23
\$\begingroup\$

Double check your character count

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.

\$\endgroup\$
  • \$\begingroup\$ I don't think I've ever seen a case where this has actually counted... \$\endgroup\$ – Jacob Jul 27 '15 at 22:59
  • 4
    \$\begingroup\$ I've just had this problem myself (hence why I'm adding it). \$\endgroup\$ – Sean Latham Jul 28 '15 at 13:19
23
\$\begingroup\$

Write an explanation of your code

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.

\$\endgroup\$
21
\$\begingroup\$

Read the question carefully

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.

\$\endgroup\$
  • 15
    \$\begingroup\$ I think a better headline here would be "Don't handle non-required edge cases". The phrase "Try to find loopholes" brings to mind ways to avoid doing what is specified through some artful reinterpretation of the rules, whereas what you are offering is merely the good advice not to over-implement your solution. \$\endgroup\$ – Jonathan Van Matre Mar 18 '14 at 15:06
  • 1
    \$\begingroup\$ Yes, but an artful reinterpretation of the rules is part of code golfing as well! (0 char solutions, etc.) \$\endgroup\$ – Tobia Mar 18 '14 at 18:57
  • 8
    \$\begingroup\$ That would/should be a different answer, though. There's a fundamental difference between, for example, implementing a solution that only works for ints because OP didn't require float support, and an answer that prints the text "any prime greater than 100" because "you didn't say it had to be an actual prime number". \$\endgroup\$ – Jonathan Van Matre Mar 18 '14 at 19:20
  • \$\begingroup\$ I think some OPs include a "Test cases subject to change; your code must still work after the change" notice, and really change them if they see just one answer hard-coding the testcases. \$\endgroup\$ – Erik the Outgolfer Sep 11 '16 at 9:49
19
\$\begingroup\$

Use bitwise operations for checking numbers between 0 and any 2n-1

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.

\$\endgroup\$
18
\$\begingroup\$

Reuse function parameters instead of new variables

\$\endgroup\$
  • 1
    \$\begingroup\$ Well for example, in C, your main function is always passed the number of arguments supplied to the program (which is 1 - the program name - by 'default') so with main(i){... you now have a variable with the value of 1 without having to do any assignments. 2 chars saved there.. \$\endgroup\$ – Griffin Mar 27 '12 at 22:09
  • 6
    \$\begingroup\$ I think it's quite specific to the C. Script languages don't need declarations, and in most compiled languages defining a variable isn't longer than defining a parameter. \$\endgroup\$ – ugoren Mar 30 '12 at 18:27
  • \$\begingroup\$ in java when needing a array inside a function which has same type as one parameter you can save off a few bytes by putting that parameter as last and make it a vararg parameter; (used that to shave off some bytes on a function to find longest word in a sentence) \$\endgroup\$ – masterX244 Feb 18 '14 at 14:11
18
\$\begingroup\$

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...

(source link)

\$\endgroup\$
  • \$\begingroup\$ I'm not sure I understand the point of this. What does this reduce from? \$\endgroup\$ – Gaffi Mar 27 '12 at 21:25
  • \$\begingroup\$ @Gaffi you save one character and they do exactly the same \$\endgroup\$ – ajax333221 Mar 27 '12 at 21:40
  • \$\begingroup\$ vs. what alternative? Sorry, not trying to be obstinate, I just don't get it. (newb, here, apparently...) \$\endgroup\$ – Gaffi Mar 27 '12 at 22:22
  • 1
    \$\begingroup\$ Ah, I see. Works on any integer transition from 9 to 10, 99 to 100, etc. Sorry that took me so long! (I say integer only, because I can see an issue with n = 9.5...) \$\endgroup\$ – Gaffi Mar 28 '12 at 10:53
  • 8
    \$\begingroup\$ Also in some languages(if supported) if your numbers are large/small enough the scientific notation may actually save you some chars instead: if(n>99999) vs if(n<1e5) \$\endgroup\$ – scragar Sep 11 '14 at 12:15
16
\$\begingroup\$

Use > and < instead of >= and <=

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)
\$\endgroup\$
  • 3
    \$\begingroup\$ Related: If you are sure an outcome can't be negative, you could use <1 instead of ==0 as zero-check (or >0 instead of !=0 for the mirrored check). \$\endgroup\$ – Kevin Cruijssen Jun 21 '16 at 11:28
  • 1
    \$\begingroup\$ Shouldn't you add a note about x being an integer? \$\endgroup\$ – Zacharý Jun 19 '17 at 16:14
14
\$\begingroup\$

Avoid premature loop breaks

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;
}
\$\endgroup\$
  • 5
    \$\begingroup\$ You can also simplify the 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\$ – marinus Jul 16 '13 at 21:53
14
\$\begingroup\$

use - 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

\$\endgroup\$
12
\$\begingroup\$

Split strings for long arrays

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
\$\endgroup\$
  • 6
    \$\begingroup\$ The notable exception is Ruby, which provides an array-of-strings literal which automatically splits on spaces at the cost of two bytes: %w{Foo Bar Baz Quux}. \$\endgroup\$ – Martin Ender Dec 21 '14 at 11:39
  • 1
    \$\begingroup\$ Perl provides something similar: qw(Foo Bar Baz Quux) becomes a list of strings. \$\endgroup\$ – BenGoldberg Dec 16 '16 at 1:17
12
\$\begingroup\$

Understand what other people did

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.

\$\endgroup\$
10
\$\begingroup\$

know your operator precedence

Whenever You combine several expressions, check the operator precedence table for your language to see if you can reorder stuff to save parentheses.

Examples:

  • In all languages that I know, bitwise operators have a higher precedence than boolean operators: (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.
    That doesn´t save anything for this example, but it will if c is a small integer literal (<14).
  • Bitwise operations have a lower precedence than most arithmetic operations, so if your language implicitly casts boolean to int, you can save a byte on a<b&&c<d with a<b&c<d (unless you need the short circuit evaluation)
\$\endgroup\$
7
\$\begingroup\$

Shorter for-loops

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

\$\endgroup\$
  • \$\begingroup\$ @ETHproductions Thanks for golfing a tip :) \$\endgroup\$ – Cows quack Jan 17 '17 at 18:42
  • \$\begingroup\$ And if the for is the final statement, the ; becomes optional \$\endgroup\$ – elipszilon Mar 25 at 18:46
7
\$\begingroup\$

Use unary ~ 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.

\$\endgroup\$
6
\$\begingroup\$

Compress or/and streaks

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]):
\$\endgroup\$
  • \$\begingroup\$ That's a cool one, I'll have to try it! \$\endgroup\$ – stokastic Sep 11 '14 at 13:18
  • 4
    \$\begingroup\$ Which languages have an all(array-of-Booleans) built-in? \$\endgroup\$ – Peter Taylor Sep 11 '14 at 13:50
  • 3
    \$\begingroup\$ Ruby has it. [a>0,a<10,a+b==4,a+3<1].all? \$\endgroup\$ – kernigh Oct 6 '14 at 19:22
  • 4
    \$\begingroup\$ Although if this were Python you'd be using something like if 10>a>0 and a+b==4>1>a+3: \$\endgroup\$ – Sp3000 Nov 29 '14 at 3:30
  • \$\begingroup\$ @PeterTaylor Haskell has too \$\endgroup\$ – proud haskeller Nov 29 '14 at 17:31
6
\$\begingroup\$

Rely on the compiler to provide the required performance.

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 for a grand total of one character or two across the entire program.

\$\endgroup\$
  • \$\begingroup\$ @ETHproductions yep, sorry, I did \$\endgroup\$ – John Dvorak Jan 17 '17 at 18:43
  • \$\begingroup\$ Shouldn't the first example just be p(x-1)*2? \$\endgroup\$ – Cyoce Feb 7 '17 at 22:25
5
\$\begingroup\$

Maybe somewhat obvious but...

Make use of operator return values

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)
\$\endgroup\$
  • \$\begingroup\$ In which language can you do assignment inside a call? or is that a keyword arg? \$\endgroup\$ – cat Dec 24 '15 at 17:40
  • 2
    \$\begingroup\$ I think assignments are expressions (with the assigned variable's new value as their expression value) in at least C, C++, C#, and Java. a = (b=c)+1; sets b to c, and then sets a to b+1. \$\endgroup\$ – Lynn Dec 25 '15 at 1:22
  • \$\begingroup\$ @Lynn Try a=1+b=c. And you can add PHP and JavaScript to your list. \$\endgroup\$ – Titus Jan 16 '17 at 0:31
  • 2
    \$\begingroup\$ Ruby does this the best. It gives the = operator a higher precedence on the left than the right, so 1+x=2 is valid and evaluates to 3 \$\endgroup\$ – Cyoce Feb 7 '17 at 22:22
  • \$\begingroup\$ @Cyoce afaik it´s that way in all languages where an assignment is an expression. \$\endgroup\$ – Titus Feb 24 '18 at 11:02
5
\$\begingroup\$

Utilize language version / compiler / environment quirks / new features

This is especially useful for s, 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.

\$\endgroup\$
4
\$\begingroup\$

Combine multiple/nested if checks using And/Or when possible.

i.e.:

if (a && (b || c)) {

}

instead of:

if (a) {
    if (b) {
        //Do Stuff
    } elseif (c) {
        //Do same stuff
    }
}
\$\endgroup\$
  • 5
    \$\begingroup\$ Also, use bitty conditionals (&, `|) to remove more characters. \$\endgroup\$ – FUZxxl Aug 13 '12 at 18:33
  • 2
    \$\begingroup\$ Although using bitwise & instead of && removes 1 character in some cases it messes up the operator precedence and you will have to put parentheses to make it work.Use it wisely. \$\endgroup\$ – DollarAkshay Jul 4 '15 at 22:24
4
\$\begingroup\$

Find better ways to initialise your variables

Some other answers came close to mentioning this already, but in many (strictly typed?) languages, it's shorter to initialise x as empty string like:

x:=""

or x as empty rune (char) like:

x:=''

than

var x string

and

var x rune

Using preexisting values is obviously preferred, but not so easy.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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