Hotspot Coding Style

How will my new code best fit in with the Hotspot code base? Here are some guidelines.

The Top Ten List for Writing Good HotSpot Code

  1. Encapsulate code within classes, factor the code, and make it easy to understand.
  2. Use public accessor functions for instance variables accessed outside the class.
  3. Use arrays with abstraction supporting range checks.
  4. Always enumerate all cases in a switch statement or specify default case. It is ok to have an empty default with comment.
  5. Use assert(...), guarantee(...), ShouldNotReachHere(), Unimplemented() and comments wherever needed. (Performance is almost never a reason to omit asserts.)
  6. Use single inheritance, no operator overloading, no C++ exception handling, no templates, and no goto statements.
  7. Assign names to constant literals and use the names instead. Use enum to name integer constants inside class definitions
  8. Use bool for booleans (not int), use true & false (not 1 & 0); use NULL for pointers (not 0).
  9. Instance variable names start with underscore "_", classes start with upper case letter, local functions are all lower case, all must have meaningful names.
  10. Ifdefs should only be used for:

(Draft of 10/12/1998 by Lars Bak and Robert Griesemer.)

Lesser rules and practices of long standing.

(Collected 7/2002 by John Rose, based on observation and old docs.)

Why Care About Style?

Some programmers seem to have lexers and even C preprocessors installed directly behind their eyeballs. The rest of us require code that is not only functionally correct but also easy to read. More than that, since there is no one style for easy-to-read code, and since a mashup of many styles is just as confusing as no style at all, it is important for coders to be conscious of the many implicit stylistic choices that historically have gone into the Hotspot code base.

Nearly all of the guidelines mentioned below have many counter-examples in the Hotspot code base. Finding a counterexample is not sufficient justification for new code to follow the counterexample as a precedent, since readers of your code will rightfully expect your code to follow the greater bulk of precedents documented here. For more on counterexamples, see the section at the bottom of this page.

Whitespace

Naming

Grouping

Patterns

Counterexamples

Occasionally a guideline mentioned here may be just out of synch with the actual Hotspot code base. That's why we're using a wiki to document the guidelines. If you find that a guideline is consistently contradicted by a large number of counterexamples, please mention it here, to assist the rest of us coders with making an informed decision about coding style. The architectural rule, of course, is "When in Rome do as the Romans". Sometimes in the suburbs of Rome the rules are a little different; these differences can be pointed out here.

There may also be corrections needed. Please correct in a cautious and incremental fashion, because other Hotspot coders have been using these guidelines for years.