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.
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.
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!
Latest posts by Kieran France (see all)
- Pros and Cons of Bitbucket Pipelines - March 15, 2018
- Off Screen Rendering Improvements to JPedal Viewer - February 2, 2018
- How to use Cipher streams in Java - January 10, 2018
- Improved Shape Rendering with Soft Clipping - August 24, 2017
- 4 Hidden Features in Java 9 - May 30, 2017