You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/ql-style-guide.md
+56-70Lines changed: 56 additions & 70 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,20 +2,20 @@
2
2
3
3
## Introduction
4
4
5
-
This document describes how to format the QL code in this repository, covering aspects such as layout, white-space, naming and documentation. Adhering to consistent standards makes code easier to read and maintain. Of course, these are only guidelines, and can be overridden as the need arises on a case-by-case basis. Where existing code deviates from these guidelines, prefer consistency with the surrounding code.
5
+
This document describes how to format the QL code you contribute to this repository. It covers aspects such as layout, white-space, naming and documentation. Adhering to consistent standards makes code easier to read and maintain. Of course, these are only guidelines, and can be overridden as the need arises on a case-by-case basis. Where existing code deviates from these guidelines, prefer consistency with the surrounding code.
6
6
7
7
Words in *italic* are defined in the [Glossary](#glossary).
8
8
9
9
## Indentation
10
10
1.*Always* use 2 spaces for indentation.
11
11
1.*Always* indent:
12
12
- the *body* of a module, newtype, class or predicate,
13
-
- the second and subsequent lines after a line break used to split a long line,
13
+
- the second and subsequent lines after you use a line break to split a long line,
14
14
- the *body* of a `from`, `where` or `select` clause where it spans multiple lines,
15
15
- the *body* of a *quantifier* that spans multiple lines.
16
16
17
17
18
-
Examples:
18
+
### Examples
19
19
20
20
```ql
21
21
module Helpers {
@@ -47,15 +47,15 @@ select c, "This call to '$@' is deprecated because " + reason + ".",
47
47
- before each declaration, except for the first declaration in a *body*,
48
48
- before the `from`-`where`-`select` section in a query file.
49
49
1.*Avoid* two or more adjacent blank lines.
50
-
1. There *must* be a new line after the *annotations*`cached`, `deprecated`, `pragma`, `language` and `bindingset`.
50
+
1. There *must* be a new line after the *annotations*`cached`, `deprecated`, `pragma`, `language` and `bindingset`. Other *annotations* do not have a new line.
51
51
1. There *should not* be additional blank lines within a predicate.
52
52
1. There *may* be a new line:
53
53
- immediately after the `from`, `where` or `select` keywords in a query,
54
54
- immediately after `if`, `then`, or `else` keywords. The `then` and `else` parts *should* be consistent.
55
55
1.*Avoid* other line breaks in declarations, other than to break long lines.
56
56
1. When operands of *binary operators* span two lines, the operator *should* be placed at the end of the first line.
57
57
58
-
Examples:
58
+
### Examples
59
59
60
60
```ql
61
61
cached
@@ -78,12 +78,6 @@ where main.getName() = "Main"
78
78
select main, "This is the program entry point."
79
79
```
80
80
81
-
```ql
82
-
from Method main
83
-
where main.getName() = "Main"
84
-
select main, "This is the program entry point."
85
-
```
86
-
87
81
```ql
88
82
from Method main
89
83
where
@@ -98,6 +92,13 @@ select main, "Main method has no parameters."
98
92
else result = "private"
99
93
```
100
94
95
+
```ql
96
+
if x.isPublic() then
97
+
result = "public"
98
+
else
99
+
result = "private"
100
+
```
101
+
101
102
```ql
102
103
if
103
104
x.isPublic()
@@ -113,8 +114,7 @@ select main, "Main method has no parameters."
113
114
1. Braces of empty blocks *may* be placed on a single line, with a single space separating the braces.
114
115
1. Short predicates, not exceeding the maximum line width, *may* be placed on a single line, with a space following the opening brace and preceding the closing brace.
115
116
116
-
117
-
For example,
117
+
### Examples
118
118
119
119
```ql
120
120
class ThrowException extends ThrowExpr {
@@ -146,7 +146,7 @@ class ThrowException extends ThrowExpr {
1.*Avoid* short or single-letter names for classes, predicates and fields.
175
175
1. Short or single letter names for parameters and *quantifiers**may* be used provided that they are sufficiently clear.
176
176
1. Use names as they are used in the target-language specification.
177
+
1. Use American English.
177
178
178
-
Examples:
179
+
### Examples
179
180
180
181
```ql
181
182
/** ... */
@@ -205,16 +206,25 @@ class Type extends ... {
205
206
```
206
207
207
208
## Documentation
209
+
210
+
General requirements:
211
+
208
212
1. Documentation *must* adhere to the [QLDoc specification](https://help.semmle.com/QL/QLDocSpecification.html).
209
-
1. Public declarations *must* be documented.
210
-
1. Non-public declarations *should* be documented.
211
-
1. Declarations in query files *should* be documented.
212
213
1. Use `/** ... */` for documentation, even for single line comments.
213
214
1. For single-line documentation, the `/**` and `*/` are written on the same line as the comment.
214
215
1. For multi-line documentation, the `/**` and `*/` are written on separate lines. There is a `*` preceding each comment line, aligned on the first `*`.
215
-
1. Library files (`.qll` files) *should* be documented with QLDoc.
216
-
1. Query files, except for tests, *must* have a QLDoc query documentation comment.
217
-
1. Documentation *should* be appropriate for users of the library. Put documentation for maintainers in normal comments.
216
+
1. Use full sentences, with capital letters and full stops.
217
+
1. Use American English.
218
+
1. Documentation comments *should* be appropriate for users of the code.
219
+
1. Documentation for maintainers of the code *must* use normal comments.
220
+
221
+
Documenting specific items:
222
+
223
+
1. Public declarations *must* be documented.
224
+
1. Non-public declarations *should* be documented.
225
+
1. Declarations in query files *should* be documented.
226
+
1. Library files (`.qll` files) *should* be have a documentation comment at the top of the file.
227
+
1. Query files, except for tests, *must* have a QLDoc query documentation comment at the top of the file.
218
228
1. Predicates that do not have a result *should* be documented `/** Holds if ... */`
219
229
1. Predicates that have a result *should* be documented `/** Gets ... */`
220
230
1. All predicate parameters *should* be referred to in the predicate documentation.
@@ -224,10 +234,8 @@ class Type extends ... {
224
234
1. Where a class denotes a generic concept with subclasses, list those subclasses.
225
235
1. Declarations that are deprecated *should* be documented as `DEPRECATED: ...`
226
236
1. Declarations that are for internal use *should* be documented as `INTERNAL: Do not use`.
227
-
1. Use full sentences, with capital letters and full stops.
228
237
229
-
230
-
Examples:
238
+
### Examples
231
239
232
240
```ql
233
241
/** Provides logic for determining constant expressions. */
1. Write the `and` at the end of the line. This also applies in `where` clauses.
279
287
1.*Prefer* to write the `or` keyword on its own line.
280
288
1. The `or` keyword *may* be written at the end of a line, or within a line, provided that it has no unparenthesised `and` operands.
281
-
1. Single-line formulas *may* be used in order to save space or add clarity, particularly in the *body* of a *quantifier/aggregation*.
282
-
1.*Avoid* additional brackets to clarify the precedence of`not`, `and` and `or`.
283
-
1.*Always* clarify the precedence of `implies` with brackets.
284
-
1.*Always* clarify the precedence of `if`-`then`-`else` with brackets.
289
+
1. Single-line formulas *may* be used in order to save space or add clarity, particularly in the *body* of a *quantifier/aggregation*.
290
+
1.*Always* use brackets to clarify the precedence of:
291
+
-`implies`,
292
+
-`if`-`then`-`else`.
285
293
1. Parenthesised formulas *can* be written:
286
294
- within a single line. There *should not* be an additional space following the opening parenthesis or preceding the closing parenthesis.
287
295
- spanning multiple lines. The opening parenthesis *should* be placed at the end of the preceding line, the body should be indented one level, and the closing bracket should be placed on a new line at the outer indentation.
An additional specifier used to modify a declaration, such as `private`, `override`, `deprecated`, `pragma`, `bindingset`, or `cached`. |
408
+
|*body*| The text inside `{ }`, `( )`, or each section of an `if`-`then`-`else` or `from`-`where`-`select`. |
409
+
|*binary operator*| An operator with two operands, such as comparison operators, `and`, `or`, `implies`, or arithmetic operators. |
410
+
|*call*| A *formula* that invokes a predicate, e.g. `this.isStatic()` or `calls(a,b)`. |
411
+
|*[conjunct](https://help.semmle.com/QL/QLLanguageSpecification.html#conjunctions)*| A formula that is an operand to an `and`. |
412
+
|*declaration*| A class, module, predicate, field or newtype. |
413
+
|*[disjunct](https://help.semmle.com/QL/QLLanguageSpecification.html#disjunctions)*| A formula that is an operand to an `or`. |
414
+
|*[formula](https://help.semmle.com/QL/QLLanguageSpecification.html#formulas)*| A logical expression, such as `A = B`, a *call*, a *quantifier*, `and`, `or`, `not`, 'in` or `instanceof`. |
415
+
|*should/should not/avoid/prefer*| Adhere to this rule wherever possible, where it makes sense. |
416
+
|*may/can*| This is a reasonable alternative, to be used with discretion. |
417
+
|*must/always/do not*| Always adhere to this rule. |
418
+
|*[quantifier/aggregation](https://help.semmle.com/QL/QLLanguageSpecification.html#aggregations)*|`exists`, `count`, `strictcount`, `any`, `forall`, `forex` and so on. |
419
+
|*variable*| A parameter to a predicate, a field, a from variable, or a variable introduced by a *quantifier* or *aggregation*. |
0 commit comments