Java Style Guide

Intro: What and Why

This is a short guide to the code style expected for this course.

Every major software endeavor eventually develops style guidelines; this is true for everything from open-source projects to giant corporate codebases. The pragmatic purpose of good style is so that code written by one person is comprehensible to other people working with that code. When code across an entire project, written by many developers, shares a consistent style, then any developer can dive into the code at any spot and have a fair chance of understanding it. This is a really remarkable feeling as a developer (one I hope you will get to experience later in your career), and the opposite (inconsistent style across a codebase) is a nightmare I hope you'll dodge.

Interestingly enough, even if you're just working on code for yourself, having a consistent style benefits you too: the future you is a completely different person. In a few months or years, you'll have forgotten all the particulars of a given project you worked on, but if you wrote your code to be comprehensible to anyone, this will include future-you. And trust me, future-you will be grateful, because they're going to want to pillage ideas from your code!

In fact, in a few hours your memory of your work will be a little cloudy. Maintaining good style makes debugging much easier, because it's a way to guide two-hours-in-the-future-you through the code that you wrote. So this is also implicitly a basic guide to good software-development practice.

All this motivates the existence of this style guide. To motivate you to follow it, your code in CS 201 will be graded on style. This guide is an explanation of what we will be looking for when we grade.

This style guide is strongly inspired by Google's style requirements for Java and C++. You can check out those links for way more detailed guidelines and deeper discussion of the reasoning behind them.

A Caveat

The fundamental rule of any style guide is that clarity is the goal. If you encounter an isolated situation in which breaking a particular guideline serves to make your code more easily comprehensible, then go ahead and do it. But think critically about whether it's necessary to break the guideline, or if you can do something different to accomplish your goal in a style-conforming way.

Formatting

The most basic element of code style is formatting. Here are the requirements for our course.

Commenting

The essential rule of commenting is this: assume your reader knows the language better than you, but has no idea what you're trying to accomplish. Communicate the meaning of your code, not the mechanics.

Naming

The names you choose for classes, methods, and variables ought to act like comments in their own right: they should make it easier for an outsider to understand the meaning and intention of your code, just by looking at variable names.