Skip to content

Comments#

// println("test1");
println("test2");

Run this code

Run the code shown above.

What happens? Why?

Help, this looks weird and does not run all commands!#

You will have noticed, that test1 does not get printed. It also looks different in your editor. This is intentional! You have written a comment in your code! That comment just happens to look like a command.

The // tells your computer "don't look at the rest of this line". Processing just skips over it and continues on the next line. We call this a line comment, although the name is not terribly important.

There's also a different style of comment that may span multiple lines (a block comment): /* starts this kind of comment and */ concludes it. It looks like this:

println("test1");
/*
  Hi, everyone!
  I can write whatever I want in here!
  Great!
*/
println("test2");

Different languages may have other types of comments, but these two are pretty universal.

But why?#

There are two important things that you can use comments for:

  • Add some human-readable messages to your code
  • "disable" a portion of your code easily

Human-Readable Messages#

As your code grows larger and larger, It will become more complicated to keep all of it in your head. Comments can help by reminding you what a specific piece of code is about. Or, often more importantly, why it was written this way. This is one form of documentation of your code.

stroke(255, 153, 0); // bright orange outline

Disabling Code#

Sometimes you will need to quickly only run a few lines of code, but not other lines of code. Maybe you're adding a new feature that's not fully finished, maybe you're hunting down an error, and for this you want to try skipping over some part of the program. Either way, both types of comments can help you do that. We call this "commenting out" of code.

clear();
/*
stroke(255, 153, 0);
fill(0, 0, 0);
*/
rect(20, 20, 50, 50);

Normally, this would draw a black rectangle with an orange border, but since we've commented out two of the commands, it will draw differently!

Processing drawing a black rectangle

Processing drawing a white rectangle

Look out for other people's comments!#

From this point forward, I will add further information about the code examples right into the examples by using the comments!

Relevant excerpt from Learning Processing#

(the section starts at 41:03 and runs through 42:27, the video should start and stop on these automatically.)