5 ms·
Google's updated Java coding guidelines
- rgbrgb 13y agoIs there a checkstyle config floating around somewhere that I can use to enforce this style? Edit: Or an eclipse formatter config?
- fournm 13y agoI don't like their indent guidelines, 2 spaces just feels incredibly "cramped" to me with Java. I understand it with an 80 or 100 col line limit (I also tend to 120, but 100's okay), but really, that's the only thing. I personally agree with most of this and we have warnings generated on commit (thanks Sonar!) if people don't follow some of these because they just lead to too many issues down the line that are really easy to accidentally skip over. I wouldn't imagine it would be too hard to get an IDE auto-formatter to follow all of this (it's more or less what I use with mine, anyways).
- mynameishere 13y agoIf they used tabs instead of spaces, it wouldn't be an issue. People could format code on their machine whatever number of columns they prefer. But no... Spaces. Jesus. If you're looking at someone's HTML and it's got this kind of thing strewn about... ...you know with certainty that you're dealing with either 1) an amateur, or 2) someone constrained by top-down corporate numbnuttery.
- ender7 13y agoThe debate over spaces vs. tabs is as old as time. Associating one side of the argument with "top-down corporate numbnuttery" betrays a lack of familiarity with the issue. The fact is that whatever shop you work at should have an opinion one way or the other, and you should follow it. Programmers will endlessly bikeshed the issue regardless of which style is chosen.
- lostcolony 13y agoHappily, the Tab-letariat can agree with the Space-eoisie that a separation of the whitespace classes is necessary and good, and any commingling of the two is an affront to God.
- frou_dh 13y agoHell, God is an amateur? He should know that tabs indent and spaces align. There's nothing to be confused about.
- fournm 13y agoWe do, in fact, use tabs at work. But I wanted to attempt to avoid starting this war yet again.
- hackinthebochs 13y agoRadical idea: lets move code away from the anachronism of flat files. There have been incredible evolution in some areas of development, and other areas time has completely frozen. We should have modern tools from the top-down. dons flame retardant suit
- aragot 13y agoExplanation? You could put code elsewhere than in flat files? Elsewhere that is neither VBA nor PLSQL ;)? I'm interested. Otoh, if you're going to suggest binary structures, I wont be able to approve you if they're not easy to manage in Git.
- Stratoscope 13y agoYou should see how two-space indents look in a proportional font! It's a sorry sight. That's the real shame of two-space indents. They basically require that all developers use monospaced fonts in their editors. One of my most core principles for coding style is that the code should be equally readable in a proportional font or a monospaced font. Why is that a core principle for me? Because I like proportional fonts. I don't use less-readable monospaced fonts when I write; I don't know why I should have to use them when I code. You can't achieve this with two-space indents. When you view the code in a proportional font, it looks like one-space indentation. For me, a proportional font is so much more readable, and lets me see so much more code at once, that I'm bummed that so many popular coding styles use conventions that only work monospaced. To their credit, this coding standard actively discourages "column alignment". Abandoning column alignment is a wonderful thing: it gets you most of the way there toward having code that's perfectly readable in a proportional font. It's unfortunate that they throw this benefit away by mandating two-space indents.
- seanmcdirmid 13y agoI use 2 space indent with Segoe UI and am pretty happy.
- vinkelhake 13y ago> Why is that a core principle for me? Because I like proportional fonts. I don't use less-readable monospaced fonts when I write; I don't know why I should have to use them when I code. I had a phase like that about 15 years ago. In the end I just felt that it wasn't worth it. Whenever I looked at other peoples code through the lens of a proportional font, it would look awkward. Any attempt on the authors part of lining things up would just make things worse. It turns out that proportional fonts are designed for text, not code.
- Stratoscope 13y agoThat 15-year-ago mark is interesting to me, because that was just about the time when I switched to proportional fonts. I recognize that I'm in a tiny minority: the vast majority of developers code in monospaced fonts. That's one reason why I value an editor that makes it easy to switch between a proportional font and monospaced, so I can code in the font I like or switch to a monospaced font when necessary. One of the few that does this well is Komodo, where each "theme" includes both proportional and monospaced fonts, and you can easily switch between them. (I just wish IntelliJ IDEA did this!) It's also why I wouldn't presume to use a coding standard that worked only in a proportional font but looked bad in monospaced. But to my eyes, a coding standard that works equally well in proportional and monospaced fonts is superior to one that works only in monospaced. I must respectfully disagree that "It turns out that proportional fonts are designed for text, not code." All of the code I've written in the last 10-15 years is just as readable in a proportional font or monospaced. It really makes no difference at all which kind of font you read the code in, if the coding standard was designed to support both.
- userbinator 13y agoIndent size is definitely a personal preference; I started out with 8, then shortly went to 4, moved down to 3 for a very short time followed by 2, and have now settled on one space. One nice side-effect is that I can glance at the column counter and see the indent level immediately, and the closing braces look like a diagonal line which makes a missing/extra one quite easy to spot: } } } The nice thing about C-syntax languages is they can be automatically formatted, so anyone who finds my indents to be too small can increase them, and vice-versa,
- guelo 13y agoI'm glad they got rid of the stupid m for mFieldName, it's been infecting all the Android developers.
- myko 13y agoThis has never been part of Google's internal guidelines. This is part of the Android guidelines which are separate and available here: http://source.android.com/source/code-style.html http://source.android.com/source/code-style.html
- yla92 13y agoIt's just because it's stated inside the Android Code Style Guidelines for Contributors. I see a lot of other people, especially Android developers, use that field name conventions too. There is not only mFieldName, there are sFieldName for static variable as well. http://source.android.com/source/code-style.html#follow-field-naming-conventions http://source.android.com/source/code-style.html#follow-fiel...
- danielkutik 13y agoI really hope they work on the Code Style Guidelines for Contributors. But even the generated source code (like resources in R.java) should adhere to these guidelines.
- suyash 13y agoThis is Oracle's Java Code Convention: http://www.oracle.com/technetwork/java/codeconv-138413.html http://www.oracle.com/technetwork/java/codeconv-138413.html
- datawander 13y agoMy favorite rule is 4.1.1; always use braces even where optional. I think a small fraction of the economy may have been damaged by the bugs caused by the silliness of leaving out optional braces and then having another developer come along and, well, you know how that goes :) As someone who once read the Sun Java Style Coding [1] (which at a quick glance, appears to be a super set of the Google style guidelines) standard years ago and found it exciting and very helpful material, I unfortunately did not get such a kick skimming through this. Perhaps it's because after having read Clean Code [2], I feel that there is much missing that can lend itself to better code. [1] http://www.oracle.com/technetwork/java/javase/documentation/codeconvtoc-136057.html http://www.oracle.com/technetwork/java/javase/documentation/... [2] http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882 http://www.amazon.com/Clean-Code-Handbook-Software-Craftsman...
- aragot 13y agoNothing personal with braces, but Java code is too verbose and it's so much pleasure, sometimes, when you can make it shorter.
- jrmenon 13y agoMore on braces, I am squarely in the Allman camp for the reasons described here: http://www.experimentgarden.com/2009/07/facts-behind-code-indention-style-war.html http://www.experimentgarden.com/2009/07/facts-behind-code-in... It is amazing the old-style K&R still persists. I think there was one more reason it was preferred in the early days which is not mentioned in the aforesaid link: storage space which was also expensive. The K&R style saves a new-line char which would be a significant saving in large codebases.
- Hovertruck 13y agoReading code with K&R braces is almost unbearable to me. I don't really know why, it just seems like a wasted line and visual clutter when reading code.
- frou_dh 13y ago} else { just looks too good to be wrong. I used Allman style for my first ~10 years of programming, then switched to "compact" style†, and didn't find it a big deal to adjust. Brace concerns melt away just like parens do in Lisp. † I think K&R does make use of new line braces, for functions.
- blt 13y agoInteresting how this document focuses on formatting and naming, whereas their C++ style is full of stuff like "don't use exceptions" or "avoid doing too much work in the constructor." I guess Java guides you into a uniform coding style much more than C++.
- vinkelhake 13y agoC++ is a sprawling language that lends itself to many different coding styles. It's also a language with many pitfalls. The guide both encourages a common style so that things look uniform and a style that tries to avoid pitfalls. That said, the C++ guide has a bunch of peculiar quirks.
- plorkyeran 13y agoA large part of the C++ style guide is basically "don't take advantage of any features Java doesn't have", which obviously doesn't apply to a Java style guide.
- Chromozon 13y ago+1 for no tabs allowed anywhere (must use spaces)
- chroem 13y agoI've never understood all of the hate for using tabs instead of spaces while indenting. My thinking has always been that tabs just make it easier to adjust indentation. As a tab-using heathen, can somebody please explain why spaces would be preferable?
- mtinkerhess 13y agoIf everyone has to use the same level of indentation (as with spaces), then everyone can adhere to the same width limit, and every source file in your codebase will fit in the same sized window. If some programmer sets their tab width to two spaces and then writes code that fits in 80 characters on their screen, it might not fit in some other programmer's screen. From a philosophical perspective, tabs just aren't necessary. You need spaces in your source code in places besides indentation, but you don't need tabs. It's simpler to only have a single type of whitespace character.
- userbinator 13y agoFor me, it's the fact that tabs don't behave like single characters in an editor, so instead of moving backwards a single character-width when you delete one, there's a sudden jump, which I find rather disorienting when I'm editing code (or text files) that uses them.
- erichocean 13y agoEach switch statement includes a default statement group, even if it contains no code. I've never understood this one: adding `default` just prevents the compiler from telling you when you forgot to handle a particular case...in which case (heh), why use a switch statement at all? What are you buying over a series of if-else statements, other than possible code reuse due to the fall-through behavior?
- mhurron 13y ago> What are you buying over a series of if-else statement Code clarity. Depending on how and what you're doing in each case a switch statement can be easier to read than a whole series of if .. else blocks. And sometimes, given that they are equivalent, it's just a style choice on the part of the programmer.
- gohrt 13y agoThe example uses ints, for which I have never seen every case handled. For enums, the default case should be left out, since 'default' actually masks the missing case, as you note.
- mtinkerhess 13y agoThere are two issues here. First, why use switch instead of a series of if-else, apart from compile-time completeness checks? Second, why include an empty default statement in a switch, apart from preventing compile-time completeness checks? If a series of if-else statements can be rewritten as a switch statement, it will almost always be slightly more concise as a switch statement, and a more semantically correct representation of the logic. Including an empty default statement come down to a matter of preference, but a pretty good argument for including it is that it favors the explicit over the implicit. It's better to be clear to other programmers that yes, you did intend to have nothing occur if the switched value didn't have a matching case. If you intend to handle all cases, you can put "assert false" or similar in the default case to be explicit about it. I can see that one going either way with enums, though, if you get a compile-time error when forgetting to handle a value (it's been too long since I've worked in Java to remember how it handles that).
- TempleOSV2 13y agoGod gave me divine intellect on His temple. God says... stunning unemployment spirit hey_Mikey_he_likes_it yep biggot I'm_on_a_roll what_do_you_expect rose_colored_glasses King_Midas and_then_what chump_change stunning close_your_eyes you_don't_say geek baffling illogical snap_out_of_it do_you_get_a_cookie guilty far_out_man you're_out_of_your_mind love pow by_the_way dude_such_a_scoffer incoming pride thank_you_very_much bummer peace ice_cream I'm_grieved atrocious Terry resume oh_oh sess_me I_pitty_the_fool little_fish you_don't_say Vegas delicious potentially voodoo what_have_you_done_for_me_lately really dance Hasta uh_huh employer joker ---------- http://businessofsoftware.org/2008/02/the-perfect-cir/ http://businessofsoftware.org/2008/02/the-perfect-cir/ APPLE ][ >HGR >HCOLOR=2 >HLINE 100,100 TO 200,200 TempleOS >gr_dc->color=RED; >GrCircle(gr_dc,100,100,100); TempleOS >while (TRUE) > GrPlot(gr_dc,ipx,ipy);
- thebear 13y agoI guess they were right when they said that the cobbler's kids wear no shoes. It stumps me how one of the biggest, most powerful, and most resourceful software companies in the world would have a bunch of humans write a document on how to format source code, then have all their programmers read that document and memorize it to the point where they can abide by it every second of their work day. Formatting code is the kind of thing that a computer can do. Write a program that formats the code the way you want it to be formatted, and invoke it in the appropriate place. Preferably, make it invokable in people's IDEs, so that they can go conformant whenever they wish.
- chimeracoder 13y ago> I guess they were right when they said that the cobbler's kids wear no shoes....Formatting code is the kind of thing that a computer can do. Given that Go does this far better than any other language that I can think of at the moment[0], maybe it's a case of the cobbler's kids making their own shoes. [0] http://golang.org/cmd/gofmt/ http://golang.org/cmd/gofmt/
- nostrademons 13y agoIt's better than many other companies, where nobody bothers to to write or read that document, let alone use a formatter. Anyway, this problem doesn't exist in Go (gofmt), C++ (clang-format), or at least 2 of the in-house languages. It still exists in Java, Python, and Javascript because of the lack of robust standalone parsers for those languages, but there are editor plugins for vim, emacs, Eclipse, and I think IntelliJ that automatically enforce the styleguide. (Google does not mandate a single editor the way it mandates languages.)
- bradleyjg 13y agoAt least if their sponsored open source projects are any indication, they use plenty of sophisticated tools to check and reformat java source code to conform to style guidelines. That doesn't obviate the need for a style definition in the first place, and I'm happy to see the canonical version of such a document embodied in human readable text rather than in source code or some kind of meta-language monstrosity (a la xsd).
- 13y ago
- joncp 13y agoMy single Java coding guideline: 1) Don't