Two Tools, One Job
The last two episodes gave you conditionals and switch statements, and they overlap a fair bit. So which one do you actually use?
The good news is the decision is mostly made for you by what switch statements can’t do. Once you know the limits, the choice is usually obvious.
Limit 1: Switch Only Handles Five Types
Integer, Long, String, SObject, Enum. That’s your lot.
Working with a Decimal? A Date? A Boolean? Switch isn’t available. Use a conditional.
//Amount is a Decimal, so switch is off the table
if (opp.Amount > 100000) { System.debug('Enterprise deal'); }
else if (opp.Amount > 10000) { System.debug('Mid market'); }
else { System.debug('Small deal'); }Limit 2: Switch Evaluates One Variable
A switch statement looks at exactly one thing. You cannot switch on two variables at once. There’s no syntax for it and there’s no clever workaround.
//Two variables. Conditional territory.
if (stageName == 'Closed Won' && amount > 10000)
{
System.debug('Big win');
}Limit 3: Switch Tests Equality Only
Switch asks “is this variable equal to that value?” It cannot ask “is it greater than,” “is it between,” “does it contain,” or anything else.
Ranges, comparisons, string matching, anything that isn’t a straight equality check needs a conditional.
So When Is Switch The Better Call?
When all three of those limits happen to suit you: one variable, one of the five supported types, checked against a known set of values.
Which describes an enormous amount of real Salesforce code, because picklists are everywhere. Stage names, case statuses, record types, industries. All Strings, all checked against a known list of values.
switch on cse.Status
{
when 'New' { assignToQueue(cse); }
when 'Working' { notifyOwner(cse); }
when 'Escalated' { alertManager(cse); }
when 'Closed' { sendSurvey(cse); }
when else { System.debug('Unhandled status: ' + cse.Status); }
}Compare that to five else if lines all repeating cse.Status == over and over. The switch version says what it’s doing at a glance.
My rule of thumb: three or more branches on a single value? Switch. Anything else? Conditional.
With one or two branches an if statement is shorter and perfectly clear, and a switch is just ceremony.
The Other Argument For Switch
Beyond readability, there’s a subtler benefit worth knowing about.
A switch statement makes it structurally obvious that you’re enumerating every possible value of one thing. When somebody adds a new picklist value, a reviewer looking at a switch statement immediately thinks “is there a branch for that?” The same logic buried in a chain of else ifs is much easier to skim past.
It also nudges you towards handling the unexpected case, because when else is sitting right there at the bottom looking at you. If/else chains let you just… not write the else, and nobody notices.
Small things, but they add up in a codebase people have to maintain for years.
A Third Option You Should Know About
Sometimes the right answer is neither.
If your switch is only mapping a value to another value, a Map does it in one line and no branches at all:
//Instead of a five branch switch that just returns a String...
private static final Map<String, String> STAGE_LABELS = new Map<String, String>{
'Prospecting' => 'Early days',
'Negotiation' => 'Getting close',
'Closed Won' => 'Nice!'
};
String label = STAGE_LABELS.get(stageName);Cleaner, and adding a new value is a one line change rather than a new branch. If your switch branches all do genuinely different work, keep the switch. If they’re all just returning a different value, reach for a Map.
What’s Next
Switch when it’s one variable of a supported type against a known set of values, with three or more branches. Conditional for everything else. Map when you’re only translating one value into another.
Your code can now make decisions. What it still can’t do is repeat itself, so in the next episode we get to loops.
See you next time!
Get Coding With The Force Merch!!
We now have a redbubble store setup so you can buy cool Coding With The Force merchandise! Please check it out! Every purchase goes to supporting the blog and YouTube channel.
Get Shirts Here!
Get Cups, Artwork, Coffee Cups, Bags, Masks and more here!
Check Out More Coding With The Force Stuff!
If you liked this post make sure to follow us on all our social media outlets to stay as up to date as possible with everything!
Youtube
Patreon
Github
Facebook
Twitter
Instagram
Salesforce Development Books I Recommend
Advanced Apex Programming
Salesforce Lightning Platform Enterprise Architecture
Mastering Salesforce DevOps
Good Non-SF Specific Development Books:
Clean Code
Clean Architecture