kaitai-io / kaitai_struct_java_runtime

Kaitai Struct: runtime for Java
MIT License
42 stars 18 forks source link

Java style enforcement #24

Open GreyCat opened 4 years ago

GreyCat commented 4 years ago

Given that our Java codebase grows, I guess it's time to decide on a certain coding style and stick to it.

Historically, Java coding style guidelines were slightly better than total anarchy of C++, but still pretty ambiguous. As far as I understand, the only current tool which performs the enforcement is checkstyle, and it offers two options:

"Sun style" seems to be very ambiguous and checks almost nothing, and "Google style" seems to be pretty strict, but it's mandating 2 spaces indent.

I was thinking of adopting Google style with one exception of using 4 spaces indent (as it seems to be the default in most Java environments).

This will be beneficial both for this Java runtime project and kaitai_struct_gui.

@Mingun, @ams-tschoening — any ideas / preferences?

ams-tschoening commented 4 years ago

I don't like too strict guidelines, people should be allowed to decide that some pieces of code are more readable the way they are even if not following strict guidelines. Did work with some version of the Google-guidelines some years ago and was forced to reformat all code automatically by the IDE and the results were really often very unreadable, e.g. because things were placed entirely on the right side of the monitor, spanning multiple lines and stuff like that.

From my experience there's no formatter and guide able to handle every piece of code, therefore every strict enforcement will fail. In my opinion it's better to trust developers that they want to create readable code and people should be allowed to change their minds. So only discuss some rough guidelines like tabs vs. spaces, indentation size, where brackets are, spaces before/after brackets and stuff like that, but don't enforce too much.

Mingun commented 4 years ago

I agree with @ams-tschoening, that too strict guidlines are not necessary. But I will agree that it looks reasonable to record the bracket placement rules and the size of the indents. I have nothing against the 2-space indents, moreover, they seem more sympathetic to me, but I will not insist.

The rest of the rules would apply with caution. However, it can be noticed that Java community is quite conscious and everyone adheres to the same style as a whole.

So only discuss some rough guidelines like tabs vs. spaces, indentation size, where brackets are, spaces before/after brackets and stuff like that, but don't enforce too much.

Totally agree.

ams-tschoening commented 4 years ago

Things like tabs vs. spaces etc. can easily be achieved already using EditorConfig already, that might be low hanging fruits. More detailed formats need to be supported by various IDEs for example, EditorConfig is available almost everywhere.

Besides that, I used Astyle pretty successfully to style codebase of log4cxx in the past. Might be worth a look as well.