Scaling API-first – The story of a global engineering organization
Apex for humans
1. Apex for Humans (and recently turned zombies)
Kevin Poorman
Principal Architect
@CodeFriar
Hey, I know we just met, and I know this’ll sound
crazy but here’s my twitter: @codefriar, so follow
me maybe?
3. Agenda
Our Goal: Ensure everyone has the capability to read and understand Apex.
• A note on the philosophy of language
• Grammar school
• Diagram all the things!
• There will be a quiz
4. Metaphors we live by
A quick recap of vocabulary from third grade
• Metaphors are statements of comparison
between two objects that have a
non-logical, or non-obvious similarity.
• Similes are like Metaphors, but they use
‘Like’, or ‘As’ to make the comparison.
(see what I did there?)
5. It's language for everybody, there is no difference!
Language, regardless of it's primary usage
for human or computer communication;
utilizes metaphor to help assign
meaning to objects and actions.
6. So why do we call them programming languages?
• Because they utilize metaphor to help assign meaning.
• In addition to the use of metaphor, computer languages have the following traits as
well:
– Grammar
– Dialect
– Idioms
– Vocabulary
7. Grammar is Hard. Lets go to the Expo floor.
The grammar, or “syntax” of a language’s written form is the first hurdle to learning
any language.
• It’s also the easiest thing to learn about
a language. We pick it up, just by using
the language!
• That said …
8. Why is it hard?
• Grammar has subtle, often physically hard
to see marks.
• Consider:
– Let’s eat Grandma!
– Let’s eat, Grandma!
The single most likely cause of an extinction-event
level robot rising is a software grammar bug. – me.
10. Ground Rules
• Apex’s grammar says that complete thoughts are
ended with a semi-colon rather than a period
– We call complete thoughts “statements”
– Incomplete thoughts are “expressions”
• Additionally, Apex’s grammar dictates that
paragraphs, are grouped by { and } rather than
indentation and line breaks.
– Rarely are the {} on the same line.
• Most importantly: You are a Salesforce
Developer.
11. Ground Rules
• Variables
– Proper Names
• Types. Literally the hardest part of this.
– Apex is strongly typed, which is zombie speak for saying
that it’s a rather dumb language that demands you tell it
what kind of variable you’re creating.
– All Standard Objects are types
– All Custom Objects are types (Zombie__c)
– Built in types for Integer, String, Boolean etc.
– Type_name variable_name …
– Variable_name = (type_name) …
12. Ground Rules
• Methods
– “Last year at Dreamforce …”
– Also typed. You have to specify what kind of data the
method is going to return.
• Keywords / Reserved Words.
– Kind of like curse words, regardless of the language you
have to learn what you can and can’t say.
– abstract, break, catch, class, continue, do, else, enum, extends, false,
final, finally, for, global, if, implements, instanceOf, interface, new, null,
override, private, protected, public, return, static, super, testmethod, this,
throw, transient, trigger, true, try, webService, while, 'with sharing',
'without sharing' and virtual.
13. Ground Rules – last one I promise
• Sometimes we have complex types that only a
compiler could love.
– List<type_name> i.e.: List<Account>
– Set<type_name> i.e.: Set<Contact>
– Map<Set_type_name,List_Type_name> i.e.:
Map<Id,Zombie__c>
– Think about a roll of crackers.
Tasty tasty brainz crackers.
14. Now for the Quiz(es)
This is the interactive part, don’t make me call on you third person from the left in row
5.
15. Diagraming some Code
1. Type listType = Type.forName('List<' + objType + '>');
2. if (listType == null) {
3. return makeError('The requested resource does not exist',
'NOT_FOUND');
4. }
This code snippet has Statements, one Expression and one Paragraph. Can you
find them?
For 10,000 internet points* What line contains the creation of a variable and what’s
its type?
*redeemable only on the third Tuesday of the 5th month after the first blue moon for funny-cat-photos
16. Diagraming some Code
1. Zombie__c mattyb = Type.forName('List<‘Zombie__c’>');
2. Id objId = (id) ‘001abc1234LAKFR’;
3. String awesomeSauce = ‘Awesome Sauce’;
4. Account ZombieAccount = new Account();
For another 10,000 internet points, which of these has the type …
17. Diagraming some Code
1. Public Static Country__c getCountry() {
2. Integer i = TestUtils.getRandomNumber(10000);
3. return new Country__c(Name = 'Test Country' + i,
4. Sub_Region__c = 'Test Region’+ i,
5. Region__c = 'Test Location' + i,
6. English_Speaking__c = 'Yes');
7. }
Commas save lives, sure. Sure are hard to seesometimes.
You can do this, it’s like learning English, and heck, most of us did that as a toddler.
We use variables to store data in memory for us. You can think of memory as a pantry of sorts, full of cans and boxes and what have you, all of which are variables, each of which has a Name.
Types are a difficult beast to wrap your head around the first time. If our pantry of variables were real, we could look in and see a few boxes labeled “Account” and some Cans labeled” contact and maybe some other shaped cans labeled “Zombie__c”
Each Type has a different shape, and different properties and Apex requires us to declare what kind of box, or can we want to put in the pantry.
Best part, We already know how to think in terms of Types, because Every object in our org is a type.
We use variables to store data in memory for us. You can think of memory as a pantry of sorts, full of cans and boxes and what have you, all of which are variables, each of which has a Name.
Types are a difficult beast to wrap your head around the first time. If our pantry of variables were real, we could look in and see a few boxes labeled “Account” and some Cans labeled” contact and maybe some other shaped cans labeled “Zombie__c”
Each Type has a different shape, and different properties and Apex requires us to declare what kind of box, or can we want to put in the pantry.
Best part, We already know how to think in terms of Types, because Every object in our org is a type.
These complex types are all collections, not unlike the roll of brain crackers. But they’re also generic, allowing you to specify what goes in the roll.
You could have a roll of Account crackers, or contact crackers
The simplest collection type we have is a list. A list is an ordered … well list, of whatever object you’ve specified. These are used all the time.
With a list of zombies, you would be guaranteed that your zombies were in a given order, and you’d only have to fight them off one by one.
A set has no order, but is guaranteed to be unique. If you were given a set of zombies that was 25 elements long, you’d have to run away from 25 zombies!
Finally, a Map is a mashup of set and list. The first type given, is the keyset, and the second is the value type. You’ll see maps used a lot so that you can pull
Specific data out of a map just by knowing it’s name. It’s a bit like a phone book that way. Remember those? If you knew someone’s name you could find their contact record.
This contains:
1. Statement (lines 1 & 3)
2. Expression (line 2)
2. Paragraph (lines 2-4 denoted by the {}'s)
Line one contains a variable definition!