Photo by Thomas Jensen on Unsplash
Salesforce added switch statements in Apex, and I’ve started using them in place of long if-else chains. It’s made my code cleaner and more readable—especially when I’m dealing with picklists or status values.
Example:
switch on status {
when 'Open' {
// Do something
}
when 'Closed' {
// Do something else
}
when else {
// Default
}
}
Much easier than stacking if (status == 'Open') logic.
Bonus: It’s easier to maintain. When a new status is added, I just add a new case. No risk of missing an else-if condition. It’s a small shift that adds up when you’re writing a lot of conditional logic.