|
| 1 | +--- |
| 2 | +layout: page |
| 3 | +title: Java Style Guide |
| 4 | +version: 2024pre1 |
| 5 | +--- |
| 6 | + |
| 7 | +Style Guide Version: {{ page.version }} (WIP) |
| 8 | + |
| 9 | +# 4290 Java Style Guide |
| 10 | +This is a style guide made for writing Java code for 4290, more specifically using WPILib. This guide should follow all the specifications listed in the [Index for Style Guides](./). |
| 11 | + |
| 12 | +## Index |
| 13 | + - [1. Folder \& File Structure](#1-folder--file-structure) |
| 14 | + - [1.1 Code Licensing](#11-code-licensing) |
| 15 | + - [1.2 Citing Borrow Code](#12-citing-borrowed-code) |
| 16 | + - [1.3 Constants File](#13-in-file-constants) |
| 17 | + - [1.4 Mapping Controls](#14-mapping-controls) |
| 18 | + - [2. Formatting](#2-formatting--naming) |
| 19 | + - [2.1 Name Cases](#21-name-cases) |
| 20 | + - [2.2 Indentation](#22-indentation) |
| 21 | + |
| 22 | +## 1. Folder & File Structure |
| 23 | +### 1.1 Code Licensing |
| 24 | +Codebases should be licensed under one of the two following licenses: |
| 25 | +- A. [MIT License](https://choosealicense.com/licenses/mit/) |
| 26 | +- B. [Apache License](https://choosealicense.com/licenses/apache-2.0/) |
| 27 | + |
| 28 | +All *robot* code should be licensed under MIT, and while any other codebases are up to the discretion of the maintainers, they should still use one of the previously listed licenses. |
| 29 | + |
| 30 | + |
| 31 | +### 1.2 Citing Borrowed Code |
| 32 | +Any file that uses copied code from any external source, especially other FIRST teams, should be mentioned with a comment at the top of the file. Unless the implementation is very standard, a general rule of thumb is to just cite it. |
| 33 | + |
| 34 | +This comment should contain the name of the source, what was used, and a viable link to the source. An example of this would be: |
| 35 | +```java |
| 36 | +/* The 'Elevator Up' command was borrowed from FRC 4290, https://www.bow4290.org/ */ |
| 37 | + |
| 38 | +class Elevator { |
| 39 | + ... |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | + |
| 44 | +### 1.3 In-File Constants |
| 45 | +All constants should be contained within their relevant classes. For instance, if there is a constant called 'OPERATOR_CONTROLLER_PORT', this constant should be situated within the 'Controls.java' file. |
| 46 | + |
| 47 | +If a constant seems too general to be contained in a specific class, then it should be put into 'RobotContainer.java', and **not** a 'Constants.java' file. |
| 48 | + |
| 49 | + |
| 50 | +### 1.4 Mapping Controls |
| 51 | +All controller mappings should be contained in a dedicated 'Controls.java' file in the root of the project. |
| 52 | + |
| 53 | +The controls file should be documented with comments at the top of the file, explaining every mapping and its functionality, as well as comments above the implementation of each mapping, explaining what it does. An example of a this: |
| 54 | + |
| 55 | +```java |
| 56 | +/* |
| 57 | + * □ / X - Run Intake |
| 58 | + */ |
| 59 | + |
| 60 | + |
| 61 | +... |
| 62 | + |
| 63 | +// Runs the intake |
| 64 | +controller.square_x.whileTrue(bot.intake.runIntake(Args); |
| 65 | + |
| 66 | +``` |
| 67 | + |
| 68 | + |
| 69 | +## 2. Formatting & Naming |
| 70 | +### 2.1 Name Cases |
| 71 | +Casing for any names should follow Java's standards, as follows: |
| 72 | +- Most variables and methods use camel case (eg. camelCase), with the first word being all lower case, and each new word starting with a capital letter. |
| 73 | + |
| 74 | +- Class, interface, and file names use pascal case (eg. PascalCase), with each word starting with a capital letter. |
| 75 | +
|
| 76 | +- Constants use upper snake case (eg. UPPER_SNAKE_CASE), with all words being entirely uppercase, and each word separated with underscores. |
| 77 | +
|
| 78 | +```java |
| 79 | +// Class using Pascal Case, as well as file with same name. |
| 80 | +public class ExampleClass(){ |
| 81 | + // Constant using UPPER_SNAKE_CASE |
| 82 | + public static final VALUE_MULTIPLIER = 4; |
| 83 | +
|
| 84 | + // Method and Variable using camelCase |
| 85 | + public int myValue = 2; |
| 86 | +
|
| 87 | + public int myFunction(){ |
| 88 | + return exampleVariable * VALUE_MULTIPLIER; |
| 89 | + } |
| 90 | +} |
| 91 | +``` |
| 92 | +
|
| 93 | +
|
| 94 | +### 2.2 Indentation |
| 95 | +Indentation should be 2 spaces. This means every time a new block is indented, it should go forward 2 spaces, and back at the end of the block. An example of this is shown below: |
| 96 | +```java |
| 97 | +// Correct |
| 98 | +public int twoSpaces(){ |
| 99 | + // This is the correct indentation size |
| 100 | + return 2; |
| 101 | +} |
| 102 | +
|
| 103 | +// Incorrect |
| 104 | +public int fourSpaces(){ |
| 105 | + // This is the incorrect indentation size |
| 106 | + return 4; |
| 107 | +} |
| 108 | +
|
| 109 | +``` |
| 110 | +
|
0 commit comments