Wiki source code of CodeStyle
Version 11.1 by Vincent Massol on 2008/08/29 08:29
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | #startfloatingbox() | ||
2 | *Contents* | ||
3 | #toc ("2" "3" "") | ||
4 | #endfloatingbox() | ||
5 | |||
6 | 1 Code Style | ||
7 | |||
8 | The XWiki project is following a specific coding style. We're using [Checkstyle>http://checkstyle.sourceforge.net] ([checkstyle.xml>http://fisheye2.atlassian.com/browse/~raw,r=trunk/xwiki/xwiki-platform/xwiki-tools/trunk/xwiki-verification-resources/src/main/resources/checkstyle.xml]) to ensure compliance of the code. Our build (the Maven2 one) is configured to fail on violations. However the decision to follow this code style and enforce it was only decided recently and the code base has not all been moved to this new code style. Hence: | ||
9 | * We're only enforcing the code style in the code that have been moved to the new code style. The checked files are defined in ~~xwiki/core/pom.xml~~ (bottom of file). | ||
10 | * We're asking new code to follow the new style and then once a Java file is compliant, to edit ~~xwiki/core/pom.xml~~ and add it there so that we cannot regress... | ||
11 | |||
12 | For examples of "clean" classes see the following classes and their unit tests: | ||
13 | * ZipExplorerPlugin*.java ([main sources>http://fisheye2.cenqua.com/browse/xwiki/platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/plugin/zipexplorer]|[tests>http://fisheye2.cenqua.com/browse/xwiki/platform/core/trunk/xwiki-core/src/test/java/com/xpn/xwiki/plugin/zipexplorer]) | ||
14 | * XWikiMessageTool.java ([main sources>http://fisheye2.cenqua.com/browse/~raw,r=trunk/xwiki/platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/web/XWikiMessageTool.java]|[tests>http://fisheye2.cenqua.com/browse/xwiki/platform/core/trunk/xwiki-core/src/test/java/com/xpn/xwiki/web/XWikiMessageToolTest.java]) | ||
15 | |||
16 | Coding style topics: | ||
17 | #toc("" "" "") | ||
18 | |||
19 | 1.1 Configuring your IDE to use the XWiki code style | ||
20 | |||
21 | 1.1.1 Eclipse 3.x | ||
22 | |||
23 | Download [codestyle-eclipse.xml>http://svn.xwiki.org/svnroot/xwiki/platform/xwiki-tools/trunk/xwiki-verification-resources/src/main/resources/codestyle-eclipse.xml]. | ||
24 | |||
25 | After this, select ~~Window > Preferences~~, and open up the configuration for ~~Java > Code Style > Code Formatter~~. Click on the button labeled ~~Import...~~ and select the file you downloaded. Give the style a name, and click OK. | ||
26 | |||
27 | 1.1.1 IntelliJ IDEA 4.5\+ | ||
28 | |||
29 | Download [codestyle-idea.xml>http://svn.xwiki.org/svnroot/xwiki/platform/xwiki-tools/trunk/xwiki-verification-resources/src/main/resources/codestyle-idea.xml]. | ||
30 | |||
31 | Close IntelliJ IDEA. Place the file in your IntelliJ IDEA user configuration directory. For example for Windows Vista users of IntelliJ IDEA v6.x, it's in ~~C:\\\Users\\\username\\\.IntelliJIdea60\\\config\\\codestyles~~. Then open IntelliJ IDEA again, select ~~File > Project Settings > Project Code Style~~, select ~~Use per-project code style scheme~~, click on ~~Import...~~ and select ~~XWiki~~. | ||
32 | |||
33 | |||
34 | |||
35 | |||
36 | 1.1 Package names | ||
37 | |||
38 | All code that is not moved to the new XWiki Architecture based on components should use <tt>com.xpn.xwiki</tt>. New architecture code should use <tt>org.xwiki</tt>. | ||
39 | |||
40 | 1.1 Interface best practices | ||
41 | |||
42 | 1.1.1 Do not use 'public' in interfaces | ||
43 | |||
44 | Public is always implied in interfaces. Do not write: | ||
45 | |||
46 | {code} | ||
47 | public interface Page | ||
48 | { | ||
49 | public String getParentSpaceKey(); | ||
50 | } | ||
51 | {code} | ||
52 | |||
53 | But instead, write | ||
54 | |||
55 | {code} | ||
56 | public interface Page | ||
57 | { | ||
58 | String getParentSpaceKey(); | ||
59 | } | ||
60 | {code} | ||
61 | |||
62 | 1.1 Javadoc Best Practices | ||
63 | |||
64 | We are following the [Sun Javadoc coding conventions>http://java.sun.com/j2se/javadoc/writingdoccomments/]. Please make sure you're familiar with them when you write javadoc. | ||
65 | |||
66 | 1.1.1 Write useful comments | ||
67 | |||
68 | Do not repeat name of method or any useless information. For example, if you have: | ||
69 | |||
70 | {code} | ||
71 | /** | ||
72 | * @return the id | ||
73 | */ | ||
74 | public String getId() | ||
75 | { | ||
76 | return this.id; | ||
77 | } | ||
78 | {code} | ||
79 | |||
80 | Instead, write: | ||
81 | |||
82 | {code} | ||
83 | /** | ||
84 | * @return the attachment id (the id is the filename of the XWikiAttachment object used to construct this Attachment object) | ||
85 | */ | ||
86 | public String getId() | ||
87 | { | ||
88 | return this.id; | ||
89 | } | ||
90 | {code} | ||
91 | |||
92 | 1.1.1 Do not duplicate Javadoc | ||
93 | |||
94 | If you inherit from an interface then you shouldn't copy the Javadoc from the interface. Instead you should reference it. For example if ~~getSomething()~~ is the implementation a method defined in an inherited ~~Something~~ interface, you shouldn't write: | ||
95 | |||
96 | {code} | ||
97 | /** | ||
98 | * Do something blah blah. | ||
99 | */ | ||
100 | public void doSomething() | ||
101 | { | ||
102 | |||
103 | [...] | ||
104 | {code} | ||
105 | |||
106 | Instead, write the following: | ||
107 | |||
108 | {code} | ||
109 | /** | ||
110 | * {@inheritDoc} | ||
111 | * | ||
112 | * <p>Optionally add here javadoc additional to the one inherited from the parent javadoc.</p> | ||
113 | * | ||
114 | * @see Something#doSomething() | ||
115 | */ | ||
116 | public void doSomething() | ||
117 | { | ||
118 | [...] | ||
119 | {code} | ||
120 | |||
121 | 1.1.1 Do not duplicate method comments with parameters comments | ||
122 | |||
123 | Instead of writing: | ||
124 | |||
125 | {code} | ||
126 | /** | ||
127 | * Returns the key of the space to which this page belongs to. | ||
128 | * | ||
129 | * @return - Parent space's key as String. | ||
130 | */ | ||
131 | public String getParentSpaceKey(); | ||
132 | {code} | ||
133 | |||
134 | Write: | ||
135 | |||
136 | {code} | ||
137 | /** | ||
138 | * @return the key of the space to which this page belongs to. For example "Main". | ||
139 | */ | ||
140 | public String getParentSpaceKey(); | ||
141 | {code} | ||
142 | |||
143 | 1.1.1 Use version and since javadoc tags | ||
144 | |||
145 | For example: | ||
146 | |||
147 | {code} | ||
148 | /** | ||
149 | * Something, blah blah... | ||
150 | * | ||
151 | * @version $Id: $ | ||
152 | * @since 1.6M1 | ||
153 | */ | ||
154 | {code} | ||
155 | |||
156 | #warning("Do not use author javadoc tags.") | ||
157 | |||
158 | 1.1 Class/Interface names | ||
159 | |||
160 | * Prefix class names with <tt>Abstract</tt> for abstract classes | ||
161 | * Class names should start with an uppercase letter | ||
162 | * Interface name should be as short and expressive as possible with no technical prefix or suffix. For example "Parser". | ||
163 | ** As a consequence interfaces shouldn't be prefixed with "I" (as in "IParser") or suffixed with "Interface" (as in "ParserInterface"), nor suffixed with "IF" (as in "ParserIF). | ||
164 | * Classes implementing interfaces should extend the interface name by prefixing it with a characteristic of the implementation. For example "XWikiParser". | ||
165 | ** As a consequence implementation classes shouldn't be suffixed with "Impl", "Implementation", etc. | ||
166 | * Default implementation classes where there's only one implementation provided by XWiki should be prefixed with "Default". As in "DefaultParser". | ||
167 | |||
168 | 1.1 Package names | ||
169 | |||
170 | * The package name for code using the component-based architecture must be of the format <tt>org.xwiki.(module name).*</tt>. For example <tt>org.xwiki.rendering</tt>. | ||
171 | * Non user-public code must be located in an <tt>internal</tt> package. For example: <tt>org.xwiki.rendering.internal.parser.*</tt> |