Skip to content

Commit e6e4502

Browse files
authored
Merge pull request #148 from aschackmull/docs/ql-style-guide
QL style guide: Clarify some outstanding issues
2 parents 3e022ad + 9984647 commit e6e4502

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

docs/ql-style-guide.md

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@ select c, "This call to '$@' is deprecated because " + reason + ".",
5151
1. There *should not* be additional blank lines within a predicate.
5252
1. There *may* be a new line:
5353
- Immediately after the `from`, `where` or `select` keywords in a query.
54-
- Immediately after `if`, `then`, or `else` keywords. The `then` and `else` parts *should* be consistent.
54+
- Immediately after `if`, `then`, or `else` keywords.
5555
1. *Avoid* other line breaks in declarations, other than to break long lines.
5656
1. When operands of *binary operators* span two lines, the operator *should* be placed at the end of the first line.
57+
1. If the parameter list needs to be broken across multiple lines then there *must* be a line break after the opening `(`, the parameter declarations indented one level, and the `) {` *must* be on its own line at the outer indentation.
5758

5859
### Examples
5960

@@ -65,9 +66,11 @@ private int getNumberOfParameters() {
6566
```
6667

6768
```ql
68-
predicate methodStats(string qualifiedName, string name,
69-
int numberOfParameters, int numberOfStatements, int numberOfExpressions,
70-
int linesOfCode, int nestingDepth, int numberOfBranches) {
69+
predicate methodStats(
70+
string qualifiedName, string name, int numberOfParameters,
71+
int numberOfStatements, int numberOfExpressions, int linesOfCode,
72+
int nestingDepth, int numberOfBranches
73+
) {
7174
...
7275
}
7376
```
@@ -93,21 +96,24 @@ select main, "Main method has no parameters."
9396
```
9497

9598
```ql
96-
if x.isPublic() then
99+
if
100+
x.isPublic()
101+
then
97102
result = "public"
98103
else
99104
result = "private"
100105
```
101106

102107
```ql
103-
if
104-
x.isPublic()
105-
then
106-
result = "public"
108+
if x.isPublic()
109+
then result = "public"
107110
else
108-
result = "private"
111+
if x.isPrivate()
112+
then result = "private"
113+
else result = "protected"
109114
```
110115

116+
111117
## Braces
112118
1. Braces follow [Stroustrup](https://en.wikipedia.org/wiki/Indentation_style#Variant:_Stroustrup) style. The opening `{` *must* be placed at the end of the preceding line.
113119
1. The closing `}` *must* be placed on its own line, indented to the outer level, or be on the same line as the opening `{`.
@@ -285,22 +291,27 @@ deprecated Expr getInitializer()
285291
1. *Prefer* one *conjunct* per line.
286292
1. Write the `and` at the end of the line. This also applies in `where` clauses.
287293
1. *Prefer* to write the `or` keyword on its own line.
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.
294+
1. The `or` keyword *may* be written at the end of a line, or within a line, provided that it has no `and` operands.
289295
1. Single-line formulas *may* be used in order to save space or add clarity, particularly in the *body* of a *quantifier/aggregation*.
290296
1. *Always* use brackets to clarify the precedence of:
291297
- `implies`
292298
- `if`-`then`-`else`
299+
1. *Avoid* using brackets to clarify the precedence of:
300+
- `not`
301+
- `and`
302+
- `or`
293303
1. Parenthesised formulas *can* be written:
294304
- Within a single line. There *should not* be an additional space following the opening parenthesis or preceding the closing parenthesis.
295305
- 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.
296306
1. *Quantifiers/aggregations* *can* be written:
297307
- Within a single line. In this case, there is no space to the inside of the parentheses, or after the quantifier keyword.
298-
- Across multiple lines. In this case, type declarations are on the same line as the quantifier, the `|` *may* be at the end of the line, or *may* be on its own line, and the body of the quantifier *must* be indented one level. The closing `)` is written on a new line, at the outer indentation.
308+
- Across multiple lines. In this case, type declarations are on the same line as the quantifier with the first `|` at the same line as the quantifier, the second `|` *must* be at the end of the same line as the quantifier or on its own line at the outer indentation, and the body of the quantifier *must* be indented one level. The closing `)` is written on a new line, at the outer indentation. If the type declarations need to be broken across multiple lines then there must *must* be a line break after the opening `(`, the type declarations indented one level, and the first `|` on its own line at the outer indentation.
299309
1. `if`-`then`-`else` *can* be written:
300310
- On a single line
301311
- With the *body* after the `if`/`then`/`else` keyword
302312
- With the *body* indented on the next line
303313
- *Always* parenthesise the `else` part if it is a compound formula.
314+
1. If an `if`-`then`-`else` is broken across multiple lines then the `then` and `else` keywords *should* be at the start of lines aligned with the `if`.
304315
1. The `and` and `else` keywords *may* be placed on the same line as the closing parenthesis.
305316
1. The `and` and `else` keywords *may* be "cuddled": `) else (`
306317
1. *Always* qualify *calls* to predicates of the same class with `this`.
@@ -335,7 +346,8 @@ deprecated Expr getInitializer()
335346
```
336347

337348
```ql
338-
if e1 instanceof BitwiseOrExpr or e1 instanceof LogicalOrExpr then (
349+
if e1 instanceof BitwiseOrExpr or e1 instanceof LogicalOrExpr
350+
then (
339351
impliesSub(e1.(BinaryOperation).getAnOperand(), e2, b1, b2) and
340352
b1 = false
341353
) else (
@@ -360,7 +372,8 @@ deprecated Expr getInitializer()
360372

361373
```ql
362374
exists(Type qualifierType |
363-
this.hasNonExactQualifierType(qualifierType) |
375+
this.hasNonExactQualifierType(qualifierType)
376+
|
364377
result = getANonExactQualifierSubType(qualifierType)
365378
)
366379
```
@@ -389,7 +402,8 @@ deprecated Expr getInitializer()
389402
```
390403

391404
```ql
392-
if exists(this.getContainingType()) then (
405+
if exists(this.getContainingType())
406+
then (
393407
result = "A nested class" and
394408
parentName = this.getContainingType().getFullyQualifiedName()
395409
) else (

0 commit comments

Comments
 (0)