Skip to main content

Java Programming Standards

Comments

  • All methods should be commented following java doc styling. Make sure to include any parameters or return members. Example is show below:
    /**
    * Calculates the chassis traveled distance based on provided speed and duration
    *
    * @param speed current chassis speed (m/s)
    * @param time duration of travel (s)
    *
    * @return traveled distance (m)
    */
    double calculateDistance(double speed, double time){
    return speed * time;
    }

Variables

  • All variables should be named using snake_case. Examples are shown below:
    double current_elevator_position;
    int final_counter;
    boolean is_enabled;
    float error;
  • Variables belonging to a class (or members) should have a trailing underscore _. Examples are shown below:
    double current_elevator_position_;
    int final_counter_;
    boolean is_enabled_;
    float error_;
  • Variables that are constant should be named using SCREAMING_SNAKE_CASE. The should also use the final and static modifiers to prevent accidental updates and multiple instances. Examples are shown below:
    static final double ELEVATOR_HOME_POSITION;
    static final int COUNTER_RESET;
    static final boolean IS_MOTOR_INVERTED;
    static final float I_TERM_ERROR_MAX;

Enums

  • Both the Enum name and the fields should be named using SCREAMING_SNAKE_CASE. Example shown below:
    enum STATE {
    IDLE,
    SCORE,
    HOME,
    TRAVELING_UP,
    TRAVELING_DOWN
    }

Loops

  • Never use while loops
    • They are dangerous in a controls application and could result in a infinite loop if not updated correctly.
  • Iterators for a for loops should be used in order of i, j, and then k.
    • If the iterator is used external to the for it should be named something unique to its purpose.

Methods / Functions

  • Methods should be named using lowerCamelCase. Examples are shown below:
    void setElevatorVoltage(double volts){
    motor.set(volts);
    }

    int getElevatorVoltage(){
    return volts;
    }
  • Methods that are just updating or returning members should prefix with set or get Examples shown below:
    double target = 0;

    void setTarget(double value){
    target = value;
    }

    double getTarget(){
    return target;
    }
  • The opening curly brace { for a method should be on the same line as the method signature. Examples shown below:
    // This is correct formatting
    void doThisThing(){
    // Method code here
    }

    // Do not do this, this is incorrect formatting
    void dontDoThisThing()
    {
    // Method code here
    }
    Single line methods

    This is only ok when a method is a single line

    double getThatThing(){ return that_thing; }

Classes