Indenting your own code

Image showing code indentation

Code indentation is very important in making your code readable and easy to follow. Recently we have considered how to format our own code as Eclipse, IDEA and Netbeans appear to have different ways of indenting your own code.

Official Definition

According to the Oracle website indents come in one of two varieties, 4 space and 8 space. 4 space is the standard unit of indentation. This is type is used, for example, to indent method bodies from method name declarations. 8 space is used when line wrapping is required. There are several rules used to determine when a line should be indented. Full information on this convention can be found here.

On top of this the we have the following line “Tabs must be set exactly every 8 spaces (not 4).” which instructs that tabs should be used be used for every 8 spaces. My understanding of this is that something indented twice is indented by 1 tab, something indented three times is indented by 1 tab followed by 4 spaces.

 

How is the IDE indenting your own code

Now in the office we each tend to use our preferred IDE which results in some people using IDEA, Eclipse and Netbeans. These IDE also have different ways of formatting the indentation of your code. The following examples are the defaults.

Eclipse use spaces for the first two indents then a tab for each indent / 4 spaces after that.

Netbeans uses spaces regardless how many indents are being used.

Idea uses spaces regardless how many indents are being used.

 

Opinions?

This leads to the question, what should we do to keep our code uniform? Personally I feel that forcing your team to all work on the same platform, moving away from their preferred IDE is only going to slow them down for quite some time as they learn new shortcuts and positions of things within the IDE.

I personally think the only reasonable course to take is to choose a single style of formatting and change the formatting style within the IDE to match either the style Oracle have decided upon or the more commonly used style, using spaces only.

This post is part of our “Java Articles Index“ series. In these articles, we aim to explore the world of Java and Javafx. Have a look through!

The following two tabs change content below.
Kieran France is a programmer for IDRSolutions. He enjoys tinkering with most things including gadgets, code and electronics. He often has no idea what to write in his blog posts but tries his hardest to make them interesting and entertaining, he also makes no excuses for his odd sense of humor.

Related Posts:

KieranF

About Kieran France

Kieran France is a programmer for IDRSolutions. He enjoys tinkering with most things including gadgets, code and electronics. He often has no idea what to write in his blog posts but tries his hardest to make them interesting and entertaining, he also makes no excuses for his odd sense of humor.

3 thoughts on “Indenting your own code

  1. Neil

    > According to the Oracle website

    So this is one company’s definition of how you can do it. It carries no more weight than just that, and nothing “official”. All companies have their own conventions

  2. You are correct, this is the definition of just one company.

    I went with this definition along with those used by default in the IDEs I mentioned due to their connection to the Java language. I must admit the other reason I used the Oracle convention as an example is because I have always laid out my code almost identically to their conventions when the editor I’m using does not have any automated layout / style functionality, even before I knew there were defined styles.

    I must admit I am curious which convention or style you use to layout your code.

  3. Subhash

    I follow the OTBS (one true brace style) and change the indenting in the IDE to suit that style. Tabs are best instead of spaces, particularly in a team or sharing environment, as the tabs gets automatically reformatted depending on each IDE’s or user’s setting.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>