Kieran France Kieran France is a programmer for IDRSolutions in charge of internal Java testing. In his spare time he enjoys tinkering with gadgets and code.

Indenting your own code

1 min read

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.



Our software libraries allow you to

Convert PDF files to HTML
Use PDF Forms in a web browser
Convert PDF Documents to an image
Work with PDF Documents in Java
Read and write HEIC and other Image formats in Java
Kieran France Kieran France is a programmer for IDRSolutions in charge of internal Java testing. In his spare time he enjoys tinkering with gadgets and code.

Should you travel by plane or train?

Why travel to Switzerland by train This year, my daughter set me the challenge to take the train rather than fly when I next...
Mark Stephens
1 min read

3 Replies to “Indenting your own code”

  1. > 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. 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.

Comments are closed.