How to Write Completely Unreadable Code

goto

I've written before on how to write readable code. It can be good to write readable code, but today, let's take that in another direction. Let's learn how to write completely unreadable code.

There are many different reasons to write unreadable code. Maybe you want to annoy others, or just have some fun. Whatever the reason, it's not very hard to write unreadable code. In fact, you might even do it without even knowing it.

Choose an Appropriate Language

If your goal is to write unreadable code, the first thing to do is to stay away from Python. The forced indentation and other whitespace makes everything much too readable. Instead, choose something more like Perl, C, or even Lisp.

For example, here is how unreadable a simple "hello, world" program can get in Lisp:

(DEFUN HELLO-WORLD () (PRINT (LIST 'HELLO 'WORLD)))

(I actually have no idea if this works. I just pasted it in from another source and removed some of the whitespace. If you run this and your computer blows up, don't blame me.)

Try counting the number of parentheses, and what they match up with. I bet it's way tougher that a Python "hello, world" program:

print 'hello, world'

No parentheses, or even a semicolon! The best you could really do in Python is this:

import sys,os,re,fileinput;a=[i[2] for i in os.walk('.') if i[2]] [0];[sys.stdout.write(re.sub('at','op',j)) for j in fileinput.input(a,inplace=1)]

This is from Powerful Python One-Liners:

I came up with this one-liner in response to an article that said it couldn't be done as an one-liner in Python.

What this does is replace the substring "at" by "op" on all lines of all files (in place) under the path specified (here, the current path).

Caution: Don't run this on your home directory or you're going to get all your text files edited.

However, Perl is probably the best language for this type of task. The following program prints "Just another Perl hacker."

`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`

Do Things Your Own Way

Why not change things up a bit to show your "personality"? Doing some things your own way can turn into a fun game for the next person to work on your code. If you're using C:

#define TRUE FALSE

If you're using PHP, you can instead do this:

define('ONE', 1);
define('TWO', 2);
define('THREE', 3);
// etc.

And eventually throw in this:

define('THIRTEEN', 42);

Another awesome feature included in some languages is goto. Using that lets you literally jump from one part of a script to another. Pretty cool, eh?

if ($foo == ONE) goto a;
if ($bar == TWO) goto b;
 
goto c;
a:
echo ONE;
goto c;
b:
echo TWO;
goto c;
c:
echo 'not ONE or TWO';

However, some crazy people will try to tell you to avoid using goto. Don't listen to them.

Choose Your Names Wisely

Be creative when choosing names for variables, functions, classes, etc. They don't have to be related to what you're trying to accomplish at all. Come up with an analogy with pizza slices, if you have to. For example, here is some code for a project that could be for say, managing pet rocks.

$pizza = slicePizza('Mr. Pet Rock');
$numPetRocks = slicePizza($pizza) / EIGHT;

If you're not creative enough, single-letter variable names are always a good choice.

$b = a('Mr. Pet Rock');
$c = a($b) / EIGHT;

Make Your Source Files As Small As Possible

To save room on your hard drive, try to make your source files as small as possible. That means removing whitespace, using short variable names, and generally writing as little code as possible.

<?php
if ($fooOrBar == $bazOrBag) {
    doStuff();
}
?>

That's about 56 bytes. We can reduce this to about 32 bytes:

<?php if($fa==$b){doStuff();} ?>

Now we have an extra 24 bytes on our hard drive for another project. That doesn't sound like much, but when you apply this to larger projects, it starts to add up.

Don't Provide Helpful Comments

Don't worry about being helpful in the comments.

Even the pros don't leave helpful comments. Here is a little excerpt from the Unix kernel:

/* You are not expected to understand this. */

A common choice is to treat the next person to read your code like an idiot:

return ONE; // returns ONE

If the other people working on your projects won't like being treated like idiots, the alternative would be to lie in the comments. Don't simply say that something does the opposite of what it really does, because they'll pick up on that easily. Just make it look like that bit of code does something that has either been removed, or has been moved somewhere else.

Conclusion

If you follow all of these steps, you'll have the most unreadable code ever, without having to resort to code obfuscation. Do you have any other tips to reduce the readability of your code?

Stay Updated

Did you enjoy this post? Don't miss a single post by getting free updates!

12 Comments

  1. January 8, 2010

    Hahaha!! Great Post! Thanks for the laugh

  2. Josd
    January 8, 2010

    Haha. What a "useful" post!

  3. Dave
    January 8, 2010

    Seriously, why would anyone ever even think of doing this?

    • Stevesed
      January 8, 2010

      Ummm... I dont think this was meant to be taken seriously...

    • January 8, 2010

      Yeah, like Stevesed said, this post was not meant to be 100% serious :P

  4. david
    January 9, 2010

    Use globals. always good for inflexibility.

    • January 9, 2010

      Yep, globals are a great way to make code less maintainable.

  5. January 10, 2010

    I'd also add to this list an advice to keep all code (+several levels of application) in one place/function, better hardcoded right in the view (in contrast with useless "Composed Method" and "Single Level of Abstraction" patterns)

  6. April 6, 2010

    If you have very wide screens (30") write your lines as wide as the screens are. But minimum 300 characters and more.

Trackbacks/Pingbacks