Coding conventions are important in this course because they make your code more readable, quicker to develop, and easier to grade. The bigger the project, the more benefit software conventions will have on development time.
In the real world, good software conventions are valued because the code is easier to maintain. Sun Microsystems, who created the Java language, found:
Good coding conventions also allow you to revisit your own software months, maybe years later and quickly pick up where you left off.
firstName
, lastName
,
middleInitial
instead
of fName
, lName
, and
mInitial
.
theLengthOfTheField
should be shortened to fieldLength
. product
, products
,
and Product
are too similar and might
get confused in the program.
TAX_RATE
rather
than taxRate
or TAXRATE
.
final
int CARD_HAND = 5;
.
parseToken()
is a valid name.
isAlphaChar()
.
getBalance()
returns the balance value and setBalance(value)
sets the balance value.
isOverdrawn()
and hasCreditLeft()
describes
the truth values. Avoid using not in the method name,
like
isNotOverdrawn()
. Instead, use !
isOverdrawn()
in the program when this logic
is required.
Every program, no matter how small, should have file header comments to document the purpose, author, and version. Commenting should be done as you code, and not left as an afterthought.
/**
). This is a javadoc convention.
@author
line with your full name.@since
line with the file’s creation date.
*/
) is on its own line./** * This program reads from a text file, counts the number of each letter in the alphabet, * then prints out a frequency chart. * * @author Mr Greenstein * @since August 30, 2017 */
/**
). This
is a javadoc convention. @param
and @return
annotations are used as needed. */
) is on its own line./** * This method computes the monthly payment. * * @param numYears Number of years * @param interest Yearly interest rate * @return The monthly payment */ public double monthlyPayment(int numYears, double interest) { ...
//
). They are
preceded by a blank line and the comment is indented the same distance
as the code around it. System.out.println(); // Start with the letter 'A' lineChar = 'A'; out.printf("\nFrequency of letters in file %s\n\n", inFileName); // Print out each line of letter frequency for (int i = 0; i < 26; i++) { ...
int maxRows = 1000/20; // maximum number of rows int maxCols = 11; // maximum number of coloumns
public void Run() { for (int i = 0; i < index; i++) { if (hasNext(i)) { j = i + 'a';
public Status Play() { // zero out turn counter count = 0; Status stat = new Status(); // Place the pieces randomly on the board // and make them all alive InitializeBoard(); s1.alive = d1.alive = j1.alive = true; Scanner keyboard = new Scanner(System.in); // 1 = slider1, 2 = diag1, 3 = jumper1 int turn = 1; ...
public void student(String aName, int anAge, int aGrade, double anAverage, boolean aMale, String address, String phone)
and
double average = (grade1 + grade2 + grade3 + grade4 + grade5 + grade6 + grade7 + grade8 + grade9) / 5.0;
import java.util.Scanner; import java.util.ArrayList; import java.io.File; import javax.swing.JFrame; import javax.swing.JPanel;
import java.awt.*; // Avoid this! import java.awt.Color; // Do this instead import java.awt.Graphics; ...