# Tips for golfing in <all languages>

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

• "Majority" by what metric? – ceased to turn counterclockwis Mar 27 '12 at 10:41
• @leftaroundabout by the metric of the same word – ajax333221 Mar 27 '12 at 22:01
• 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. – ceased to turn counterclockwis Mar 27 '12 at 22:51
• 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. – Joey Apr 5 '12 at 6:10
• Hopefully nobody creates a language called <all languages>... – mbomb007 Apr 4 '16 at 21:19

# Use bitwise operators in your if statements

In an if statement, you can (usually) use the bitwise operator "and" (usually & , if it is present) rather than && or and and save 1-2 bytes. Sames goes with other keywords (not is the same as a bitwise ! or ~ in many languages (saves 2 bytes), or is bitwise | in many languages (saves 1 byte), etc.).

### Don't declare variables in for-loops

If for-loops have one statement inside of them, you can move the iterating variable's initialisation with the other variables that you already have declared.

int a=1;for(int i=1;i<n;)a*=++i;  // 32 bytes
int a=1,i=1;for(;i<n;)a*=++i;     // 29 bytes


Also, if you have multiple un-nested for-loops, it would be better if you reuse the same variable. This sort of ties in with this answer.

int a=1;for(int i=1;i<n;)a*=++i;for(int j=2;j<n;)a-=j++; // 56 bytes
int a=1,i=1;for(;i<n;)a*=++i;for(i=2;i<n;)a-=i++;        // 49 bytes


(I know you can do a=i=1, but that is not the point)

Now if you don't have any statement before the for-loop (and you only have 1 for-loop), it would be best to initialise the variable inside the for-loop

for(int i=0;i<99;)i*=2;       // 23 bytes
int i=0;for(;i<99;)i*=2;      // 24 bytes

• for some language you can just for(int a=1,i=1;i<n;)a*=++i;for(i=2;i<n;)a-=i++; and using a and i outside is fine – l4m2 Apr 18 '18 at 19:32

Using float -> integer conversion automatic truncates for flooring (positive values) in most languages that don't strongly enforce types:

f.floor     # Ruby
f.to_i      # -1 byte
int $f # Perl 0|$f        # -2 bytes
parseInt(f) # Javascript (Never use Math.floor)
0|f


This will truncate downwards (for positive values), and to get the ceil instead, it can be achieved with the same trick by reflecting the value across 0 (not true in all languages / implementations, not always shorter):

(i/5.0).ceil # Ruby
-(i/-5)


# Use truth tables

An example condition can be (((A AND B) OR (A OR B)) AND C) OR (A OR C) with A, B and C being boolean variables. But it's so long! Is there any way to shorten it, and prove it acts the same after shortening? Well, truth tables to the rescue!

+---------+---------+---------+---------+---------+---------+---------+---------+---------+
|    A    |    B    |    C    |D=A AND B|E=A OR B |F=D OR E |G=F AND C|H=A OR C |I=G OR H |
+---------+---------+---------+---------+---------+---------+---------+---------+---------+
|    0    |    0    |    0    |    0    |    0    |    0    |    0    |    0    |    0    |
+---------+---------+---------+---------+---------+---------+---------+---------+---------+
|    0    |    0    |    1    |    0    |    0    |    0    |    0    |    1    |    1    |
+---------+---------+---------+---------+---------+---------+---------+---------+---------+
|    0    |    1    |    0    |    0    |    1    |    1    |    0    |    0    |    0    |
+---------+---------+---------+---------+---------+---------+---------+---------+---------+
|    0    |    1    |    1    |    0    |    1    |    1    |    1    |    1    |    1    |
+---------+---------+---------+---------+---------+---------+---------+---------+---------+
|    1    |    0    |    0    |    0    |    1    |    1    |    0    |    1    |    1    |
+---------+---------+---------+---------+---------+---------+---------+---------+---------+
|    1    |    0    |    1    |    0    |    1    |    1    |    1    |    1    |    1    |
+---------+---------+---------+---------+---------+---------+---------+---------+---------+
|    1    |    1    |    0    |    1    |    1    |    1    |    0    |    1    |    1    |
+---------+---------+---------+---------+---------+---------+---------+---------+---------+
|    1    |    1    |    1    |    1    |    1    |    1    |    1    |    1    |    1    |
+---------+---------+---------+---------+---------+---------+---------+---------+---------+


The truth table above represents the possible values and outcomes of the example condition we specified earlier. D, E, F, G and H are variables which make our life a lot easier. You can see what they're assigned to at the top row of the table. The final variable, I, is the outcome of the condition. So, now that we have the outcomes handy, we can simplify the table to just the base variables and the final outcome:

+-+-+-+-+
|A|B|C|I|
+-+-+-+-+
|0|0|0|0|
+-+-+-+-+
|0|0|1|1|
+-+-+-+-+
|0|1|0|0|
+-+-+-+-+
|0|1|1|1|
+-+-+-+-+
|1|0|0|1|
+-+-+-+-+
|1|0|1|1|
+-+-+-+-+
|1|1|0|1|
+-+-+-+-+
|1|1|1|1|
+-+-+-+-+


We can now relatively easily see that the condition above really reduces to just A OR C.

• One can also try and ask WolframAlpha. – Jonathan Frech Feb 23 '18 at 23:51
• @JonathanFrech But how are you going to prove it then? ;) – Erik the Outgolfer Feb 24 '18 at 0:30
• Outsource the proof and trust. – Jonathan Frech Feb 24 '18 at 1:01
• @JonathanFrech Sorry, but you must be able to prove your answers by yourself, and Wolfram|Alpha doesn't have a proof, so I'm afraid it isn't going to help you much. :) – Erik the Outgolfer Feb 24 '18 at 11:15
• Using a K-map would be more systematic and guarantees an optimal solution. – ბიმო Dec 27 '18 at 22:37