|
| 1 | +# Style Guide for Java |
| 2 | + |
| 3 | +The following guidelines should be followed when writing Java code for this project. |
| 4 | + |
| 5 | +## Naming Conventions |
| 6 | + |
| 7 | +- **Class Names**: Class names should be written in UpperCamelCase. For example: `MyClass`. |
| 8 | +- **Method Names**: Method names should be written in lowerCamelCase. For example: `myMethod`. |
| 9 | +- **Variable Names**: Variable names should also be written in lowerCamelCase. For example: `myVariable`. |
| 10 | +- **Constant Names**: Constant names should be written in all caps with underscores separating words. For example: `MY_CONSTANT`. |
| 11 | + |
| 12 | +## Formatting |
| 13 | + |
| 14 | +- **Indentation**: Use 4 spaces for indentation. Do not use tabs. |
| 15 | +- **Braces**: Opening braces should be on the same line as the statement that begins the block. Closing braces should be on a new line. |
| 16 | +- **Line Length**: Each line of code should be no longer than 80 characters. |
| 17 | +- **Spacing**: There should be a space after keywords (`if`, `for`, `while`, etc.) and between operators (`+`, `-`, `=`, etc.). |
| 18 | + |
| 19 | +## Comments |
| 20 | + |
| 21 | +- **Class Comments**: Every class should have a comment that describes what the class does. |
| 22 | +- **Method Comments**: Every method should have a comment that describes what the method does, its parameters, and its return value (if any). |
| 23 | +- **In-line Comments**: In-line comments should be used sparingly and only to clarify complex code. |
| 24 | + |
| 25 | +## Other Guidelines |
| 26 | + |
| 27 | +- **Use Standard Libraries**: Whenever possible, use standard libraries instead of writing your own code. |
| 28 | +- **Avoid Magic Numbers**: Do not use "magic numbers" (hard-coded numerical values) in your code. Instead, define constants for these values. |
| 29 | +- **Error Handling**: Handle errors and exceptions appropriately. Do not ignore or suppress them. |
| 30 | + |
| 31 | +## Example |
| 32 | + |
| 33 | +Here is an example of Java code that follows these guidelines: |
| 34 | + |
| 35 | +```Java |
| 36 | + |
| 37 | +public class MyClass { |
| 38 | +/** |
| 39 | +* This method does something. |
| 40 | +* |
| 41 | +* @param param1 The first parameter. |
| 42 | +* @param param2 The second parameter. |
| 43 | +* @return The result of the operation. |
| 44 | +*/ |
| 45 | +public int myMethod(int param1, int param2) { |
| 46 | +int result = param1 + param2; |
| 47 | +if (result > 10) { |
| 48 | +result = 10; |
| 49 | +} |
| 50 | +return result; |
| 51 | +} |
| 52 | +} |
| 53 | + |
| 54 | +``` |
| 55 | + |
| 56 | + |
| 57 | +These guidelines should be followed as closely as possible to ensure consistency throughout the codebase. |
0 commit comments