|
| 1 | +# Java Coding Conventions |
| 2 | + |
| 3 | +## Variables and Types |
| 4 | + |
| 5 | +Choose type declarations that make the code's intent immediately clear to readers. While `var` reduces verbosity, explicit types often communicate intent more effectively. |
| 6 | + |
| 7 | +### Type Declarations |
| 8 | + |
| 9 | +Use `var` for local variable declarations ONLY when the type is immediately obvious from the |
| 10 | +right-hand side. When in doubt, explicit types improve clarity and maintainability. |
| 11 | + |
| 12 | +**Decision Checklist:** |
| 13 | + |
| 14 | +- ✓ Can you tell the exact type in 1 second? → Use `var` |
| 15 | +- ✗ Would you need to check documentation or method signatures? → Use explicit type |
| 16 | +- ✗ Is the type generic, an interface, or complex? → Use explicit type |
| 17 | +- ✗ Is the variable used far from its declaration? → Use explicit type |
| 18 | +- ✗ Does the explicit type reveal important semantic information? → Use explicit type |
| 19 | + |
| 20 | +**What counts as "obvious from the right-hand side":** |
| 21 | + |
| 22 | +- Constructor calls with concrete types: `new ArrayList<String>()`, `new User(...)` |
| 23 | +- Literals: strings, numbers, booleans, `null` |
| 24 | +- Collection factory methods with only literals: `List.of(1, 2, 3)`, `Map.of("key", "value")` |
| 25 | +- Standard library methods with obvious return types: `isEmpty()`, `size()`, `toString()` |
| 26 | +- Builder patterns that return the same type: `User.builder().name("John").build()` |
| 27 | + |
| 28 | +```java |
| 29 | +// Good: Type is clear from the right-hand side |
| 30 | +var list = new ArrayList<String>(); |
| 31 | +var name = "John"; |
| 32 | +var count = 42; |
| 33 | +var user = new User(id, name, email); |
| 34 | +var isEmpty = list.isEmpty(); |
| 35 | +var items = List.of("a", "b", "c"); |
| 36 | + |
| 37 | +// Good: Explicit type when not obvious |
| 38 | +InputStream stream = getStream(); |
| 39 | +Result<User> result = repository.getUser(id); |
| 40 | +Function<String, Integer> parser = Integer::parseInt; |
| 41 | +List<Item> items = Stream.of(item1, item2).collect(toList()); |
| 42 | + |
| 43 | +// Good: Explicit type for interface/abstract return types |
| 44 | +Map<String, Object> config = loadConfiguration(); |
| 45 | +Callable<Data> task = () -> fetchData(); |
| 46 | + |
| 47 | +// Good: Explicit type for method chains |
| 48 | +ProcessedData result = data.transform().normalize(); |
| 49 | + |
| 50 | +// Good: Explicit type for factory methods |
| 51 | +User user = User.create(name); |
| 52 | +Order order = orderService.findById(id); |
| 53 | + |
| 54 | +// Avoid: Unclear type from the right-hand side |
| 55 | +var data = process(); // What type is returned? |
| 56 | +var result = calculate(); // Not immediately obvious |
| 57 | +var callback = createHandler(); // What functional interface? |
| 58 | +``` |
| 59 | + |
| 60 | +**When in doubt, prefer explicit types.** |
| 61 | + |
| 62 | +## Imports |
| 63 | + |
| 64 | +Prefer importing classes and using their simple names over inline fully qualified class names. |
| 65 | +Fully qualified names add visual clutter and make code harder to read. |
| 66 | + |
| 67 | +**Use imports and simple names:** |
| 68 | + |
| 69 | +```java |
| 70 | +import com.inductiveautomation.ignition.gateway.redundancy.types.ProjectState; |
| 71 | +import com.inductiveautomation.ignition.gateway.redundancy.types.HistoryLevel; |
| 72 | + |
| 73 | +// Good: Clean and readable |
| 74 | +return new RedundancyState( |
| 75 | + NodeRole.Backup, |
| 76 | + ProjectState.Unknown, |
| 77 | + HistoryLevel.Partial, |
| 78 | + activityLevel); |
| 79 | +``` |
| 80 | + |
| 81 | +**Avoid inline fully qualified names:** |
| 82 | + |
| 83 | +```java |
| 84 | +// Avoid: Verbose and cluttered |
| 85 | +return new RedundancyState( |
| 86 | + NodeRole.Backup, |
| 87 | + com.inductiveautomation.ignition.gateway.redundancy.types.ProjectState.Unknown, |
| 88 | + com.inductiveautomation.ignition.gateway.redundancy.types.HistoryLevel.Partial, |
| 89 | + activityLevel); |
| 90 | +``` |
| 91 | + |
| 92 | +**Exception:** Use fully qualified names only when necessary to resolve ambiguity between classes |
| 93 | +with the same simple name: |
| 94 | + |
| 95 | +```java |
| 96 | +import java.util.Date; |
| 97 | + |
| 98 | +// Acceptable: Resolves ambiguity with java.util.Date |
| 99 | +java.sql.Date sqlDate = new java.sql.Date(timestamp); |
| 100 | +``` |
| 101 | + |
| 102 | +## Nullability |
| 103 | + |
| 104 | +Packages should be annotated `@NullMarked` (JSpecify). Assume non-null by default; use `@Nullable` |
| 105 | +only for parameters, fields, or return types that genuinely accept or return null. |
| 106 | + |
| 107 | +## Documentation |
| 108 | + |
| 109 | +- Document public APIs with Javadoc |
| 110 | + |
| 111 | +- Focus on why, not what (code should be self-documenting for "what") |
| 112 | + |
| 113 | +- Keep documentation up to date with code changes |
| 114 | + |
| 115 | +- Javadoc tag descriptions MUST begin with a lowercase letter and MUST end with a period |
| 116 | + |
| 117 | + ```java |
| 118 | + /** |
| 119 | + * Creates a new connection to the server. |
| 120 | + * |
| 121 | + * @param endpoint the server endpoint URL. |
| 122 | + * @param timeout the connection timeout in milliseconds. |
| 123 | + * @return the established connection. |
| 124 | + * @throws IOException if the connection fails. |
| 125 | + */ |
| 126 | + ``` |
| 127 | + |
| 128 | +## Other |
| 129 | + |
| 130 | +For any coding practices not explicitly covered by these conventions, defer to established Java best |
| 131 | +practices and community standards. This codebase uses Java 17. |
0 commit comments