Good programming style makes your code much more readable and maintainable. Here are some basic style directives that I expect you to follow in your code. Here is a template for structuring your Java classes, which I encourage to you use.
Line up braces. You may have used method (a) in your intro class. The textbook uses method (b), which also happens to be my personal method. Either method is equally acceptable. Consistency is the key here: pick one and stick with it.
Method (a) | Method (b) |
if (a == b) { c = d; e = f; } |
if (a == b) { c = d; e = f; } |
Use whitespace in a meaningful way to make your code readable.
Use capitalization and descriptive names to make your identifiers clear.
Variable and method names should start with a lower case letter. Capital letters should be used to separate multiple words.
Examples:
name, age, personName, getAge
Class names should start with a capital letter. Capital letters should be used to separate multiple words.
Examples:
class Person, class Gradebook, class BlackjackGame
Constants (public static final) should be in all capital letters. Underscores should be used to separate multiple words.
Examples:
MAX_CAPACITY, DIME_VALUE
Declare your variables with appropriate access.
private
.public
if they are to be accessed
from outside of the class. Otherwise, they should be
protected
or private
.Keep your methods short. A method shouldn't be longer than 1-2 screens without good reasons.
Each class header should contain documentation containing
You may use Javadoc to accomplish this, and I encourage you to do so.
Each method should contain header documentation containing
You may use Javadoc to accomplish this, and I encourage you to do so.