Does it make a difference if you choose a non-standard indent style?
This is the style I see most often:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class Test {
static public void main(String args[]) throws Exception {
FileInputStream fin = new FileInputStream("infile.txt");
FileOutputStream fout = new FileOutputStream("outfile.txt");
FileChannel inc = fin.getChannel();
FileChannel outc = fout.getChannel();
ByteBuffer bb = ByteBuffer.allocateDirect(1024);
while (true) {
int ret = inc.read(bb);
if (ret == -1)
break;
bb.flip();
outc.write(bb);
bb.clear();
}
}
}
But I prefer this style where everything starts on the next line:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class Test
{
static public void main(String args[]) throws Exception
{
FileInputStream fin = new FileInputStream("infile.txt");
FileOutputStream fout = new FileOutputStream("outfile.txt");
FileChannel inc = fin.getChannel();
FileChannel outc = fout.getChannel();
ByteBuffer bb = ByteBuffer.allocateDirect(1024);
while (true)
{
int ret = inc.read(bb);
if (ret == -1)
break;
bb.flip();
outc.write(bb);
bb.clear();
}
}
}
I find this easier to read but will I encounter problems working with others if I use this style?
This will make all sources be uniform at all times, and ensure that changes in the source repository is actual changes and not just reformats at a later time.
For Eclipse this can be done by configuring the formatter (I happen to like the defaults), and save the preferences which can then be loaded by everybody else. Also the Java -> Editor -> Save actions allow for automatic reformatting at every Ctrl-S, which is also a savable preference.
I’ve found with the above that an additional heuristic
gives a lot of automatically triggered refactorings giving a lot of named locals which then capture intent by their naming, which in turn works well for debugging as you generally have more values in variables which show up in the debugger when single stepping, and you tend to have less opportunities for NullPointerExceptions on each line.
Edit: I wrote on my blog about this.
Edit 2014-08-19: It appears that if the Eclipse formatter settings are saved to a file, IntelliJ IDEA can format source using that file.