Paul Haahr / Essays / Java Style |
|
A Programming Style for Java
Paul Haahr
I've been programming full-time in Java for several years, which hardly makes me an expert on the language, but I've developed a style I've become comfortable with and thought it would be worthwhile to write down. This is just my personal style, and while I've written this essay with lots of recommendations, it remains a distinctly personal view of how to write code. This style has evolved from the style I used for C programs, with some influences from the years I spent working in Scheme and Dylan, along with the usual collection of ``little'' or domain-specific languages I've also used, such as YACC or various Unix shells. Much of my style is independent of the language being used; I believe that my Dylan and C programs have substantially the same feel as my Java programs, and that's natural to expect. | |
|
Most of my experience with Java has been in writing a bytecode to native compiler, which is a moderate-sized Java program, containing around 500 classes and 80,000 lines of code. That includes comments and boilerplate code, but I think it's a good amount to have produced in a year. My style has remained mostly consistent over that time, with a few exceptions discussed below. All style guides are inherently rules of thumb. Rules are meant to be broken: I'm sure there are many good reasons to violate every suggestion here. When I'm about to write code which goes against my (previously uncodified) style, I think about why I'm doing something different and whether that reason is sufficient. I assume that the reader of this essay is familiar with Java; the more general comments are independent of the language, but many are concerned with the particulars of Java. Contents
For printing or downloading, you may prefer to use the single page version of this essay |