Skip to content

Commit 995af2c

Browse files
author
Charles S. Koppelman-Milstein
committed
Added lots more
1 parent a56a784 commit 995af2c

File tree

1 file changed

+46
-5
lines changed

1 file changed

+46
-5
lines changed

Apex style guide.md

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,64 @@ Open braces should have a space before them and not a newline. The matching clo
4242

4343
`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.
4444

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+
4553
E.g.:
4654
```
4755
public void foo(Integer bar) {
48-
if(bar == 3) {
56+
if (bar == 3) {
57+
System.debug(debugCode(bar) + ' - hi there!');
4958
return;
50-
} else if(bar > 7) {
59+
} else if (bar > 7) {
5160
List<Integer> wasteOfSpace = new List<Integer>();
5261
do {
5362
wasteOfSpace.add(8);
54-
} while(wasteOfSpace.size < 5);
63+
} while (wasteOfSpace.size < 5);
5564
} else {
56-
upsert v;
65+
try {
66+
upsert v;
67+
} catch (Exception ex) {
68+
handleException(ex);
69+
}
5770
}
5871
}
5972
```
6073

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.
89+
90+
Example (in context):
91+
92+
```
93+
String typeToSelect = 'abcde';
94+
List<Contact> cnts = [SELECT Id, FirstName, LastName, Phone, Email,
95+
MailingCity, MailingState,
96+
(SELECT Id, ActivityDate, Origin, Type,
97+
WhatId, What.Name, RecordTypeId
98+
FROM ActivityHistories
99+
WHERE Type = :typeToSelect)
100+
FROM Contact
101+
WHERE CreatedDate = TODAY];
102+
```
62103

63104

64105

0 commit comments

Comments
 (0)