Classic CS Algorithms In Apex

The Salesforce ecosystem always seems to have a higher number of self taught developers than other industries. I, certainly, count myself among them. It was not too many years ago that I could not have told you the difference between Java and HTML. As my own developer career has grown I have realized that two of the common concerns with self taught or boot camp grads is their lack of understanding of core computer science principles. Particularly, algorithms and data structures and how to evaluate them. I know I wrote more than my share of nested for loops before I realized that they didn’t scale too well and have a way of blowing up your CPU governor limits at the worst time.

In my own studies I realized that there is a lack of examples of some of these classic CS concepts implemented in Apex. I wanted to give back by creating a GitHub repository of classic sort and search algorithms and blogging about some of them.

Bubble Sort In Apex

Bubble sort seems to be one of the first sorting algorithms that is taught. Typically as an example of of algorithm that scales poorly. The nested for loop means that sorting 10 items will take 100 operations, 100 items will take 1000 operations and so on. If you are starting your journey as a developer it is worth taking some time to understand time complexity and Big-O notation.

Lets talk about the code. If we look at the class BubbleSort in the image above we we can see it has two methods Sort and Swap. The Sort method accepts a list of integers and then create a basic for loop. We set the index of the loop to the last value in our collection. This is our unsorted partition of our collection. The loop will continue as long as the value of the unsortedIndex variable is greater than 0 and will decrement by 1 each time go through the loop. A list of 10 integers would result create an unsortedIndex of 9.

We then set up an inner for loop. The index starts at 0 and will loop as long as the value is less than that of the unsortedIndex in the parent loop. (This is a slightly optimized bubble sort) The inner loop then compares each value in the array. If the element at position i is greater than the element at position i+1 we call our Swap method and change their place in the array. This results in the largest element in the loop “bubbling” up to the end of the collection.

The image below is an example of bubble sort being called on a simple 5 integer list. On the right you can see the debug log to help you follow the logic of the algorithm. We can see that in our first traversal through the inner loop that 78 is greater than 4 and their positions are swapped. 78 is greater than 22 and so forth until 78 bubbles up to the end of the array. We now know that the largest value is in the last position of our array. We can reduce the size of our unsorted partition by 1 and and examine the remaining 4 values.

I hope that this is a useful post to new developers and illustrates the issues with nested for loops and large data volumes.


Where To Begin

You signed up for Vetforce and Trailhead and now you are not sure what to do next. You are not alone, the list of career options and paths are a lot to take in: developer, admin, marketing, multiple types of consultant and business analysts. The most common question I get is “which one do I choose?” I can say, without hesitation, that you should start the admin trail, regardless of what your ultimate goals might be.

The admin certification is the foundation of of your Salesforce knowledge and career no matter if your eventual goal is to become a Advanced Developer or Certified Technical Architect. The admin certification is also a requirement for any of the consultant level certifications. If you are using Vetforce, I believe the decision is made for you and you must earn the admin certification before the developer path is open to you.

One of the keys to success on the Salesforce platform is how extendable it is without writing code. If you are a developer new to Salesforce this means that non-developers can impact your software. Even if you have a strong background in object oriented programing you will want to understand how a junior admin can break your code with a poorly tested validation rule.

The multileveled Salesforce security model is something else you have to understand to work effectively on the platform. If you are designing or implementing solutions you must understand how to use, roles, profiles, permission sets and public groups.

Salesforce Visual Workflow

Perhaps the most important reason for a developer to understand the declarative (clicks rather than code) side of the platform is determine when not to code. Is the only thing needed a simple field update? No problem -work flow rules or a process builder to the rescue. Do you need to work with arrays and create and delete records? Maybe a flow is an option.

Understanding when a to use a formula field or a roll up summary vs writing a a new class and method will speed your development process, allowing you to get your product to your users or faster and minimize the maintenance costs that come with programmatic solutions. If a user needs to the understand how discounting effects their margin a simple formula field will get you there much faster than coding a new method to do the same thing. Below is an example of this business case implemented in a formula and in code. And remember the programmatic solution still needs a trigger or another class to call it and unit tests. In Salesforce unit tests are a requirement. You can’t deploy code into production without them. No code coverage? No Deployment.

No matter where your career on the Salesforce platform takes you understanding how to be an Awesome Admin is the first step.

Success! You're on the list.