|
| 1 | += Development Ideology |
| 2 | + |
| 3 | +Truths which we believe to be self-evident (adapted from https://github.com/WhisperSystems/TextSecure/blob/master/contributing.md[TextSecure's]) |
| 4 | + |
| 5 | +1. **The answer is not more options.** |
| 6 | +If you feel compelled to add a preference that's exposed to the user, it's very possible you've made a wrong turn somewhere. |
| 7 | +2. **There are no power users.** |
| 8 | +The idea that some users "understand" concepts better than others has proven to be, for the post part, false. |
| 9 | +If anything, "power users" are more dangerous than the test, and we should avoid exposing dangerous functionality to them. |
| 10 | +3. **If it's "like PGP," it's wrong.** |
| 11 | +PGP is our guide for what not to do. |
| 12 | +4. **It's an asynchronous world.** |
| 13 | +We wary of anything that is anti-asynchronous: ACKs, protocol confirmations, or any protocol-level "advisory" message. |
| 14 | +5. **There is no such thing as time**. |
| 15 | +Protocol ideas that require synchonized clocks are doomed to failure. |
| 16 | +
|
| 17 | += Code Style Guidelines |
| 18 | + |
| 19 | +== Resulting from long experience |
| 20 | + |
| 21 | +* To the largest extent possible, all fields shall be private. Use an IDE to generate the getters and setters. |
| 22 | +* If a class has more than one `volatile` member field, it is probable that there are subtle race conditions. |
| 23 | +Please consider where appropriate encapsulation of the multiple fields into an immutable value object replace the multiple `volatile` member fields with a single `volatile` reference to the value object (or perhaps better yet an `AtomicReference` to allow for `compareAndSet` - if compare-and-set logic is appropriate). |
| 24 | +* If it is `Serializable` it shall have a `serialVersionUID` field. |
| 25 | +Unless code has shipped to users, the initial value of the `serialVersionUID` field shall be `1L`. |
| 26 | + |
| 27 | +== Indentation |
| 28 | + |
| 29 | +1. **Use spaces.** Tabs are banned. |
| 30 | +2. **Java blocks are 4 spaces.** JavaScript blocks as for Java. **XML nesting is 2 spaces** |
| 31 | + |
| 32 | +== Field Naming Conventions |
| 33 | + |
| 34 | +1. "hungarian"-style notation is banned (i.e. instance variable names preceded by an 'm', etc) |
| 35 | +2. If the field is `static final` then it shall be named in `ALL_CAPS_WITH_UNDERSCORES`. |
| 36 | +3. Start variable names with a lowercase letter and use camelCase rather than under_scores. |
| 37 | +4. Spelling and abreviations: |
| 38 | +If the word is widely used in the JVM runtime, stick with the spelling/abreviation in the JVM runtime, e.g. `color` over `colour`, `sync` over `synch`, `async` over `asynch`, etc. |
| 39 | +5. It is acceptable to use `i`, `j`, `k` for loop indices and iterators. |
| 40 | +If you need more than three, you are likely doing something wrong and as such you shall either use full descriptive names or refactor. |
| 41 | +6. It is acceptable to use `e` for the exception in a `try...catch` block. |
| 42 | +7. You shall never use `l` (i.e. lower case `L`) as a variable name. |
| 43 | + |
| 44 | +== Line Length |
| 45 | + |
| 46 | +To the greatest extent possible, please wrap lines to ensure that they do not exceed 120 characters. |
| 47 | + |
| 48 | +== Maven POM file layout |
| 49 | + |
| 50 | +* The `pom.xml` file shall use the sequencing of elements as defined by the `mvn tidy:pom` command (after any indenting fix-up). |
| 51 | +* If you are introducing a property to the `pom.xml` the property must be used in at least two distinct places in the model or a comment justifying the use of a property shall be provided. |
| 52 | +* If the `<plugin>` is in the groupId `org.apache.maven.plugins` you shall omit the `<groupId>`. |
| 53 | +* All `<plugin>` entries shall have an explicit version defined unless inherited from the parent. |
| 54 | + |
| 55 | +== Java code style |
| 56 | + |
| 57 | +=== Modifiers |
| 58 | + |
| 59 | +* For fields, the order is: |
| 60 | + . `public` / `protected` / `private` |
| 61 | + . `static` |
| 62 | + . `final` |
| 63 | + . `transient` |
| 64 | + . `volatile` |
| 65 | +* For methods, the order is: |
| 66 | + . `public` / `protected` / `private` |
| 67 | + . `abstract` |
| 68 | + . `static` |
| 69 | + . `final` |
| 70 | + . `synchronized` |
| 71 | + . `native` |
| 72 | + . `strictfp` |
| 73 | +* For classes, the order is: |
| 74 | + . `public` / `protected` / `private` |
| 75 | + . `abstract` |
| 76 | + . `static` |
| 77 | + . `final` |
| 78 | + . `strictfp` |
| 79 | + |
| 80 | +=== Imports |
| 81 | + |
| 82 | +* For code in `src/main`: |
| 83 | + . `*` imports are banned. |
| 84 | + . `static` imports are strongly discouraged. |
| 85 | + . `static` `*` imports are discouraged unless code readability is significantly enhanced and the import is restricted to a single class. |
| 86 | +* For code in `src/test`: |
| 87 | + . `*` imports of anything other than JUnit classes and Hamcrest matchers are banned. |
| 88 | + . `static` imports of anything other than JUnit classes and Hamcrest matchers are strongly discouraged. |
| 89 | + . `import static org.hamcrest.Matchers.*`, `import static org.junit.Assert.*`, `import static org.junit.Assume.*` are expressly encouraged and permitted. |
| 90 | + Any other `static` `*` imports are discouraged unless code readability is significantly enhanced and the import is restricted to a single class. |
| 91 | + |
| 92 | +=== Annotation placement |
| 93 | + |
| 94 | +* Annotations on classes, interfaces, annotations, enums, methods, fields and local variables shall be on the lines immediately preceding the line where modifier(s) (e.g. `public` / `protected` / `private` / `final`, etc) would be appropriate. |
| 95 | +* Annotations on method arguments shall, to the largest extent possible, be on the same line as the method argument (and, if present, before the `final` modifier) |
| 96 | + |
| 97 | +=== Javadoc |
| 98 | + |
| 99 | +* Each class shall have a Javadoc comment. |
| 100 | +* Each field shall have a Javadoc comment. |
| 101 | +* Unless the method is `private`, it shall have a Javadoc comment. |
| 102 | +* When a method is overriding a method from a super-class / interface, unless the semantics of the method have changed it is sufficient to document the intent of implementing the super-method's contract with: |
| 103 | ++ |
| 104 | +``` |
| 105 | +/** |
| 106 | + * {@inheritDoc} |
| 107 | + */ |
| 108 | +@Override |
| 109 | +``` |
| 110 | +* Getters and Setters shall have a Javadoc comment. |
| 111 | +The following is prefered |
| 112 | ++ |
| 113 | +``` |
| 114 | +/** |
| 115 | + * The count of widgets |
| 116 | + */ |
| 117 | +private int widgetCount; |
| 118 | + |
| 119 | +/** |
| 120 | + * Returns the count of widgets. |
| 121 | + * |
| 122 | + * @return the count of widgets. |
| 123 | + */ |
| 124 | +public int getWidgetCount() { |
| 125 | + return widgetCount; |
| 126 | +} |
| 127 | + |
| 128 | +/** |
| 129 | + * Sets the count of widgets. |
| 130 | + * |
| 131 | + * @param widgetCount the count of widgets. |
| 132 | + */ |
| 133 | +public void setWidgetCount(int widgetCount) { |
| 134 | + this.widgetCount = widgetCount; |
| 135 | +} |
| 136 | +``` |
| 137 | +* When adding a new class / interface / etc, it shall have a `@since` doc comment. |
| 138 | + The version shall be `FIXME` to indicate that the person merging the change should replace the `FIXME` with the next release version number. |
| 139 | + The fields and methods within a class/interface (but not nested classes) will be assumed to have the `@since` annotation of their class/interface unless a different `@since` annotation is present. |
| 140 | + |
| 141 | +=== IDE Configuration |
| 142 | + |
| 143 | +* Eclipse, by and large the IDE defaults are acceptable with the following changes: |
| 144 | +** Tab policy to `Spaces only` |
| 145 | +** Indent statements within `switch` body |
| 146 | +** Maximum line width `120` |
| 147 | +** Line wrapping, ensure all to `wrap where necessary` |
| 148 | +** Organize imports alphabetically, no grouping |
| 149 | +* NetBeans, by and large the IDE defaults are acceptable with the following changes: |
| 150 | +** Tabs and Indents |
| 151 | +*** Change Right Margin to `120` |
| 152 | +*** Indent case statements in switch |
| 153 | +** Wrapping |
| 154 | +*** Change all the `Never` values to `If Long` |
| 155 | +*** Select the checkbox for Wrap After Assignement Operators |
| 156 | +* IntelliJ, by and large the IDE defaults are acceptable with the following changes: |
| 157 | +** Wrapping and Braces |
| 158 | +*** Change `Do not wrap` to `Wrap if long` |
| 159 | +*** Change `Do not force` to `Always` |
| 160 | +** Javadoc |
| 161 | +*** Disable generating `<p/>` on empty lines |
| 162 | +** Imports |
| 163 | +*** Class count to use import with '*': `9999` |
| 164 | +*** Names count to use static import with '*': `99999` |
| 165 | +*** Import Layout |
| 166 | +**** import all other imports |
| 167 | +**** blank line |
| 168 | +**** import static all other imports |
0 commit comments