Project

General

Profile

Code-style » History » Version 12

Tobias Wich, 11/07/2014 11:08 AM

1 1 Tobias Wich
{{toc}}
2
3
h1. Code-Style Guidelines
4
5
This document defines style rules and formatting advises, so the resulting code is easily readable and understandable. As this project will be open sourced at some point, the code should reflect the discipline of the team and its work.
6
7
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED",  "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.
8
9
10
h1. General Rules
11
12
13
h2. Whitespace and Indentation
14
15
Almost any editor behaves differently, or at least can, displaying tabs. Automatic code indentation quickly becomes a pain when different settings are chosen for tab-size and indent-size.
16
The three most frequently used settings are:
17
# tab-size=4 and indent-size=4
18
 This setting means that each indentation step is 4 space characters wide and each 4 space indentation block is replaced with a tab character. This setting is very popular so most editors will not need any changes to support it.
19
# tab-size=8 and indent-size=4
20
 This setting means that each indentation step is 4 space characters wide and each 8 space indentation block is replaced with a tab character. This is a rather old setting, however it has the advantage that mis-formatting quickly becomes apparent by just looking at the source code.
21
# no tabs and indent-size=4
22
 This setting means that each indentation step is 4 space characters wide and tabs are not used at all.
23
24
The indentation style in every applicable source file must use the setting tab-size=8 and indent-size=4. There is however one exception. Imported files such as the eCard XML Schema may be reformatted, but it is not required to do so.
25
26
27
h2. Line Endings, Newlines and Line Wrapping
28
29
Line endings must be either \r\n for Windows systems and \n for every other system. Git takes care of the proper conversion (see http://stackoverflow.com/questions/3206843/how-line-ending-conversions-work-with-git-core-autocrlf-between-different-operat) only if these characters are used uniformly across a text file. Binary files are not affected by this rule.
30
One common pitfall is to import XML files containing the wrong or even mixed line endings. Before adding such files to the repository check the line ending and convert it if needed. Sophisticated editors can take care of this task.
31
32
Empty lines must only contain the newline character, but no other whitespace characters. Furthermore whitespace between printable characters and the newline character must not be present. Some editors have a feature to indicate these whitespace characters. The formatter should also take care of their removal.
33
34
Most newline related rules are explained later in the document.
35
Regarding text files, the following rule must be applied:
36
# Add a newline (\n or \r\n on windows) character to the last line containing text
37
Most of the reasons to do that are historical (see http://stackoverflow.com/questions/729692/why-should-files-end-with-a-newline), however it also has and impact on diffs and the version control system.
38
39
Line wrapping rules are relevant, when very wide lines must be wrapped, so they fit on the screen. With todays common widescreen displays, the 80 column rule seems a bit restrictive. But still it is useful to restrict the line site further than the screen can display.
40
There are a few reasons for that:
41
# General readability
42
 It is much harder for the human eye to follow very long lines and jump back to the front on the next line.
43
 A general rule of thumb is, the longer the line, the more a new line to visually separate the following line of code is needed.
44
# General comprehensibility
45
 Long lines tend to carry a lot of information. One line should perform a single action, so that its intention is clear immediately. Command chaining should therefore not be used except there are good reasons to do so.
46
# Side by Side Display
47
 When looking at diffs or editing files side by side, the screen size is suddenly only half as wide as it used to be. When the majority of lines is too long, this is practically impossible.
48
 A line length of 120 columns has proven to be a good value for the line length and thus should not be exceeded in general.
49
50
51
h2. File Encoding
52
53
The default file encoding nowadays should be UTF-8. It is often argued that using UTF-16 results in faster text processing. This myth stems from the misconception that UTF-16 is a fixed length encoding, which is not true. In fact only the underlying code table is defined as 2 byte codepage (UCS-2) rather than an 4 byte codepage (UCS-4). Despite its name, UTF-8 is the newer standard.
54
Besides the UTF file encodings, UNIX systems used to encode in ISO/IEC 8859 or one of its region specific encodings. Windows systems use CP-1252 as default encoding which is an extension to ISO/IEC 8859.
55
56
In Java generally everything read with a "Reader":http://docs.oracle.com/javase/6/docs/api/java/io/Reader.html or written with a "Writer":http://docs.oracle.com/javase/6/docs/api/java/io/Writer.html is encoded with the default character set defined by the operating system. That also includes XML processors and serializer. It must be made sure that all data is read accordingly by supplying the respective encoding name to the module.
57
58
Source as well as resource files, except binary files, must be encoded as UTF-8. When writing a module which processes text, it must be assumed that the input data is UTF-8 if nothing else is specified like a XML Header or a HTTP encoding header.
59
60
61
h2. Language
62
63
It is more than good style to name identifiers in code and write documentation, including comments, in English. There is a high possibility that the code will be read and used by foreign language speakers. Furthermore it gives a uniform picture when only a single language is used.
64
65
66
h1. XML Rules
67
68
Writing XML documents is pretty straightforward, however a few rules should be followed, so that the files are readable and easily understandable.
69
70
# The file must contain a XML header with newline character
71
 <pre><code class="xml"><?xml version="1.0" encoding="UTF-8"?></code></pre>
72
# Indentation of the root element must start at level 0
73
# Child elements must be one indentation level deeper than their parent element is.
74
# Child elements may be written inline with their parents only if the parent element's closing tag is also on the same line. The inline use of child elements is not recommended.
75
 <pre><code class="xml"><parent><child/></parent></code></pre>
76
# Attributes must be written inline with the element if possible.
77
 An exception is made when successive attributes exceed the supposed column size. It is then up to the developer to decide whether multiple attributes can be written inline or if each attribute is written on a new line.
78
# Attributes on new lines must be indented as much as the attribute on the element line.
79
 <pre><code class="xml"><wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
80
                  xmlns:oec="http://ws.openecard.org"
81
                  targetNamespace="http://ws.openecard.org"
82
                  name="Combined_eCard">
83
</code></pre>
84
# Elements without content must be written in shorthand notation (see example).
85
 <pre><code class="xml"><ShortHand/></code></pre>
86
# Shorthand notation elements must not include space characters, except one or more attributes are located inside it. In that case there must be exactly one space character.
87
 <pre><code class="xml"><ShortHand1/>
88
<ShortHand2 attr="val" />
89
</code></pre>
90
# Empty newlines may be introduced into the document to separate groups of elements or single elements which stand out from the context.
91
 <pre><code class="xml"><!-- This is the first Group describing XXX -->
92
<Group1>
93
    <Child/>
94
</Group1>
95
<Group1Second/>
96
<!-- This is the second Group describing YYY -->
97
<Group2>
98
    <Child/>
99
</Group2>
100
<MoreSiblingsOrNot/>
101
</code></pre>
102
103
104
h1. Java Rules
105
106
h2. File Structure
107
108
The file structure of any Java file (including enums and interfaces) is as shown below. Some elements are optional.
109
110 10 Tobias Wich
# Multiline comment containing the license followed by a newline.
111 1 Tobias Wich
 The license block must have a maximum column width of 80. The first and last line must not contain text. All other lines must be prefixed with the string " * ".
112 10 Tobias Wich
 The Copyright notice should contain the date the file was created and if and edit is done in a different year, then this year. The Copyright holder of the file should also be mentioned.
113
 <pre><code class="java">/****************************************************************************
114
 * Copyright (C) 2012-2014 ecsec GmbH.
115
 * All rights reserved.
116
 * Contact: ecsec GmbH (info@ecsec.de)
117
 *
118
 * This file is part of the Open eCard App.
119
 *
120
 * GNU General Public License Usage
121
 * This file may be used under the terms of the GNU General Public
122
 * License version 3.0 as published by the Free Software Foundation
123
 * and appearing in the file LICENSE.GPL included in the packaging of
124
 * this file. Please review the following information to ensure the
125
 * GNU General Public License version 3.0 requirements will be met:
126
 * http://www.gnu.org/copyleft/gpl.html.
127
 *
128
 * Other Usage
129
 * Alternatively, this file may be used in accordance with the terms
130
 * and conditions contained in a signed written agreement between
131
 * you and ecsec GmbH.
132
 *
133
 ***************************************************************************/
134 1 Tobias Wich
135
</code></pre>
136
# Package identifier followed by a newline.
137
 <pre><code class="java">package org.example.com;
138
139
</code></pre>
140
# Import statements followed by a newline.
141
 The imports must be sorted lexicographically. They must not include empty lines. Static imports go to the end of the list.
142
 <pre><code class="java">import java.io.File;
143
import java.io.Inputstream;
144
import static org.junit.Assert.*;
145
146
</code></pre>
147
# The class javadoc comment prefixed with a newline and followed by the class definition.
148 11 Tobias Wich
 The class comment must contain an @author tag in the form used in the example.
149 1 Tobias Wich
 <pre><code class="java">
150
151
/**
152
 * Description of class Foo.
153
 * @see Bar
154 11 Tobias Wich
 * @author Max Mustermann
155 1 Tobias Wich
 */
156
public class Foo extends Bar {
157
158
}
159
</code></pre>
160
161
Putting these elements together results in the file below. Pay attention to the number of newlines between the elements.
162
<pre><code class="java">
163 11 Tobias Wich
/****************************************************************************
164
 * Copyright (C) 2012-2014 ecsec GmbH.
165
 * All rights reserved.
166
 * Contact: ecsec GmbH (info@ecsec.de)
167
 *
168
 * This file is part of the Open eCard App.
169
 *
170
 * GNU General Public License Usage
171
 * This file may be used under the terms of the GNU General Public
172
 * License version 3.0 as published by the Free Software Foundation
173
 * and appearing in the file LICENSE.GPL included in the packaging of
174
 * this file. Please review the following information to ensure the
175
 * GNU General Public License version 3.0 requirements will be met:
176
 * http://www.gnu.org/copyleft/gpl.html.
177
 *
178
 * Other Usage
179
 * Alternatively, this file may be used in accordance with the terms
180
 * and conditions contained in a signed written agreement between
181
 * you and ecsec GmbH.
182
 *
183
 ***************************************************************************/
184 1 Tobias Wich
185
package org.example.com;
186
187
import java.io.File;
188
import java.io.Inputstream;
189
import static org.junit.Assert.*;
190 12 Tobias Wich
191
192 1 Tobias Wich
/**
193
 * Description of class Foo.
194
 * @see Bar
195 11 Tobias Wich
 * @author Max Mustermann
196 1 Tobias Wich
 */
197
public class Foo extends Bar {
198
199
}
200
201
</code></pre>
202
203
h2. Java Class
204
205
This section deals with the rules specific to Java classes. Most rules are also applicable to Enum and Interface types.
206
207
208
h3. Internal Class Structure
209
210
* The class definition must take up exactly one line and must be followed by an empty newline.
211
 Inner classes are free to disobey the newline rule. However it is advisable to add a newline, if the inner class so big, that it can not be comprehended at once.
212
213
The following elements are recommended to appear in the given order, but it is up to the developer to change their ordering and even mix up their elements.
214
Each block should at least have a newline before and after it, so that is clear that there are different blocks which kind of belong together.
215
Individual groups can be formed inside these blocks by placing newlines between them. One example may be to separate static and non static members.
216
* Member variables
217
* Constructors
218
* Inner classes
219
* Functions
220
 In general functions should be separated by one empty newline.
221
 When writing functions, the following rules help to create readable code:
222
** Very short get and set functions can omit the newline.
223
 <pre><code class="java">
224
public void setFoo(Foo foo) {
225
    this.foo = foo;
226
}
227
public Foo getFoo() {
228
    return this.foo;
229
}
230
</code></pre>
231
** Very big functions should be separated by two newlines.
232
 In case the function is very big (say it takes up the whole screen), then two instead of only one empty newlines can be inserted, so that the reader can quickly find the next function.
233
** Public Static functions should be separated from non static functions.
234
 A block of static functions can be placed before the non static functions. Private static functions which are only used in one particular function should be placed directly in front of the using function, so that it becomes apparent that these two functions belong together.
235
236
* The end of the class symbol } is prefixed with an empty newline.
237
 Inner classes are free to disobey the newline rule. However it is advisable to add a newline, if the inner class so big, that it can not be comprehended at once.
238
 This newline should be in sync with the first newline after the class definition.