How to Indent Your Code

Indentations are responsible for making your code easy to read and understand. Indenting your code is always best practice as it helps to show the overall structure of your code.

"Karel indentation gif"

Indenting Your Code

A general rule for Java is to always place brackets on their own lines, and indent the code within the brackets. Here is an example for you to reference:

// Notice how the brackets are on new lines and the code within is indented.
public void run()
{  
    move();
    turnLeft();
    move();
}

You apply the same rule for indentations when using nested control structures. Consider this example:

// Notice how the brackets are on new lines and the code within is indented.
public void run() 
{
    while(ballsPresent())
    {
        if(frontIsClear())
        {
            move();
        }
    }
}

results matching ""

    No results matching ""