|
| 1 | +# Coding Standard |
| 2 | + |
| 3 | +When contributing code or patches to Jython, |
| 4 | +please try to follow these guidelines. |
| 5 | + |
| 6 | +This guidance is quite old (Java 6 or earlier) |
| 7 | +so will not answer all questions. |
| 8 | +It applies to Jython 2. |
| 9 | +It has been rescued from the Jython Wiki. |
| 10 | + |
| 11 | +Parts of the Jython code base do not conform very well to this guide. |
| 12 | +(They may be even older than the rules.) |
| 13 | +Avoid the temptation to reformat them when working on a contribution, |
| 14 | +as it makes it difficult for a reviewer to see |
| 15 | +the code change being contributed. |
| 16 | + |
| 17 | + |
| 18 | +## Python Code |
| 19 | + |
| 20 | +In general, follow PEP 8. |
| 21 | + |
| 22 | +When importing Java code, always use fully qualified class names, |
| 23 | +not package names i.e. `from java.lang import String` |
| 24 | +instead of `from java import lang`. |
| 25 | + |
| 26 | +## Java Code |
| 27 | + |
| 28 | +In general, have in mind that code is read more times than it is written, |
| 29 | +and that contributors not familiar with your thinking |
| 30 | +will seek to maintain or extend it. |
| 31 | +Code that cannot be followed by others is likely to be replaced |
| 32 | +when it is found (or suspected) to be the source of a bug or limitation, |
| 33 | +and then the value of your excellent work is lost. |
| 34 | + |
| 35 | +1. Javadoc on any publicly exposed method or field. |
| 36 | +2. 4 spaces for indentation, no tabs. |
| 37 | +3. No nested ternary statements (no ternary statements inside other ternaries). |
| 38 | +4. A luxurious 100 characters per line. |
| 39 | +5. No copy and pasted, repeated code: |
| 40 | + if you're doing the same thing twice, make a method. |
| 41 | +6. Braces on all loops and `if-else` statements |
| 42 | +7. A space between an if and its parenthesis i.e. `if (` instead of `if(`. |
| 43 | +8. Spaces between annotation element-value pairs, |
| 44 | + i.e. `@ExposedType(name = "unicode", base = PyBaseString.class)` |
| 45 | + instead of `@ExposedType(name="unicode",base=PyBaseString.class)`. |
| 46 | +9. Methods longer than 10 lines should have whitespace and comments breaking them up into coherent operations. |
| 47 | +10. Descriptive names for fields and methods. |
| 48 | +11. No @author tags in code. |
| 49 | +12. Any field on an object that isn't modified after construction should be final. |
| 50 | +13. Fields at the top of the class. |
| 51 | +14. Don't declare fields with their default values ie `private Object blah;` |
| 52 | + instead of private `Object blah = null;` |
| 53 | + and `int i;` instead of `int i = 0;` |
| 54 | +15. Comments begin with a space unless they're commented out code: |
| 55 | + Poor: |
| 56 | + ``` |
| 57 | + //TODO: Not implemented yet |
| 58 | + // bar.bar() |
| 59 | + ``` |
| 60 | + Better: |
| 61 | + ``` |
| 62 | + // TODO: Not implemented yet |
| 63 | + //bar.bar() |
| 64 | + ``` |
| 65 | +
|
| 66 | +Beyond these rules, follow the Sun Java standards. |
| 67 | +
|
| 68 | +> [!NOTE] |
| 69 | +> We should provide a set of formatting definitions that can be imported into |
| 70 | +> the Eclipse IDE to get it to follow the standards. |
| 71 | +> The Java formatter in VSCode will read the rules Eclipse exports. |
| 72 | +> In 2025 this was incomplete and provided no way to edit the rules, |
| 73 | +> buthings may have moved on, |
| 74 | +> see https://code.visualstudio.com/docs/java/java-linting. |
| 75 | +
|
| 76 | +
|
| 77 | +### Example (adapted from Sun document) |
| 78 | +
|
| 79 | +```java |
| 80 | +package org.jython.blah; |
| 81 | +import org.jython.blah.BlahBlah; |
| 82 | +/** |
| 83 | + * Class description goes here. |
| 84 | + */ |
| 85 | +public class Blah extends SomeClass { |
| 86 | + /* A class implementation comment can go here. */ |
| 87 | + /** classVar1 documentation comment */ |
| 88 | + public static int classVar1; |
| 89 | + /** |
| 90 | + * classVar2 documentation comment that happens to be |
| 91 | + * more than one line long |
| 92 | + */ |
| 93 | + private static Object classVar2; |
| 94 | + /** instanceVar1 documentation comment */ |
| 95 | + public Object instanceVar1; |
| 96 | + /** instanceVar2 documentation comment */ |
| 97 | + protected int instanceVar2; |
| 98 | + /** instanceVar3 documentation comment */ |
| 99 | + private Object[] instanceVar3; |
| 100 | +
|
| 101 | + /** |
| 102 | + * ...constructor Blah documentation comment... |
| 103 | + */ |
| 104 | + public Blah() { |
| 105 | + // ...implementation goes here... |
| 106 | + } |
| 107 | +
|
| 108 | + /** |
| 109 | + * ...method doSomething documentation comment... |
| 110 | + */ |
| 111 | + public void doSomething() { |
| 112 | + // ...implementation goes here... |
| 113 | + } |
| 114 | +
|
| 115 | + /** |
| 116 | + * ...method doSomethingElse documentation comment... |
| 117 | + * @param someParam description |
| 118 | + */ |
| 119 | + public void doSomethingElse(Object someParam) { |
| 120 | + // ...implementation goes here... |
| 121 | + } |
| 122 | +} |
| 123 | +``` |
| 124 | + |
0 commit comments