Cypher styleguide
The purpose of the Cypher® styleguide is to make queries as easy to read as possible.
For rules and recommendations for naming of labels, relationship types and properties, please see the Naming rules and recommendations.
General recommendations
-
When using Cypher language constructs in prose, use a
monospaced
font and follow the styling rules. -
When referring to labels and relationship types, the colon should be included as follows:
:Label
,:REL_TYPE
. -
When referring to functions, use lower camel case and parentheses. For example:
toString()
. -
If you are storing Cypher statements in a separate file, use the file extension
.cypher
.
Indentation and line breaks
-
Start a new clause on a new line.
MATCH (n) WHERE n.name CONTAINS 's' RETURN n.name
MATCH (n)
WHERE n.name CONTAINS 's'
RETURN n.name
-
Indent
ON CREATE
andON MATCH
with two spaces. PutON CREATE
beforeON MATCH
if both are present.
MERGE (n) ON CREATE SET n.prop = 0
MERGE (a:A)-[:T]->(b:B)
ON MATCH SET b.name = 'you'
ON CREATE SET a.name = 'me'
RETURN a.prop
MERGE (n)
ON CREATE SET n.prop = 0
MERGE (a:A)-[:T]->(b:B)
ON CREATE SET a.name = 'me'
ON MATCH SET b.name = 'you'
RETURN a.prop
-
Start a subquery on a new line after the opening brace, indented with two (additional) spaces. Leave the closing brace on its own line.
MATCH (a:A)
WHERE
EXISTS { MATCH (a)-->(b:B) WHERE b.prop = 'yellow' }
RETURN a.foo
MATCH (a:A)
WHERE EXISTS
{MATCH (a)-->(b:B)
WHERE b.prop = 'yellow'}
RETURN a.foo
MATCH (a:A)
WHERE EXISTS {
MATCH (a)-->(b:B)
WHERE b.prop = 'yellow'
}
RETURN a.foo
-
Do not break the line if the simplified subquery form is used.
MATCH (a:A)
WHERE EXISTS {
(a)-->(b:B)
}
RETURN a.prop
MATCH (a:A)
WHERE EXISTS { (a)-->(b:B) }
RETURN a.prop
Casing
-
Write keywords in upper case.
match (p:Person)
where p.name starts with 'Ma'
return p.name
MATCH (p:Person)
WHERE p.name STARTS WITH 'Ma'
RETURN p.name
-
Write the value
null
in lower case.
WITH NULL AS n1, Null AS n2
RETURN n1 IS NULL AND n2 IS NOT NULL
WITH null AS n1, null AS n2
RETURN n1 IS NULL AND n2 IS NOT NULL
-
Write
BOOLEAN
literals (true
andfalse
) in lower case.
WITH TRUE AS b1, False AS b2
RETURN b1 AND b2
WITH true AS b1, false AS b2
RETURN b1 AND b2
-
Use camel case, starting with a lower-case character, for:
-
functions
-
properties
-
variables
-
parameters
-
CREATE (N:Label {Prop: 0})
WITH N, RAND() AS Rand, $pArAm AS MAP
RETURN Rand, MAP.property_key, count(N)
CREATE (n:Label {prop: 0})
WITH n, rand() AS rand, $param AS map
RETURN rand, map.propertyKey, count(n)
Spacing
-
For literal maps:
-
No space between the opening brace and the first key
-
No space between key and colon
-
One space between colon and value
-
No space between value and comma
-
One space between comma and next key
-
No space between the last value and the closing brace
-
WITH { key1 :'value' ,key2 : 42 } AS map
RETURN map
WITH {key1: 'value', key2: 42} AS map
RETURN map
-
One space between label/type predicates and property predicates in patterns.
MATCH (p:Person{property: -1})-[:KNOWS {since: 2016}]->()
RETURN p.name
MATCH (p:Person {property: -1})-[:KNOWS {since: 2016}]->()
RETURN p.name
-
No space in patterns.
MATCH (:Person) --> (:Vehicle)
RETURN count(*)
MATCH (:Person)-->(:Vehicle)
RETURN count(*)
-
Use a wrapping space around operators.
MATCH p=(s)-->(e)
WHERE s.name<>e.name
RETURN length(p)
MATCH p = (s)-->(e)
WHERE s.name <> e.name
RETURN length(p)
-
No space in label predicates.
MATCH (person : Person : Owner )
RETURN person.name
MATCH (person:Person:Owner)
RETURN person.name
-
Use a space after each comma in lists and enumerations.
MATCH (),()
WITH ['a','b',3.14] AS list
RETURN list,2,3,4
MATCH (), ()
WITH ['a', 'b', 3.14] AS list
RETURN list, 2, 3, 4
-
No padding space within function call parentheses.
RETURN split( 'original', 'i' )
RETURN split('original', 'i')
-
Use padding space within simple subquery expressions.
MATCH (a:A)
WHERE EXISTS {(a)-->(b:B)}
RETURN a.prop
MATCH (a:A)
WHERE EXISTS { (a)-->(b:B) }
RETURN a.prop
Patterns
-
When patterns wrap lines, break after arrows, not before.
MATCH (:Person)-->(vehicle:Car)-->(:Company)
<--(:Country)
RETURN count(vehicle)
MATCH (:Person)-->(vehicle:Car)-->(:Company)<--
(:Country)
RETURN count(vehicle)
-
Use anonymous nodes and relationships when the variable would not be used.
MATCH (kate:Person {name: 'Kate'})-[r:LIKES]-(c:Car)
RETURN c.type
MATCH (:Person {name: 'Kate'})-[:LIKES]-(c:Car)
RETURN c.type
-
Chain patterns together to avoid repeating variables.
MATCH (:Person)-->(vehicle:Car), (vehicle:Car)-->(:Company)
RETURN count(vehicle)
MATCH (:Person)-->(vehicle:Car)-->(:Company)
RETURN count(vehicle)
-
Put named nodes before anonymous nodes.
MATCH ()-->(vehicle:Car)-->(manufacturer:Company)
WHERE manufacturer.foundedYear < 2000
RETURN vehicle.mileage
MATCH (manufacturer:Company)<--(vehicle:Car)<--()
WHERE manufacturer.foundedYear < 2000
RETURN vehicle.mileage
-
Keep anchor nodes at the beginning of the
MATCH
clause.
MATCH (:Person)-->(vehicle:Car)-->(manufacturer:Company)
WHERE manufacturer.foundedYear < 2000
RETURN vehicle.mileage
MATCH (manufacturer:Company)<--(vehicle:Car)<--(:Person)
WHERE manufacturer.foundedYear < 2000
RETURN vehicle.mileage
-
Prefer outgoing (left to right) pattern relationships to incoming pattern relationships.
MATCH (:Country)-->(:Company)<--(vehicle:Car)<--(:Person)
RETURN vehicle.mileage
MATCH (:Person)-->(vehicle:Car)-->(:Company)<--(:Country)
RETURN vehicle.mileage
Meta-characters
-
Use single quotes,
'
, for literalSTRING
values.
RETURN "Cypher"
RETURN 'Cypher'
-
Disregard this rule for literal
STRING
values that contain a single quote character. If theSTRING
has both, use the form that creates the fewest escapes. In the case of a tie, prefer single quotes.
RETURN 'Cypher\'s a nice language', "Mats' quote: \"statement\""
RETURN "Cypher's a nice language", 'Mats\' quote: "statement"'
-
Avoid using characters and keywords that require the input to be quoted with backticks.
MATCH (`odd-ch@racter$`:`Spaced Label` {`&property`: 42})
RETURN labels(`odd-ch@racter$`)
MATCH (node:NonSpacedLabel {property: 42})
RETURN labels(node)
-
Do not use a semicolon at the end of the statement.
RETURN 1;
RETURN 1