Secret Messages

Welcome young padowan. Sit down. Listen. Here I will reveal to you one of the most important and closely guarded secrets of programming. Are you ready? Is the force with you? Shh, listen…

Programmers write secret messages to each other and themselves.

They've been participating in this shadowy practice for at least 30 years. It is a practice that defines good programmers. You should pick it up too.

These messages are called comments. They are used to document your code so that other people can tell what it does. If you're not into sharing, you should comment your code to remind yourself what your code does and how it does it.

I know what you are thinking, you're thinking "I wrote the damned code, how could I ever forget how it works!". You'd be surprised. Imagine that you have just blazed through a game that took you 100,000 lines of code to write. Chances are, during that time of furious typing you slept under your desk (when you slept) and kept yourself from slipping into the darkness of sleep by liberal amounts of caffeine. In this state, it's easy to forget what you wrote and how you wrote it.

But, wait, that's not all! You can also forget over time. If you had to come back to a game after a few weeks of pleasant female company in a foreign country, all the details of your game's source code would likely be forgotten.

Comments on Comments

Depending on what syntax you use, the comments will be started in a different way, but both work the same way. Everything on the same line after a comment starter is a comment. By learning what comments look like, you can spot them quickly when looking at someone's example code. Let's start with C syntax.

C syntax comments

// This is a comment.

BASIC syntax comments

REM This is a comment.

Back in the ancient days of BASIC, comments were started with the REM command. Generally this practice has been dropped. Today, you can expect an apostrophe ( ' ) in the place of REM.

' Comment madness!

The Secret Pact

The smarter among you reading this will wonder how a computer deals with comments, being that computers only speak machine language and that comments are not real programming code. Well, along time ago, after a big (imaginary) war, a secret pact was arranged between humans and computers.

The pact is this : An interpreter or compiler will politely look the other way when we start a comment and begin on the next line. That's right, both interpreters and compilers completely ignore these comments. In return, programmers are able to continue programming without programming themselves into a corner.

Multi-line Comments

C-syntax programming languages have multi-line comments for those who have alot of word to write. I haven't come across something similar in BASIC syntax language, so I'll just give you the C syntax version.

/* This line begins a comment
blah blah blah blah blah blah.
this line ends a comment */

Everything between /* and */ is considered a comment and ignored by computers the same way as the single line comment.

The Vanishing Trick

Comments have another use, which is a side effect of the secret pact. Remember that computers look the other way politely when a comment is started. Therefore, if you comment out a line of code, the computer won't know it's there. From the computer's point of view it's like a vanishing trick. This is extremely useful for debugging.

display_text("There's a bug around here somewhere, waiting…");
// display_text( I'm the bug");

The second line in the above example is poorly written code. Someone forgot to add the first double quote, which would cause an error. By commenting out the second line, the computer doesn't know its there and because of this, it won't cause an error. It's vanished. Like magic. By commenting out lines like this we can hunt down bugs and errors.

Do Computers Have Balls?

Doesn't matter. They can lose their balls anyway. If you want to know why and how, read the next chapter.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution 2.5 License.