The purpose of the style guide is to make the code as easy to read as possible, which helps reduce maintenance costs. Rules and recommendations for label, relationship type, and attribute naming are as follows:
1. General advice
- When using Cypher language constructs, use monospaced fonts and follow style rules.
- When referring to labels and relation types, colons should include the following: :Label, :REL_TYPE.
- When referencing a function, use lower camel case, and parentheses should be used as follows: shortestPath(). Parameters should generally not be included.
- If you store Cypher statements in a separate file, use the file extension .cypher.
2. Indentation and line breaks
start a new clause on a new line
Bad Cypher
MATCH (n) WHERE n.name CONTAINS 's' RETURN n.name
Good Cypher
MATCH (n)
WHERE n.name CONTAINS 's'
RETURN n.name
Indent ON CREATE and ON MATCH with two spaces. If both are present, place ON CREATE before ON MATCH.
Bad Cypher
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
Good Cypher
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 by two extra spaces, and the closing brace on a separate line.
Bad Cypher
MATCH (a:A)
WHERE
EXISTS { MATCH (a)-->(b:B) WHERE b.prop = $param }
RETURN a.foo
Also bad Cypher
MATCH (a:A)
WHERE EXISTS
{MATCH (a)-->(b:B)
WHERE b.prop = $param}
RETURN a.foo
Good Cypher
MATCH (a:A)
WHERE EXISTS {
MATCH (a)-->(b:B)
WHERE b.prop = $param
}
RETURN a.foo
If using a simplified subquery form, do not wrap
Bad Cypher
MATCH (a:A)
WHERE EXISTS {
(a)-->(b:B)
}
RETURN a.prop
Good Cypher
MATCH (a:A)
WHERE EXISTS { (a)-->(b:B) }
RETURN a.prop
3. Capitalization
Keyword capitalization
Bad Cypher
match (p:Person)
where p.name starts with 'Ma'
return p.name
Good Cypher
MATCH (p:Person)
WHERE p.name STARTS WITH 'Ma'
RETURN p.name
lowercase null values
Bad Cypher
WITH NULL AS n1, Null AS n2
RETURN n1 IS NULL AND n2 IS NOT NULL
Good Cypher
WITH null AS n1, null as n2
RETURN n1 IS NULL AND n2 IS NOT NULL
boolean ( true
and false
) lowercase
Bad Cypher
WITH TRUE AS b1, False AS b2
RETURN b1 AND b2
Good Cypher
WITH true AS b1, false AS b2
RETURN b1 AND b2
Use camel case for functions, properties, variables and parameters starting with a lowercase letter
Bad Cypher
CREATE (N {Prop: 0})
WITH RAND() AS Rand, $pArAm AS MAP
RETURN Rand, MAP.property_key, Count(N)
Good Cypher
CREATE (n {prop: 0})
WITH rand() AS rand, $param AS map
RETURN rand, map.propertyKey, count(n)
4. Space
space in mapping
- There is no space between the opening brace and the first key
- No space between key and colon
- There is a space between the colon and the value
- No space between value and comma
- There is a space between the comma and the next key
- There is no space between the last value and the closing brace
Bad Cypher
WITH { key1 :'value' ,key2 : 42 } AS map
RETURN map
Good Cypher
WITH {key1: 'value', key2: 42} AS map
RETURN map
A space between label/type predicate and attribute predicate
Bad Cypher
MATCH (p:Person{property: -1})-[:KNOWS {since: 2016}]->()
RETURN p.name
Good Cypher
MATCH (p:Person {property: -1})-[:KNOWS {since: 2016}]->()
RETURN p.name
No spaces between patterns
Bad Cypher
MATCH (:Person) --> (:Vehicle)
RETURN count(*)
Good Cypher
MATCH (:Person)-->(:Vehicle)
RETURN count(*)
Use spaces around operators
Bad Cypher
MATCH p=(s)-->(e)
WHERE s.name<>e.name
RETURN length(p)
Good Cypher
MATCH p = (s)-->(e)
WHERE s.name <> e.name
RETURN length(p)
No spaces in label predicates
Bad Cypher
MATCH (person : Person : Owner )
RETURN person.name
Good Cypher
MATCH (person:Person:Owner)
RETURN person.name
Use a space after each comma in lists and enumerations
Bad Cypher
MATCH (),()
WITH ['a','b',3.14] AS list
RETURN list,2,3,4
Good Cypher
MATCH (), ()
WITH ['a', 'b', 3.14] AS list
RETURN list, 2, 3, 4
No spaces around function call parentheses
Bad Cypher
RETURN split( 'original', 'i' )
Good Cypher
RETURN split('original', 'i')
Use spaces in simple subquery expressions
Bad Cypher
MATCH (a:A)
WHERE EXISTS {(a)-->(b:B)}
RETURN a.prop
Good Cypher
MATCH (a:A)
WHERE EXISTS { (a)-->(b:B) }
RETURN a.prop
5. Patterns
Wrap line after arrow when matching model requires line break
Bad Cypher
MATCH (:Person)-->(vehicle:Car)-->(:Company)
<--(:Country)
RETURN count(vehicle)
Good Cypher
MATCH (:Person)-->(vehicle:Car)-->(:Company)<--
(:Country)
RETURN count(vehicle)
Use anonymous for subsequent unused nodes and relationships
Bad Cypher
CREATE (a:End {prop: 42}),
(b:End {prop: 3}),
(c:Begin {prop: id(a)})
Good Cypher
CREATE (a:End {prop: 42}),
(:End {prop: 3}),
(:Begin {prop: id(a)})
try to connect patterns together
Bad Cypher
MATCH (:Person)-->(vehicle:Car), (vehicle:Car)-->(:Company)
RETURN count(vehicle)
Good Cypher
MATCH (:Person)-->(vehicle:Car)-->(:Company)
RETURN count(vehicle)
Put the named node before the named node
Bad Cypher
MATCH ()-->(vehicle:Car)-->(manufacturer:Company)
WHERE manufacturer.foundedYear < 2000
RETURN vehicle.mileage
Good Cypher
MATCH (manufacturer:Company)<--(vehicle:Car)<--()
WHERE manufacturer.foundedYear < 2000
RETURN vehicle.mileage
Try to have named nodes after MATCH
Bad Cypher
MATCH (:Person)-->(vehicle:Car)-->(manufacturer:Company)
WHERE manufacturer.foundedYear < 2000
RETURN vehicle.mileage
Good Cypher
MATCH (manufacturer:Company)<--(vehicle:Car)<--(:Person)
WHERE manufacturer.foundedYear < 2000
RETURN vehicle.mileage
Try to use left-to-right schema relationships
Bad Cypher
MATCH (:Country)-->(:Company)<--(vehicle:Car)<--(:Person)
RETURN vehicle.mileage
Good Cypher
MATCH (:Person)-->(vehicle:Car)-->(:Company)<--(:Country)
RETURN vehicle.mileage
6. Metacharacters
Try to use single quotes ' to return the value
Bad Cypher
RETURN "Cypher"
Good Cypher
RETURN 'Cypher'
For literal strings containing single quote characters, ignore the above rules. If the string has both, use the form that creates the least escaping.
Bad Cypher
RETURN 'Cypher\'s a nice language', "Mats' quote: \"statement\""
Good Cypher
RETURN "Cypher's a nice language", 'Mats\' quote: "statement"'
Avoid backticks to escape characters and keywords
Bad Cypher
MATCH (`odd-ch@racter$`:`Spaced Label` {`&property`: 42})
RETURN labels(`odd-ch@racter$`)
Good Cypher
MATCH (node:NonSpacedLabel {property: 42})
RETURN labels(node)
Do not use semicolons at the end of statements
Bad Cypher
RETURN 1;
Good Cypher
RETURN 1