Skip to content

Commit e0fd036

Browse files
author
Charles S. Koppelman-Milstein
committed
finishing up
1 parent 270c9b0 commit e0fd036

File tree

1 file changed

+77
-25
lines changed

1 file changed

+77
-25
lines changed

Apex style guide.md

Lines changed: 77 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,78 @@
11
# Apex Style Guide #
2-
[TOC]
3-
##Intro##
42

5-
###Goals###
3+
<!-- MarkdownTOC depth=0 autolink=true autoanchor=true -->
4+
5+
- [Intro][intro]
6+
- [Goals][goals]
7+
- [Sources][sources]
8+
- [Basics][basics]
9+
- [Special characters][special-characters]
10+
- [Whitespace][whitespace]
11+
- [Special escape sequences][special-escape-sequences]
12+
- [Other Non-ASCII Characters][other-non-ascii-characters]
13+
- [Structure][structure]
14+
- [Indentation][indentation]
15+
- [New-lines and spaces][new-lines-and-spaces]
16+
- [Prefer Explicit Declarations][prefer-explicit-declarations]
17+
- [Capitalization][capitalization]
18+
- [Example][example]
19+
- [SOQL][soql]
20+
- [Naming Conventions][naming-conventions]
21+
- [Class and Trigger][class-and-trigger]
22+
- [Methods][methods]
23+
- [Test classes][test-classes]
24+
25+
<!-- /MarkdownTOC -->
26+
27+
<a name="intro"></a>
28+
## Intro
29+
30+
<a name="goals"></a>
31+
### Goals
632
The goal of this style guide is like that of any other style guide. Everyone has their own ideas of what makes code pretty. As long as there's some logic behind that beauty, no one is right or wrong. But it's important to have a standard so that:
733

834
1. New and old developers, and outside contractors of all sorts can easily read, understand and maintain our growing code base.
935
2. Code merges are easy to handle and are content-ful, not style-ful.
1036

1137
See the Internet for more arguments about why style guides are important and useful things and not just a waste of time.
1238

13-
###Sources###
39+
<a name="sources"></a>
40+
### Sources
1441
Since Apex is largely a Java and C# spin-off, I am largely relying on [Google Java Style Guide](http://google-styleguide.googlecode.com/svn/trunk/javaguide.html) first, then C# where those do not apply. And some is just pulled out of my own opinions.
1542

1643
This is still a living document and I'm happy to make changes.
1744

18-
##Basics##
19-
###Special characters###
20-
####Whitespace####
45+
<a name="basics"></a>
46+
## Basics
47+
<a name="special-characters"></a>
48+
### Special characters
49+
<a name="whitespace"></a>
50+
#### Whitespace
2151
The only permissible whitespace characters in source code are newline and space (0x20). Inside of a string literal, only a space is allowed. Line must not end with spaces (`/ +$/` must not match anything in the file). Classes should all end with a newline.
2252

23-
####Special escape sequences####
53+
<a name="special-escape-sequences"></a>
54+
#### Special escape sequences
2455
For any character that has a special escape sequence (`\b`, `\t`, `\n`, `\f`, `\r`, `\"`, `\'` and `\\`), that sequence is used rather than the corresponding octal (e.g. `\012`) or Unicode (e.g. `\u000a`) escape.
2556

26-
####Other Non-ASCII Characters####
57+
<a name="other-non-ascii-characters"></a>
58+
#### Other Non-ASCII Characters
2759
For the remaining non-ASCII characters, either the actual Unicode character (e.g. ``) or the equivalent Unicode escape (e.g. `\u221e`) is used, depending only on which makes the code easier to read and understand.
2860

2961
> Tip: In the Unicode escape case, and occasionally even when actual Unicode characters are used, an explanatory comment can be very helpful.
3062
31-
##Structure##
63+
<a name="structure"></a>
64+
## Structure
3265

3366
The ordering of the members of a class can have a great effect on learnability, but there is no single correct recipe for how to do it. Different classes may order their members differently.
3467

3568
What is important is that each class order its members in some logical order, which its maintainer could explain if asked. For example, new methods are not just habitually added to the end of the class, as that would yield "chronological by date added" ordering, which is not a logical ordering.
3669

37-
38-
###Indentation###
70+
<a name="indentation"></a>
71+
### Indentation
3972
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.
4073

41-
###New-lines and spaces###
74+
<a name="new-lines-and-spaces"></a>
75+
### New-lines and spaces
4276
Use vertical whitespace as appropriate. Don't be afraid to separate blocks of code.
4377

4478
Prefer placing comments on a line by themselves.
@@ -53,7 +87,7 @@ In method definitions, there should be no space before the open parenthesis, and
5387

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

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.
90+
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. There should be no whitespace before commas, and one space after (e.g., `System.debug(LoggingLevel.INFO, 'fsdfs');`).
5791

5892
If using C#-style properties, code should follow the following rules:
5993

@@ -62,22 +96,25 @@ If using C#-style properties, code should follow the following rules:
6296
* If there is logic, there should be a new-line before each open-brace, and before and after each closed-brace.
6397
* If one clause has logic and one does not, place the clause without logic on its own line.
6498

65-
### Prefer Explicit Declarations ###
99+
<a name="prefer-explicit-declarations"></a>
100+
### Prefer Explicit Declarations
66101
Always specify:
67102

68-
* public/private modifiers
69-
* with/without sharing
103+
* `global`/`public`/`private` modifiers - prefer `private`, and if possible, `static`
104+
* `with sharing`/`without sharing`
70105
* `this` when calling local methods or setting local members/properties.
71106

72-
### Capitalization ###
107+
<a name="capitalization"></a>
108+
### Capitalization
73109

74110
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`.
75111

76112
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).
77113

78114
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.
79115

80-
### Example ###
116+
<a name="example"></a>
117+
### Example
81118

82119
```
83120
public class MyClass {
@@ -106,13 +143,14 @@ public class MyClass {
106143
107144
public void foo(Integer bar) {
108145
if (bar == 3) {
109-
System.debug(debugCode(bar) + ' - hi there!');
146+
// Diane often asks when bar is 3.
147+
System.debug(this.debugCode(bar) + ' - hi there!');
110148
return;
111149
} else if (bar > 7) {
112150
List<Integer> wasteOfSpace = new List<Integer>();
113151
do {
114-
wasteOfSpace.add(this.calculatedInteger);
115-
} while (wasteOfSpace.size < 5);
152+
wasteOfSpace.add(this.calculatedInteger);
153+
} while (wasteOfSpace.size() < 5);
116154
} else {
117155
try {
118156
upsert v;
@@ -130,13 +168,14 @@ public class MyClass {
130168
```
131169

132170

133-
##SOQL##
171+
<a name="soql"></a>
172+
## SOQL
134173

135174
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.
136175

137176
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`.
138177

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.
178+
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`, and always select it first.
140179

141180
Example (in context):
142181

@@ -149,10 +188,23 @@ List<Contact> cnts = [SELECT Id, FirstName, LastName, Phone, Email,
149188
FROM ActivityHistories
150189
WHERE Type = :typeToSelect)
151190
FROM Contact
152-
WHERE CreatedDate = TODAY];
191+
WHERE CreatedDate >= TODAY];
153192
```
154193

194+
<a name="naming-conventions"></a>
195+
## Naming Conventions
196+
197+
<a name="class-and-trigger"></a>
198+
### Class and Trigger
199+
Name a class or trigger after what it does. Triggers should be verbs and end with `Trigger` (e.g., `SyncCaseToplineWithDescriptionTrigger`). Controllers and Controller Extensions should end with the word `Controller`.
200+
201+
<a name="methods"></a>
202+
### Methods
203+
Methods should all be verbs. Getters and setters should have no side effects (with the exception of setting up cached values and/or logging), and should begin with `get` or `set`.
155204

205+
<a name="test-classes"></a>
206+
### Test classes
207+
Test classes should be named `TEST_ClassUnderTest`. If the test is not a unit-level test but instead a broader test case, it it should be named `TEST_StuffThatsGenerallyBeingTested`.
156208

157209

158210

0 commit comments

Comments
 (0)