Skip to content

Commit 9157825

Browse files
authored
Merge pull request #595 from jf205/qhelp-guide
Query help style-guide
2 parents 2a30dee + bdda3ae commit 9157825

File tree

1 file changed

+183
-0
lines changed

1 file changed

+183
-0
lines changed

docs/query-help-style-guide.md

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
# Query help style-guide
2+
3+
## Introduction
4+
5+
When you contribute a new query to Semmle/ql for inclusion in the standard queries, you should also write a query help file. This file provides detailed information about the purpose and use of the query, which is available to users in LGTM (for example [here](https://lgtm.com/rules/1506093386171/)) and on the query homepages:
6+
7+
* [C/C++ queries](https://wiki.semmle.com/pages/viewpage.action?pageId=19334052)
8+
* [C# queries](https://wiki.semmle.com/display/CSHARP/C%23+queries)
9+
* [COBOL queries](https://wiki.semmle.com/display/COBOL/COBOL+queries)
10+
* [Java queries](https://wiki.semmle.com/display/JAVA/Java+queries)
11+
* [JavaScript queries](https://wiki.semmle.com/display/JS/JavaScript+queries)
12+
* [Python queries](https://wiki.semmle.com/display/PYTHON/Python+queries)
13+
14+
### Location and file name
15+
16+
Query help files must have the same base name as the query they describe and must be located in the same directory.
17+
18+
### File structure and layout
19+
20+
Query files are written using an XML format called Qhelp, and stored in a file with a `.qhelp` extension. The basic structure is as follows:
21+
22+
```
23+
<!DOCTYPE qhelp SYSTEM "qhelp.dtd">
24+
<qhelp>
25+
CONTAINS one or more section-level elements
26+
</qhelp>
27+
```
28+
29+
The header and single top-level `qhelp` element are both mandatory.
30+
31+
### Section-level elements
32+
33+
Section-level elements are used to group the information within the query help file. All query help files should include at least the following section elements, in the order specified:
34+
35+
1. `overview`—a short summary of the issue that the query identifies, including an explanation of how it could affect the behavior of the program.
36+
2. `recommendation`—information on how to fix the issue highlighted by the query.
37+
3. `example`—an example of code showing the problem. Where possible, this section should also include a solution to the issue.
38+
4. `references`—relevant references, such as authoritative sources on language semantics and best practice.
39+
40+
For further information about the other section-level, block, list and table elements supported by the qhelp format, see [Qhelp files](https://wiki.semmle.com/display/SD/Qhelp+files).
41+
42+
43+
## English style
44+
45+
You should write the overview and recommendation elements in simple English that is easy to follow. You should:
46+
47+
* Use simple sentence structures and avoid complex or academic language.
48+
* Avoid colloquialisms and contractions.
49+
* Use US English spelling throughout.
50+
* Use words that are in common usage.
51+
52+
## Code examples
53+
54+
Whenever possible, you should include a code example that helps to explain the issue you are highlighting. Any code examples that you include should adhere to the following guidelines:
55+
56+
* The example should be less than 20 lines, but it should still clearly illustrate the issue that the query identifies. If appropriate, then the example may also be runnable.
57+
* Put the code example after the recommendation element where possible. Only include an example in the description element if absolutely necessary.
58+
* If you are using an example to illustrate the solution to a problem, and the change required is minor, avoid repeating the whole example. It is preferable to either describe the change required or to include a smaller snippet of the corrected code.
59+
* Clearly indicate which of the samples is an example of bad coding practice and which is recommended practice.
60+
* Define the code examples in `src` files. The language is inferred from the file extension:
61+
62+
```
63+
<example>
64+
<p>This example highlights poor coding practice</p>
65+
66+
<sample src = "example-code-bad.java" />
67+
68+
<p>This example shows how to fix the code</p>
69+
70+
<sample src = "example-code-fixed.java" />
71+
</example>
72+
```
73+
74+
Note, if any code words are included in the `overview` and `recommendation` sections, they should be formatted with `<code> ... </code>` for emphasis.
75+
76+
## Including references
77+
78+
You should include one or more references, list formatted with `<li> ... </li>` for each item, to provide further information about the problem that your query is designed to find. References can be of the following types:
79+
80+
### Books
81+
82+
If you are citing a book, use the following format:
83+
84+
>\<Author-initial. Surname>, _\<Book title>_ \<page/chapter etc.\>, \<Publisher\>, \<date\>.
85+
86+
For example:
87+
88+
>W. C. Wake, _Refactoring Workbook_, pp. 93 – 94, Addison-Wesley Professional, 2004.
89+
90+
Note, & symbols need to be replaced by \&amp;. The symbol will be displayed correctly in the html files generated from the qhelp files.
91+
92+
### Academic papers
93+
94+
If you are citing an academic paper, we recommend adopting the reference style of the journal that you are citing. For example:
95+
96+
>S. R. Chidamber and C. F. Kemerer, _A metrics suite for object-oriented design_. IEEE Transactions on Software Engineering, 20(6):476-493, 1994.
97+
98+
99+
### Websites
100+
101+
If you are citing a website, please use the following format, without breadcrumb trails:
102+
103+
>\<Name of website>: \<Name of page or anchor>
104+
105+
For example:
106+
107+
>Java 6 API Specification: [Object.clone()](http://docs.oracle.com/javase/6/docs/api/java/lang/Object.html#clone%28%29).
108+
109+
### Referencing potential security weaknesses
110+
111+
If your query checks code for a CWE weakness, you should use the `@tags` element in the query file to reference the associated CWEs, as explained [here](query-metadata-style-guide.md). When you use these tags, a link to the appropriate entry from the [MITRE.org](https://cwe.mitre.org/scoring/index.html) site will automatically appear as a reference in the qhelp file.
112+
113+
## Query help example
114+
115+
The following example is a qhelp file for a query from the standard query suite for Java:
116+
117+
```
118+
<!DOCTYPE qhelp PUBLIC
119+
"-//Semmle//qhelp//EN"
120+
"qhelp.dtd">
121+
<qhelp>
122+
123+
<overview>
124+
<p>A control structure (an <code>if</code> statement or a loop) has a body that is either a block
125+
of statements surrounded by curly braces or a single statement.</p>
126+
127+
<p>If you omit braces, it is particularly important to ensure that the indentation of the code
128+
matches the control flow of the code.</p>
129+
130+
</overview>
131+
<recommendation>
132+
133+
<p>It is usually considered good practice to include braces for all control
134+
structures in Java. This is because it makes it easier to maintain the code
135+
later. For example, it's easy to see at a glance which part of the code is in the
136+
scope of an <code>if</code> statement, and adding more statements to the body of the <code>if</code>
137+
statement is less error-prone.</p>
138+
139+
<p>You should also ensure that the indentation of the code is consistent with the actual flow of
140+
control, so that it does not confuse programmers.</p>
141+
142+
</recommendation>
143+
<example>
144+
145+
<p>In the example below, the original version of <code>Cart</code> is missing braces. This means
146+
that the code triggers a <code>NullPointerException</code> at runtime if <code>i</code>
147+
is <code>null</code>.</p>
148+
149+
<sample src="UseBraces.java" />
150+
151+
<p>The corrected version of <code>Cart</code> does include braces, so
152+
that the code executes as the indentation suggests.</p>
153+
154+
<sample src="UseBracesGood.java" />
155+
156+
<p>
157+
In the following example the indentation may or may not be misleading depending on your tab width
158+
settings. As such, mixing tabs and spaces in this way is not recommended, since what looks fine in
159+
one context can be very misleading in another.
160+
</p>
161+
162+
<sample src="UseBraces2.java" />
163+
164+
<p>
165+
If you mix tabs and spaces in this way, then you might get seemingly false positives, since your
166+
tab width settings cannot be taken into account.
167+
</p>
168+
169+
</example>
170+
<references>
171+
172+
<li>
173+
Java SE Documentation:
174+
<a href="http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-142311.html#15395">Compound Statements</a>.
175+
</li>
176+
<li>
177+
Wikipedia:
178+
<a href="https://en.wikipedia.org/wiki/Indent_style">Indent style</a>.
179+
</li>
180+
181+
</references>
182+
</qhelp>
183+
```

0 commit comments

Comments
 (0)