Apex Trigger Refactoring Journey, Part 1
03 Oct 2019
Join me for a journey as I refactor Salesforce Apex trigger handlers, working for cleaner code (fewer “smells”), better implementation of good design principles, and increased use of enterprise-appropriate design patterns.
I started KatieKodes a year ago because I felt I was “outgrowing” my first blog, which was a bit of a public diary as I … to be honest … kind of learned to code on the job.
Inspired by Women Code Heroes, Practical Business Python, and [Real Python](https://realpython.com/}{:target=”_blank”}, I wanted to start over and produce something more “professional.”
I wanted a repository of confident “here’s how you do this” demonstrations and tutorials that I could comfortably include in a resume to show what I already know.
Who knows – maybe at some point, I’ll shuffle these posts away to the old “diary” blog.
But for now, I’m going to be vulnerable and expose my “code smells” as I chronicle my journey to refactor old trigger handlers and write new ones better.
For years, I’ve returned again and again to Dan Appleman’s Advanced Apex Programming.
I eat up sessions like his and Robert Watson’s Dreamforce 2016 session It’s About (CPU) Time: The Dark Art Of Benchmarking.
Don’t be afraid of running multiple for loops over
Trigger.new
!!!
Take the time to learn what does what, with respect to parameter-passing by value or reference.
For example, String parameters pass by value, not by reference; this passes the asserts:
string doThing(String inString) {
inString += 'bye';
return inString;
}
String myStr = 'hi';
String myStr2 = doThing(myStr);
System.assertEquals('hi', myStr); // DID NOT GET MODIFIED
System.assertEquals('hibye', myStr2);