11

I'm using Eclipse and I will talk about it as an example but I am sure that this is the case in any other IDE.

Eclipse has the CTRL-Shift-F command which automatically formats your code. Is it a good practice to use this kind of formatting? I'm asking in the context of a big project in which there may be many programmers working on the same files and all these are administrated by a CSV system.

Thank you!

Ionut
  • 537
  • 5
  • 14

4 Answers4

12

Yes it is.

Eclipse lets you define your own exportable code style which you can share among all your developers. If you don't want to setup your own formatting style, you may tell your developers to use Eclipse's default formatting. Even if you use the default one, I would advise you to export it and put it on a shared network drive for all developers to use. If later one, you decide to change one thing, you'll be able to easily.

Regardless, I would advise you to start as soon as possible, the best would be at the beginning of the project, and to keep your code always formatted to your chosen format. You'll want to avoid having pure "format commits" during the development because it's hard then to distinguish from "real" commits. What it means in practice is that developers should be rigorous with their commits.

Also, and I put it at the end because it is only my personal opinion, but consider removing the 80 characters per line limit (or extend it to something bigger, we have 120 characters here) which is, if I'm not mistaken the default auto-formatting line-length on Eclipse. We now all have big screens and big screen resolutions so it's ok. On that subject, check out the Is the 80-character limit still relevant in times of widescreen monitors question and answers, you may then be able to make your own decision.

Edit: Thorbjørn makes a good point about the "Format every time a file is saved" functionality.

Jalayn
  • 9,789
  • 4
  • 39
  • 58
5

Yes, it is very good practice. It ensures that everybody agrees how source files should look when being opened in an editor.

Additionally, I have had very good experiences with the "Format every time a file is saved" functionality. (We use the standard Eclipse formatter configuration).

Enable it by opening Eclipse properties, and select Java -> Editor -> Save Actions. Enable "Perform the selected actions on save", and "Format source code".

The reason is naturally that code is always formatted, so you can format at will when editing without fearing anything breaks. This also ensures that the source repository logs are accurate (which is important when doing forensic analysis).

3

You should have a coding standard that you all follow. Whether this is enforced by external tools (such as indent), or IDE, or by hand is kind of irrelevant. What is relevant is that it should be done any before commit.

3

It is a good practice to follow a well-defined coding standard in a project/company. The means of doing it though can vary, just pick whatever works best for you organization. If everyone uses Eclipse then you could just create an exportable code format for your projects and spread it between developers (Eclipse also has an option to format files automatically on each save), however I would recommend creating a document describing the chosen format.

devmiles.com
  • 217
  • 2
  • 10