Skip to content

Commit 270c9b0

Browse files
author
Charles S. Koppelman-Milstein
committed
Added more
1 parent a6d1df2 commit 270c9b0

File tree

1 file changed

+74
-23
lines changed

1 file changed

+74
-23
lines changed

Apex style guide.md

Lines changed: 74 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,38 +38,36 @@ What is important is that each class order its members in some logical order, wh
3838
###Indentation###
3939
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.
4040

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+
4146
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.
4247

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.
4449

4550
The parenthetical clause in `if`, `while`, `do`, `catch`, etc., statements should be preceded and followed by a single space.
4651

4752
In method definitions, there should be no space before the open parenthesis, and one space after.
4853

4954
In method calls and definitions, there should not be whitespace between the name of the method and the open parenthesis.
5055

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.
5257

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.
7371

7472
### Capitalization ###
7573

@@ -79,11 +77,64 @@ Native Apex methods and classes should generally be referenced as written in off
7977

8078
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.
8179

80+
### Example ###
81+
82+
```
83+
public class MyClass {
84+
85+
private Contact internallyUsedContact { get; set; }
86+
87+
public Integer calculatedInteger {
88+
get {
89+
return 5;
90+
}
91+
set {
92+
this.internallyUsedContact = [SELECT Id
93+
FROM Contact
94+
WHERE Number_of_Peanuts__c > :value
95+
LIMIT 1];
96+
}
97+
}
98+
99+
private Id contactId {
100+
get;
101+
set {
102+
System.debug('Why do this?');
103+
this.contactId = value;
104+
}
105+
}
106+
107+
public void foo(Integer bar) {
108+
if (bar == 3) {
109+
System.debug(debugCode(bar) + ' - hi there!');
110+
return;
111+
} else if (bar > 7) {
112+
List<Integer> wasteOfSpace = new List<Integer>();
113+
do {
114+
wasteOfSpace.add(this.calculatedInteger);
115+
} while (wasteOfSpace.size < 5);
116+
} else {
117+
try {
118+
upsert v;
119+
} catch (Exception ex) {
120+
handleException(ex);
121+
}
122+
}
123+
124+
for (Integer i : wasteOfSpace) {
125+
System.debug('Here\'s an integer! ' + i);
126+
}
127+
}
128+
129+
}
130+
```
131+
132+
82133
##SOQL##
83134

84135
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.
85136

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`.
87138

88139
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.
89140

0 commit comments

Comments
 (0)