This site requires JavaScript, please enable it in your browser!
Greenfoot back

Including code in discussion posts

Here are a couple of things to keep in mind when you want to include some of your code in a discussion post:

1. Use code tags in discussion posts

When you include code in a post, please use the [code] tags so that it's easy for other people to read, find line numbers in, and copy/paste the code. Usually, you can do this by clicking the 'code' link just below the text area in which you type your post. If that doesn't work in your browser, you can manually write '[code]' before your code and '[/code]' just after it.

For example, if you write this:

[code]
void myMethod()
{
    return;
}
[/code]

... then the code will be displayed nicely in your post, like this:

void myMethod()
{
    return;
}

2. Use proper indentation for code

Your code should be properly indented when you post it, to make it easier for others to read and understand your code. You can indent your code correctly automatically by pressing ctrl+shift+I in the editor.

If you don't understand the rules of indentation, here's a brief guide:

  • Every time you have an opening curly brace - '{' - a new line should start immediately afterwards.
  • When you have a closing curly brace - '}' - it should start at the same column as the line with the matching opening brace.
  • Everything between a pair of curly braces - '{' and '}' - should be indented one step more than the lines with the curly braces.
Example:
class Ant extends Actor
{
    public void act()
    {
       if (Greenfoot.isKeyDown("up")) {
           move(5); // <-- this line is indented from the '{' and '}'
                    //      containing it (the line above and below)
       }    //  <-- this is at the same column as the matching 'if' statement
    }   // <-- this is at the same column as the matching '{' at the
        //        beginning of the 'act' method
}    // <-- this is at the same column as the matching '{' (on the second line)

Notice that:

  • The entire "act" method is indented at least one more that the class that contains it
  • The statements inside the act method are indented at least one more level again