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.
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.
The most basic element of code style is formatting. Here are the requirements for our course.
If the argument list for a method call or method signature runs over the line length, wrap it so that the left edge of the wrapped lines falls on the right side of the open-paren, like so:
int useful_value = computeAUsefulValueFromABunchOfNumbers(first_prelim_value, second_prelim_value, third_prelim_value);
(Note that these are intentionally terrible method and variable names. More on naming later.)
If this left edge doesn't leave enough room (between it and the 80-character right margin) for long argument names, you may wrap those as above (indented just 4 or 8 characters, rather than all the way to the paren).
Curly braces — open-curly braces should go at the end of the same line that contains the signature for the code block that they denote, separated from the signature by a space. Close-curly braces should be the first non-whitespace character in their line, indented at the same level as the signature of the block that they end. For example:
public class StyleExample { public static void printAdvice() { for(int i = 0; i < 10000; i++) { System.out.println("Don't put open-braces on their own lines!"); } System.out.println("Close-braces are indented at the enclosing level."); } }
else
may follow a close-curly-brace on the same line, separated by a space. For example:
if(a) { System.out.println("A!"); } else if(b) { System.out.println("B, but not A!"); } else { System.out.println("Neither A nor B"); }
=
, +=
, etc.), comparisons (==
, <
, !=
, etc.), boolean operators (&&
, ||
, etc.), arithmetic operators (+
, /
, etc.), and :
.
*
when writing polynomials, because it emphasizes the connection between a monomial term and its coefficient.-
(as negation), !
, ++
, etc.if
or for
) from the open-paren that begins its control statements. Similarly, do not separate a method name (either for definition or invocation) from the open-paren that begins its argument list.int j = 0; for(int i = 0; i < 100; i++) { j += compute(i + 36, i - 36); if(!(i % 13 == 0) && (i % 3 == 1)) { j = -j; } }
double y = d_xx * x*x + 2.0*d_xy * x*y + 2.0*d_xz * x*z + d_yy * y*y + 2.0*d_yz * y*z + d_zz * z*z;
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.
if
or else
statement — in this case, the comment belongs below the if
(or else
) line, inside the body, above the contents of the body, and indented at the level of the body. This comment may still describe what the meaning of the condition that puts the program into this body, even though that's technically on the line above the comment./**
and ends with */
before every class signature, method signature, and public data member. Use it to describe the effects or purpose of the thing being commented, not its internal workings — you are addressing an outside user in Javadoc comments.
/**
at the same level as the signature of the thing that the comment is describing. Indent each line of the comment to at least that level as well./**
; some people like to start on the next line. Some people like to put the */
at the end of the last line; others put it on its own line. Some people prepend *
at the beginning of each line (so that there's a solid column of stars along the left edge of the Javadoc); others just indent each line to match the opening /**
. You can choose how you'd like to do it./**
, the single-line text of your comment, and the */
all on the same line, if they'll fit.// Call getNewNumber() on b and save it in a. a = getNewNumber(b);
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.
length()
, size()
, num___()
, etc.boolean
often begin with “is”; e.g., isEmpty()
.ClassNames
, lowercase camel-case for methodNames
, all-lowercase with underscores for variable_names
, and all-caps with underscores for CONSTANT_NAMES
. If you happen to create a name that includes an acronym, treat the acronym as though it were all-lowercase by default (that is, capitalize the first letter if the acronym appears as part of a class name, but not if it's part of a variable name).