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
+74-23Lines changed: 74 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -38,38 +38,36 @@ What is important is that each class order its members in some logical order, wh
38
38
###Indentation###
39
39
All blocks of code should be indented with 2 spaces. Spaces, not tabs, to ensure that it looks the same on everyone's screen and doesn't waste horizontal space.
40
40
41
+
###New-lines and spaces###
42
+
Use vertical whitespace as appropriate. Don't be afraid to separate blocks of code.
43
+
44
+
Prefer placing comments on a line by themselves.
45
+
41
46
Open braces should have a space before them and not a newline. The matching close brace should line up with the start of the opening brace's line.
42
47
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.
48
+
`else`s and `else if`s do not get a new-line before them. Neither do `catch`es or `while`s in a `do...while` loop.
44
49
45
50
The parenthetical clause in `if`, `while`, `do`, `catch`, etc., statements should be preceded and followed by a single space.
46
51
47
52
In method definitions, there should be no space before the open parenthesis, and one space after.
48
53
49
54
In method calls and definitions, there should not be whitespace between the name of the method and the open parenthesis.
50
55
51
-
A single space should separate binary operators from the surrounding elements (e.g., `+`, `||`). Unary operators (`!`, `-`) should be attached to their parameters.
56
+
A single space should separate binary operators from the surrounding elements (e.g., `+`, `||`, `=`). Unary operators (`!`, `-`) should be attached to their parameters. A colon inside a `for each` loop (e.g., `for (Contact cnt : contacts) {`) should have one space on either side.
52
57
53
-
E.g.:
54
-
```
55
-
public void foo(Integer bar) {
56
-
if (bar == 3) {
57
-
System.debug(debugCode(bar) + ' - hi there!');
58
-
return;
59
-
} else if (bar > 7) {
60
-
List<Integer> wasteOfSpace = new List<Integer>();
61
-
do {
62
-
wasteOfSpace.add(8);
63
-
} while (wasteOfSpace.size < 5);
64
-
} else {
65
-
try {
66
-
upsert v;
67
-
} catch (Exception ex) {
68
-
handleException(ex);
69
-
}
70
-
}
71
-
}
72
-
```
58
+
If using C#-style properties, code should follow the following rules:
59
+
60
+
* Always declare the getter, then the setter.
61
+
* If there is no logic, it should read `{ get; set; }`.
62
+
* If there is logic, there should be a new-line before each open-brace, and before and after each closed-brace.
63
+
* If one clause has logic and one does not, place the clause without logic on its own line.
64
+
65
+
### Prefer Explicit Declarations ###
66
+
Always specify:
67
+
68
+
* public/private modifiers
69
+
* with/without sharing
70
+
*`this` when calling local methods or setting local members/properties.
73
71
74
72
### Capitalization ###
75
73
@@ -79,11 +77,64 @@ Native Apex methods and classes should generally be referenced as written in off
79
77
80
78
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.
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
136
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.
137
+
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. That line should start in the same column as the most relevant `SELECT`.
87
138
88
139
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