Java Style Guide
Write clean, readable Java code.
Good code is easy to read, easy to understand, and easy to maintain. Following a consistent style makes your code easier for both you and your teammates.
Naming
Use descriptive names.
Variables and Methods
Use camelCase.
double drivePower;
int currentPosition;
void openClaw() {
}Classes
Use PascalCase.
public class IntakeSubsystem {
}Constants
Use UPPER_SNAKE_CASE.
public static final double MAX_POWER = 1.0;Indentation
Indent code inside braces.
if (gamepad1.a) {
intake.start();
}Avoid inconsistent indentation.
if (gamepad1.a) {
intake.start();
}Braces
Always use braces, even for a single statement.
Good:
if (ready) {
shoot();
}Avoid:
if (ready)
shoot();Keep Methods Small
Each method should do one job.
Good:
updateDrive();
updateIntake();
updateLift();Instead of one large method that does everything.
Use Comments Sparingly
Comments should explain why, not what.
Good:
// Prevent the arm from exceeding its limit.Avoid:
// Set motor power.
motor.setPower(1.0);The code already explains what it's doing.
Avoid Magic Numbers
Instead of writing numbers directly, use constants.
Avoid:
motor.setPower(0.75);Better:
public static final double DRIVE_SPEED = 0.75;
motor.setPower(DRIVE_SPEED);Keep Formatting Consistent
Use spaces around operators.
int total = left + right;Not:
int total=left+right;Organize Your Classes
A common order is:
- Constants
- Fields
- Constructors
- Public methods
- Private helper methods
Example
public class Intake {
private final DcMotor motor;
public Intake(DcMotor motor) {
this.motor = motor;
}
public void start() {
motor.setPower(1.0);
}
public void stop() {
motor.setPower(0.0);
}
}Key Takeaways
- Use descriptive names.
- Follow Java naming conventions.
- Always use braces.
- Keep methods focused on one task.
- Replace magic numbers with constants.
- Write readable, maintainable code.
Was this resource helpful?
