You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3. Licensed to the Apache Software Foundation (ASF) under one or more
  4. contributor license agreements. See the NOTICE file distributed with
  5. this work for additional information regarding copyright ownership.
  6. The ASF licenses this file to You under the Apache License, Version 2.0
  7. (the "License"); you may not use this file except in compliance with
  8. the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. -->
  16. <!-- $Id$ -->
  17. <!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V1.3//EN" "http://forrest.apache.org/dtd/document-v13.dtd">
  18. <document>
  19. <header>
  20. <title>Apache™ FOP Development: Coding Conventions</title>
  21. <version>$Revision$</version>
  22. </header>
  23. <body>
  24. <p>Acknowledgement: Some content in this guide was adapted from other Apache™ projects such as Avalon, Cactus, Turbine and Velocity.</p>
  25. <section id="svn">
  26. <title>Subversion Repository</title>
  27. <p>Conventions in this section apply to Repository content, regardless of type:</p>
  28. <ul>
  29. <li>Files checked in must conform to the code conventions for that type of file (java files must conform to java requirements, xml to xml requirements, etc.). If a submitted patch does not conform, it is the responsibility of the committer to bring it into conformance before checking it in. Developers submitting patches are encouraged to follow the code conventions to reduce the work load on the committers.</li>
  30. <li>To reduce the amount of spurious deltas, all text (non-binary) files checked into SVN must have Unix-style line endings (LF only). Many IDEs and editors (even on non-Unix platforms) have settings that can facilitate this convention.</li>
  31. <li>In order to be able to discern commits from a committer from those where a committer applied a patch from a contributor, the commit message must contain a separate line following this pattern: <strong>"Submitted by: [contributor's name] &lt;[contributor's obfuscated e-mail address]&gt;"</strong>. This also helps doing audits on the repository.</li>
  32. </ul>
  33. </section>
  34. <section id="java">
  35. <title>Java</title>
  36. <section id="java-style">
  37. <title>Java Style</title>
  38. <p>
  39. In order to facilitate the human reading of FOP source code,
  40. reduce churning in code, and prevent disputes, the FOP developers
  41. have agreed on a set of coding conventions. The basis of these coding
  42. conventions is documented in the
  43. <link href="http://xml.apache.org/source.html">Apache XML Project Guidelines</link>,
  44. which requires that <strong>all Java Language source code in the repository
  45. must be written in conformance to Sun's</strong>
  46. <link href="http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html">Code Conventions for the Java Programming Language</link>.
  47. In addition, the FOP developers have agreed to other conventions,
  48. which are summarized in the following table:</p>
  49. <table>
  50. <tr>
  51. <th>Convention</th>
  52. <th>Rationale</th>
  53. <th>Enforced By</th>
  54. </tr>
  55. <tr>
  56. <td>Every Java source file starts with the Apache licence header.</td>
  57. <td>Required by Apache.</td>
  58. <td>checkstyle</td>
  59. </tr>
  60. <tr>
  61. <td>No tabs in content.</td>
  62. <td>Programmers should not have to adjust the tab settings in their editor to be able to read the source code.</td>
  63. <td>checkstyle</td>
  64. </tr>
  65. <tr>
  66. <td>Indentation of 4 spaces per level.</td>
  67. <td>Maximize readability.</td>
  68. <td>Not enforced</td>
  69. </tr>
  70. <tr>
  71. <td>Comments, identifiers, and project documentation must be in English.
  72. In general, other languages must not be used, except in translated documentation and language-specific i10n files.
  73. </td>
  74. <td>To avoid the need for everyone to learn all languages, English has become the standard language for many technology projects, and is the only human language that all FOP developers are expected to know.</td>
  75. <td>Not enforced</td>
  76. </tr>
  77. <tr>
  78. <td>American English spelling should be used. Alternative spelling and idioms are tolerated, but may be changed by anyone to American.</td>
  79. <td>Some standard is useful, and American English is widely used and accepted for technology standards and projects.</td>
  80. <td>Not enforced.</td>
  81. </tr>
  82. <tr>
  83. <td>Fully qualify all import statements (no "import java.util.*")</td>
  84. <td>Clarity</td>
  85. <td>checkstyle</td>
  86. </tr>
  87. <tr>
  88. <td>No underscores in variable names except for static finals.</td>
  89. <td>Upper/lower case distinctions can be made in all other variable names, eliminating the need for artificial word boundaries.</td>
  90. <td>checkstyle</td>
  91. </tr>
  92. <tr>
  93. <td>Opening brace for a block should be on the same line as its control statement (if, while, etc.).</td>
  94. <td>Standardization, general preference.</td>
  95. <td>checkstyle</td>
  96. </tr>
  97. <tr>
  98. <td>Write appropriate javadoc entries for all public and protected classes, methods, and variables.</td>
  99. <td>Basic API documentation is needed.</td>
  100. <td>checkstyle</td>
  101. </tr>
  102. <tr>
  103. <td>By <link href="http://mail-archives.apache.org/mod_mbox/jakarta-jmeter-dev/200402.mbox/%3C4039F65E.7020406@atg.com%3E">ASF policy</link>, @author tags are officially discouraged.
  104. However it is permissible to indicate the original author(s) of an entire file or package in a comment provided it follows the copyright and license header.</td>
  105. <td>Attribution of subsequent contributions are recorded by the SVN commit history logs, so should not be included.</td>
  106. <td>checkstyle</td>
  107. </tr>
  108. </table>
  109. <p>For developers that dislike these conventions, one workaround is to develop using their own style, then use a formatting tool like <link href="http://astyle.sourceforge.net/">astyle</link> (Artistic Style) before committing.</p>
  110. </section>
  111. <section id="java-checkstyle">
  112. <title>Checkstyle</title>
  113. <p>The java syntax checker "<jump href="http://checkstyle.sourceforge.net">Checkstyle</jump>" is used to enforce many of the FOP coding standards.
  114. The standards enforced through Checkstyle are documented in its configuration file (xml-fop/checkstyle.cfg).
  115. The conventions defined in the configuration file are an integral part of FOP's coding conventions, and should not be changed without common consent.
  116. In other words, the configuration file contains additional conventions that are not documented on this page, but are generally accepted as good style within the java community (i.e. they are the default behavior of checkstyle, which the FOP developers have decided to adopt <em>de facto</em>).
  117. Any apparent contradiction between the configuration file and this document should be raised on the fop-dev mailing list so that it can be clarified.</p>
  118. <p>To use the "checkstyle" target in FOP's build process, download the source from the <jump href="http://checkstyle.sourceforge.net">Checkstyle web site</jump>, place checkstyle-all-*.jar in the lib directory and call "build checkstyle". Output (in the build directory) includes checkstyle_report.txt and checkstyle_report.xml. If you copy the file contrib/checkstyle-noframes.xsl from Checkstyle into FOP's root directory, you will also get an HTML report.</p>
  119. <p>Checkstyle is probably most useful when integrated into your IDE. See the Checkstyle web site for more information about IDE plugins.</p>
  120. </section>
  121. <section id="java-best-practices">
  122. <title>Java Best Practices</title>
  123. <p>The following general principles are a distillation of best practice expectations on the FOP project.</p>
  124. <ul>
  125. <li>Apply common sense when coding. When coding keep in mind that others will read your code and have to understand it.</li>
  126. <li>Readability comes before performance, at least initially.</li>
  127. <li>If you can refactor some code to make it more understandable, please do so.</li>
  128. <li>Properly document code, especially where it's important.</li>
  129. <li>Use interfaces instead of implementations where possible.
  130. This favors a clearer design and makes switching between implementations easier (Examples: List instead of ArrayList/Vector, Map instead of HashMap/Hashtable).</li>
  131. <li>Avoid using exceptions for flow control.</li>
  132. <li>Try to catch exceptions as much as possible and rethrow higher level exceptions (meaning hiding the low level detailed and putting a message that is more related to the function of your code).</li>
  133. <li>It is important not to lose the stack trace which contains important information.
  134. Use chained exception for that. Avalon Framework provides <jump href="http://jakarta.apache.org/avalon/api/org/apache/avalon/framework/CascadingException.htm">CascadingException</jump> (and similar) for this.
  135. Exception class names and stack traces must be treated like gold.
  136. Do whatever is required so that this information is not lost.
  137. Printing error messages to System.err or System.out is useless in a server-side environment where this info is usually lost.</li>
  138. <li>Always log the exception at the higher level (i.e. where it is handled and not rethrown).</li>
  139. <li>Try to avoid catching Throwable or Exception and catch specific exceptions instead.</li>
  140. </ul>
  141. </section>
  142. <section id="java-resources">
  143. <title>Resources</title>
  144. <ul>
  145. <li>[book on code style] Code Complete by Steve McConnell.</li>
  146. <li>[code formatting software] <jump href="http://jrefactory.sourceforge.net">JRefactory</jump>.</li>
  147. </ul>
  148. </section>
  149. <section id="java-links">
  150. <title>Related Links</title>
  151. <ul>
  152. <li><jump href="http://xmlgraphics.apache.org/repo.html">Apache XML Graphics Code Repositories</jump></li>
  153. <li><jump href="http://jakarta.apache.org/site/faqs.html#Coding%20Conventions%20and%20Standards">Jakarta Code Conventions and Standards</jump> (see Coding Conventions and Standards section)</li>
  154. </ul>
  155. </section>
  156. </section>
  157. <section id="xml">
  158. <title>XML</title>
  159. <table>
  160. <tr>
  161. <th>Convention</th>
  162. <th>Rationale</th>
  163. <th>Enforced By</th>
  164. </tr>
  165. <tr>
  166. <td>XML files must always be well-formed. Validation is optional.</td>
  167. <td>Document integrity</td>
  168. <td>Not enforced</td>
  169. </tr>
  170. <tr>
  171. <td>No tabs in content.</td>
  172. <td>Users should not have to adjust tab settings in their editor to be able to read the content.</td>
  173. <td>Not enforced</td>
  174. </tr>
  175. <tr>
  176. <td>Indentation of 2 spaces per level</td>
  177. <td>Maximize readability</td>
  178. <td>Not enforced</td>
  179. </tr>
  180. </table>
  181. </section>
  182. </body>
  183. </document>