Having good coding style makes your code readable and easier to understand for everyone reading it. Treat your programs as if another programmer will continue to maintain the code after you. This includes your future self!
These are the CSE 12 Style Guidelines, based on Oracle and Google Style Guidelines:
args
. See Guideline #10 for how you will be graded on formatting.// initialize an int
is redundant, vs. // set initial length to 10 inches
). It will let others who look at your code understand what's going on without having to spend time understanding your logic first. But don't be too descriptive, as too many comments reduces readability.gg=G
in command mode.a
--> indexOfApple
; letter1
and letter2
--> lowerCaseLetter
and upperCaseLetter
i
, j
, k
, one char is OK and sometimes preferred.private static final
variable (usually named with caps and underscores). Use the keyword "private", unless we specify to use "public", since most likely no other classes and files need to access these constants. These variables names should also be descriptive and placed in the beginning of the class/method grouped together. For example, private static final int TWO = 2;
is not descriptive enough; name it after its purpose, like private static final int LENGTH = 2;
Example Method Code Style
/**
* Return the score of the letter in scrabble.
*
* @param letter The letter in question
* @return The scrabble value of the letter
*/
private int letterScore(char letter)
{
char lowerCaseLetter = Character.toLowerCase(letter);
switch (lowerCaseLetter) {
case CASE_A: return CASE_A_RETURN;
...
case CASE_Z: return CASE_Z_RETURN;
default: return DEFAULT_RETURN;
}
}
Example Inline Comment:
private int countCapitalLetters(String word)
{
// counter to keep track of number of letters <- REDUNDANT, UNDESIRABLE COMMENT
int count = 0;
// iterate through characters and increment count when uppercase
for (int i = 0; i < word.length(); i++) {
if (Character.isUpperCase(word.charAt(i))) {
count++;
}
}
return count;
}
Example Magic Number Refactor:
/**
* In this example, 7 is used as the max password size. To avoid having
* to sprinkle the value 7 all over the code, MAX_PASSWORD_SIZE is used
* instead, which makes more sense. If the max password size needs to be
* changed, the value is updated everywhere MAX_PASSWORD_SIZE is used.
*/
public class Foo {
public void setPassword(String password) {
// don't do this
if (password.length() > 7) {
System.out.println(“Password is too long!”);
}
}
}
// This should be refactored to:
public class Foo {
private static final int MAX_PASSWORD_SIZE = 7;
private static final String PASSWORD_LONG = “Password is too long!”;
public void setPassword(String password) {
if (password.length() > MAX_PASSWORD_SIZE) {
System.out.println(PASSWORD_LONG);
}
}
}
Example Line Wrapping Style
// Example 1: calling a method
someMethod(longExpression1, longExpression2, longExpression3,
longExpression4, longExpression5);
// Example 2: math expression
longName1 = longName2 * (longName3 + longName4 - longName5)
+ CONSTANT * longname6;
// Example 3: method header
int someMethod(int anArg, Object anotherArg, String yetAnotherArg,
Object andStillAnother) {
...
}
// Example 4: method header with a lot of arguments
private static synchronized horkingLongMethodName(int anArg,
Object anotherArg, String yetAnotherArg,
Object andStillAnother) {
...
}
// Example 5: conditional statements
if ((condition1 && condition2)
|| (condition3 && condition4)
||!(condition5 && condition6)) {
doSomethingAboutIt();
}
// THIS COULD ALSO WORK
if ((condition1 && condition2) || (condition3 && condition4)
||!(condition5 && condition6)) {
doSomethingAboutIt();
}
If you’re curious, more coding style guides can be found on the Oracle website.
Google Style Guidelines - https://google.github.io/styleguide/javaguide.html