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: Apex style guide.md
+46-5Lines changed: 46 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -42,23 +42,64 @@ Open braces should have a space before them and not a newline. The matching clo
42
42
43
43
`else`s and `else if`s do not get a newline before them. Neither do `catch`es or `while`s in a `do...while` loop.
44
44
45
+
The parenthetical clause in `if`, `while`, `do`, `catch`, etc., statements should be preceded and followed by a single space.
46
+
47
+
In method definitions, there should be no space before the open parenthesis, and one space after.
48
+
49
+
In method calls and definitions, there should not be whitespace between the name of the method and the open parenthesis.
50
+
51
+
A single space should separate binary operators from the surrounding elements (e.g., `+`, `||`). Unary operators (`!`, `-`) should be attached to their parameters.
52
+
45
53
E.g.:
46
54
```
47
55
public void foo(Integer bar) {
48
-
if(bar == 3) {
56
+
if (bar == 3) {
57
+
System.debug(debugCode(bar) + ' - hi there!');
49
58
return;
50
-
} else if(bar > 7) {
59
+
} else if(bar > 7) {
51
60
List<Integer> wasteOfSpace = new List<Integer>();
52
61
do {
53
62
wasteOfSpace.add(8);
54
-
} while(wasteOfSpace.size < 5);
63
+
} while(wasteOfSpace.size < 5);
55
64
} else {
56
-
upsert v;
65
+
try {
66
+
upsert v;
67
+
} catch (Exception ex) {
68
+
handleException(ex);
69
+
}
57
70
}
58
71
}
59
72
```
60
73
61
-
All `if`, `while` and `for` statements must use braces even if they control just one statement.
74
+
### Capitalization ###
75
+
76
+
We follow the Java standard of capitalization with the listed exceptions. That means that statements (`for`, `if`, etc.) should be lowercase, constants should be `UPPER_CASE_WITH_UNDERSCORES`, classes and class-level variables should be declared as `UpperCamelCase`, and methods, parameters and local variables should all be declard as `lowerCamelCase`.
77
+
78
+
Native Apex methods and classes should generally be referenced as written in official Salesforce documentation. This means that schemas and classes are `UpperCamelCase` and methods are `lowerCamelCase`. The only deviation from this rule is `SObject` which should be written as such (in the documentation, it is usually written `sObject` which does not conform to this style guide and should not be used).
79
+
80
+
However, when referencing any metadata (SObject, SObjectField, FieldSet, Action, Class, Page, etc.), use the declared capitalization. When referencing a method, field, etc., that is not capitalized according to these rules, use the declared capitalization.
81
+
82
+
##SOQL##
83
+
84
+
In general, SOQL should be declared inline where it is used. In some cases, like when referencing FieldSets, it's necessary to build SOQL queries dynamically. The same rules will generally apply.
85
+
86
+
SOQL keywords (e.g., `SELECT`, `WHERE`, `TODAY`) should always be written in `ALL CAPS`. Objects, fields and bind variables should be referenced as declared. Each clause of the SOQL Query should be on its own line so that finding what changed in a diff is easier. That is, each `SELECT`, `FROM`, `WHERE`, `AND`, `OR`, `GROUP BY`, `HAVING`, `ROLL UP`, `ORDER BY`, etc., with the exception of the first `SELECT` should start a new line.
87
+
88
+
Long lists of fields in a `SELECT` clause should be ordered in a logical manner and broken to fit within page width, with subsequent lines aligned with the first field. Always select `Id` first.
0 commit comments