Docsity

Java Coding Guidelines: Naming Conventions and Comments for CS162 Students, Study notes of Computer Science

Toggle sidebar
bg1
1
1
CS162: Introduction to Computer
Science II
Coding Conventions
2
Java Coding Guidelines
What are coding guidelines?
• Requirements for layout and formatting
Why do we need it?
• Makes it easier for some one to read your
code
• Makes it easier for you to read your code
3
Enforced Coding Guidelines:
Naming Conventions
1. Variable names and method names start with
lowercase letters, with uppercase characters for
the first character of words in the middle eg.
String sublimalMessage = “Trust us, we’re
engineers”;
// not SublimalMessage or sublimal_Message
double getCell(int rowIndex, int columnIndex);
void showBlueScreenOfDeath();
4
Enforced Coding Guidelines:
Naming Conventions
2. Give meaningful names to your variables.
(Note: for loop counters, you may use variable
names like i, j, k unless it would help clarify
what you are looping over)
double x = 6.25; // very non-descriptive
double lunchPrice = 6.25; // much better
double
localBoyzMenuItem7aWithSteakAndChickenAndVegeta
blesSubstitutedForMacSaladPrice = 6.25;
// way too descriptive
5
Enforced Coding Guidelines:
Naming Conventions
3. Class names start with an uppercase letter with
uppercase characters for the first character of words in
the middle
public class NetworkLog {
. . .
}
4. Constant names are uppercase, with an underscore to
separate words
private static final
HE_WHO_SHALL_NOT_BE_NAMED =
“Voldemort”;
6
Enforced Coding Guidelines:
Naming Conventions
5. Avoid magic numbers, use a variable with
a meaningful name or use static constants
public class VideoRental {
// This fine is in dollars
private static final double PENALTY_PER_DAY = 2;
double computeLateFee(int numLateDays) {
double fee = numLateDays * PENALTY_PER_DAY;
// not numLateDays * 2
return fee;
}
}
pf2

Partial preview of the text

Download Java Coding Guidelines: Naming Conventions and Comments for CS162 Students and more Study notes Computer Science in PDF only on Docsity!

1

CS162: Introduction to Computer

Science II

Coding Conventions

2

Java Coding Guidelines

What are coding guidelines?

• Requirements for layout and formatting

Why do we need it?

• Makes it easier for some one to read your

code

• Makes it easier for you to read your code

3

Enforced Coding Guidelines:

Naming Conventions

1. Variable names and method names start with

lowercase letters, with uppercase characters for

the first character of words in the middle eg.

String sublimalMessage = “Trust us, we’re engineers”; // not SublimalMessage or sublimal_Message

double getCell(int rowIndex, int columnIndex);

void showBlueScreenOfDeath();

4

Enforced Coding Guidelines:

Naming Conventions

2. Give meaningful names to your variables.

(Note: for loop counters, you may use variable

names like i, j, k unless it would help clarify

what you are looping over)

double x = 6.25; // very non-descriptive double lunchPrice = 6.25; // much better double localBoyzMenuItem7aWithSteakAndChickenAndVegeta blesSubstitutedForMacSaladPrice = 6.25; // way too descriptive

5

Enforced Coding Guidelines:

Naming Conventions

3. Class names start with an uppercase letter with

uppercase characters for the first character of words in

the middle

public class NetworkLog {

4. Constant names are uppercase, with an underscore to

separate words

private static final

HE_WHO_SHALL_NOT_BE_NAMED =

“Voldemort”;

6

Enforced Coding Guidelines:

Naming Conventions

5. Avoid magic numbers, use a variable with

a meaningful name or use static constants

public class VideoRental { // This fine is in dollars private static final double PENALTY_PER_DAY = 2; double computeLateFee(int numLateDays) { double fee = numLateDays * PENALTY_PER_DAY; // not numLateDays * 2 return fee; } }

7

Enforced Coding Guidelines:

Comments

1. Always identify yourself and the program at the top

of the main file.

<collaborators, description of help> */

2. Always identify yourself and the class at the top of

each class file.

*/ (^8)

Enforced Coding Guidelines:

Comments

3. Every method, except for main and overridden

library methods, must have a javadoc comment

Describe method (1 sentence)

Describe parameters with @param

Describe return value with @return

Describe exceptions with @throws

After you learn about pre and post-

conditions, document them here.

9

Enforced Coding Guidelines:

Comments

Calculates the late fee for a video rental.

@param numLateDays The number of days the

video was returned after the due date

@return The late fee for the video rental

double computeLateFee(int numLateDays) {

10

A Comment about Comments

• Provide additional comments as needed,

especially if your code is complicated

• This helps out people reading your code

• And it helps you remember what you did

11

Enforced Coding Guidelines:

Comments

4. Convert large functions made up of many lines of code into a

much more readable form by:

  • Creating new functions which implement parts of the

bigger function

  • Calling your new functions

// This is nice and readable public PizzaOrderFrame() { createToppingsPanel(); createSizePanel(); createDeliveryOptionsPanel(); } (^12)

Required Reading

Read Appendix A in Big Java!