Working in web development and programming means constantly learning about new libraries and frameworks, which can be really challenging. Languages like C, Java, HTML, and Python are only a few of the hundreds in existence, and each has its own quirks and advantages.

However, functionality, logic, and performance are always prioritised, no matter the programming language — except if you’re referring to Brainfuck, a rare programming language that promotes deliberately confusing code. So, what is the Brainfuck language, and why does it have its unfortunate name? Let’s take a closer look.

What Is Brainfuck?

Illustration of a programmer in a thoughtful pose looking at a computer screen. The screen displays intricate code patterns and the word 'Brain%$@%' prominently. Pastel-colored binary numbers float around the programmer, symbolizing the complexity and uniqueness of the language.

Brainfuck was developed in 1993 by Urban Müller, a Swiss physics student. It was an attempt to make a programming language with the smallest compiler ever.

A compiler is a piece of software that turns code written in an advanced programming language into something that a machine can understand. When a programming language is closer to the instruction set of a machine, less processing will be required. Thus, Müller had to develop a language that was very similar to binary to make the smallest possible compiler.

A compact compiler’s greatest advantage is that it requires less memory on a computer, but doing so involves writing in a language that lacks all the clear commands that characterise standard programming languages. That was the idea; developers like Müller built bizarre languages for the fun of it, not because anyone would actually use them.

Brainfuck’s lexicon consists of only eight instructions, yet the sheer volume of code required to run even the simplest program makes it one of the most difficult programming languages to learn.

Nevertheless, although Brainfuck is intended to be obscure and difficult to understand, that doesn’t make it completely pointless. In fact, it is a Turing complete language, which means that — like conventional programming languages — it can be used to potentially solve any problem with adequate time and memory. However, Brainfuck is not an ideal language for developing software because of the enormous amount of time required to write even the most basic programs.

What Do You Need to Code With Brainfuck?

Here are a few things you need before you can start writing code in Brainfuck:

  • A compiler or interpreter
  • A table listing all ASCII characters and their corresponding decimal values
  • A calculator to help you in quickly determining which Greatest Common Factors to use when increasing the size of a memory block
  • Time and patience. You need to have a ton of spare time on your hands if you want to sit down and create programs in this incredibly inefficient language. Spend an hour or ten writing a program in Brainfuck, and you’ll immediately understand why it’s called what it is.

How Does Brainfuck Work?

Brainfuck operates on the principle of memory manipulation. There are basically 30,000 individual one-byte memory blocks. Though the exact array size is determined by the compiler or interpreter’s implementation, the usual Brainfuck value is 30,000. The memory pointer, as well as its associated value, can be incremented inside this array. Other components of the Brainfuck machine model include a cell pointer and an instruction pointer. Here are the commands you can use to work in this environment:

  • > : This moves the pointer to the right and advances to the next command.
  • < : This moves the pointer to the left and advances to the next command.
  • + : This increments the memory block and advances to the next command.
  • – : This decrements the memory block and advances to the next command.
  • , : This reads a character from the input and stores it in memory, then advances to the next command.
  • . : This writes the memory value to the output as an ASCII char, then advances to the next command.
  • [ : If the cell is not empty, proceed to the next command; otherwise, proceed to the corresponding closing “]”.
  • ] : Return to the corresponding opening “[” if the cell is not empty, or go to the next command if the cell is null.

Any character other than the eight listed above will be disregarded by the compiler or interpreter. Characters other than the eight operators should be treated as comments. Furthermore, at the start of the program, all memory cells in the “array” are set to zero. As for where the memory pointer begins, it’s in the leftmost memory cell.

“Hello, World!” in Brainfuck

Illustration of abstract code morphing into the phrase 'Hello, World!'. The scene is whimsical with pastel-colored swirls and floating symbols. The overall vibe suggests a moment of epiphany in coding.

To get a feel for a new programming language, novice developers often start by writing a simple program named “Hello, World!” This program’s sole purpose is to print the words “Hello, World!” to the screen. Coding for this program would look something like this in a popular language like Python:

print “Hello, World!”

However, if the same program was coded in Brainfuck, here’s what it would look like:

++++++++++[>+++++++>++++++++++>+++>+<<<<-]

++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.——.——–.>+.>.

To see how this converts to “Hello, World!” in the human language, it helps to know that specific groups of commands correspond to ASCII characters, which gives numeric values to common characters like those in the alphabet.

The Bottom Line

At first glance, the term “Brainfuck” might seem like a joke or something you say when you stub your toe. However, it’s actually something quite unique and fascinating.

Aside from the mental exercise, it provides a fresh viewpoint when it comes to exploring alternative programming languages. It demonstrates why higher-level programming languages are used, the value of abstractions, and the need for proper nomenclature in the field. Plus, if you want to create your own interpreter, Brainfuck is a great starting point.

Although this programming language can be quite confusing and truly deserving of its moniker, we hope that this article has made it less of a mind-bender.

If you need something a little more user friendly, take a look at our introduction to Laravel, a popular PHP framework.