Indentation in Java – how to deal with it in SVN

Indentation may be a nightmare when you want to merge codes between two different branches.

If there are no formatting standards inside your team, if everybody uses different tool, let it be NetBeans, Eclipse, vi, emacs, you will probably, sooner or later, face the same issue – completely unsynchronized codes.

There was this discussion that involved this topic some time ago. I remember I have read it somewhere here: http://discuss.fogcreek.com/joelonsoftware/default.asp?cmd=show&ixPost=3978. Anyway, sooner or later you will face the hell of merge if everybody uses different indentation, different editor and different tab/space schema.

So far, the best solution I have follows.

Check out the code, reformat it as you like, don’t discuss with people what is better, don’t try to enforce formatting rules, don’t fight over this one – there is no point here. Your IDE probably has some magic key to perform “Format code” trick. Just use it and code with indention as you like it.

However, just before committing changes, reformat code again. And here, you have to make some agreement within the team. But don’t fight here as well, as this “storage format” will be used only inside SVN repository. The formatting tool can be anything you like – for example the Eclipse. Simply format code using command line and that’s it.

eclipse -nosplash \
  -application org.eclipse.jdt.core.JavaCodeFormatter \
  -verbose \
  -config ~/workspace/.metadata/.plugins\
  /org.eclipse.core.runtime/.settings/\
  org.eclipse.jdt.core.prefs \
  MyClass.java

Of course, you can put configuration files wherever you like (even in SVN).

This way, you can make sure that files you commit will be really consistent with the code you have checked out. Merging will be lot more easier since now.

Update!

For java-neon configuration files have moved to different location, make sure to call this command like this

eclipse -nosplash \
  -application org.eclipse.jdt.core.JavaCodeFormatter \
  -verbose \
  -config ~/workspace/.metadata/.plugins\
  /org.eclipse.core.runtime/.settings/\
  org.eclipse.core.resources.prefs \
  MyClass.java