Skip to content

Latest commit

 

History

History
90 lines (71 loc) · 1.95 KB

how-to-indent-your-code.md

File metadata and controls

90 lines (71 loc) · 1.95 KB

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();
        }
    }
}