Skip to content

Commit deb76e5

Browse files
committed
update styleguide with cypherfmt rules
1 parent 1cf2913 commit deb76e5

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed

modules/ROOT/pages/styleguide.adoc

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ The purpose of the Cypher styleguide is to make queries as easy to read as possi
99

1010
For rules and recommendations for naming of labels, relationship types and properties, please see the xref::syntax/naming.adoc[Naming rules and recommendations].
1111

12+
The best way to make sure your queries follow the styling rules is to use `cypherfmt`, the official Cypher formatter.
13+
It is available as part of the https://marketplace.visualstudio.com/items?itemName=neo4j-extensions.neo4j-for-vscode[Neo4j VS Code Extension],
14+
and as a command line tool through the https://www.npmjs.com/package/@neo4j-cypher/language-support[Neo4j Cypher Language Support npm package].
15+
16+
17+
1218
[[cypher-styleguide-general-recommendations]]
1319
== General recommendations
1420

@@ -115,6 +121,123 @@ WHERE EXISTS { (a)-->(b:B) }
115121
RETURN a.prop
116122
----
117123

124+
* Limit line length to 80 characters if possible. Line breaks should be applied starting from the outermost group, with each nested group breaking
125+
until either the line fits or no more line breaks can be inserted. Add two spaces of indentation for each nested broken group.
126+
127+
.Bad
128+
[source, cypher]
129+
----
130+
MATCH (n)
131+
WHERE n.prop <> 'a' AND n.prop <> 'b' AND n.prop <> 'c' AND n.prop <> 'd' AND n.prop <> 'e'
132+
RETURN n
133+
----
134+
135+
.Good
136+
[source, cypher]
137+
----
138+
MATCH (n)
139+
WHERE
140+
n.prop <> 'a' AND
141+
n.prop <> 'b' AND
142+
n.prop <> 'c' AND
143+
n.prop <> 'd' AND
144+
n.prop <> 'e'
145+
RETURN n
146+
----
147+
148+
* Start ORDER BY and LIMIT clauses on new lines.
149+
150+
.Bad
151+
[source, cypher]
152+
----
153+
MATCH (n)
154+
RETURN 5 ORDER BY n.prop LIMIT 10
155+
----
156+
157+
.Good
158+
[source, cypher]
159+
----
160+
MATCH (n)
161+
RETURN 5
162+
ORDER BY n.prop
163+
LIMIT 10
164+
----
165+
166+
* A group containing a CASE expression should always split, and each WHEN, ELSE, and END should be put on a new line. Additionally, WHEN and ELSE should add two spaces of indentation.
167+
168+
.Bad
169+
[source, cypher]
170+
----
171+
MATCH (n:Person {name: 'Alice'})
172+
RETURN CASE WHEN n.age >= 18 THEN 'Adult' ELSE 'Minor' END AS ageGroup
173+
----
174+
175+
.Good
176+
[source, cypher]
177+
----
178+
MATCH (n:Person {name: 'Alice'})
179+
RETURN
180+
CASE
181+
WHEN n.age >= 18 THEN 'Adult'
182+
ELSE 'Minor'
183+
END AS ageGroup-
184+
----
185+
186+
* Lines that end with a list or map literal and do not fit within the maximum width should leave
187+
the opening bracket/brace on the previous line and use only two additional spaces of indentation.
188+
189+
.Bad
190+
[source, cypher]
191+
----
192+
RETURN
193+
[
194+
"Alice",
195+
"Bob",
196+
"Charlie",
197+
"David",
198+
"Eve",
199+
"Frank",
200+
"Grace",
201+
"Heidi",
202+
"Ivan",
203+
"Judy"
204+
]
205+
----
206+
207+
.Good
208+
[source, cypher]
209+
----
210+
RETURN [
211+
"Alice",
212+
"Bob",
213+
"Charlie",
214+
"David",
215+
"Eve",
216+
"Frank",
217+
"Grace",
218+
"Heidi",
219+
"Ivan",
220+
"Judy"
221+
]
222+
----
223+
224+
* A single blank line may be used to separate clauses, queries or comments.
225+
226+
.Good
227+
[source, cypher]
228+
----
229+
MATCH (n)--[r]->(m)
230+
RETURN n, r, m
231+
----
232+
233+
.Also good
234+
[source, cypher]
235+
----
236+
MATCH (n)--[r]->(m)
237+
238+
RETURN n, r, m
239+
----
240+
118241
[[cypher-styleguide-casing]]
119242
== Casing
120243

0 commit comments

Comments
 (0)