Force.com User Group starting in Seattle

Last Updated on Tuesday, 22 September 2009 02:28 Written by Steve Tuesday, 22 September 2009 02:28

I just heard from Val that the Seattle Force.com Developer User group will kick off on October 1st. If you’re near Seattle and code in Apex and VisualForce, come join the group. I’m really excited about it.

Want to take a deeper dive into the technical capabilities of Salesforce? Would you like to meet other Force.com developers in the Seattle area? Interested in learning about new/different tools that will help you create and develop better solutions? A new Salesforce user group will be starting up on Thursday Oct 1. Meetings will be held on the first Thursday of every month.

We will try to cover a different range of topics at these monthly meetings such as:

  • Summary of new functionality in the upcoming release
  • Project spotlight where someone will demo their project and the technical details
  • Best Practices
  • 3rd Party development and integration tools
  • Apex and VisualForce
  • Open session to discuss specific questions you may have regarding your current projects

Time: 8-9am
Date: Oct 1, 2009
Location:

West Monroe Partners
1215 4th Ave Suite 1010
Seattle, WA 98161

Please contact Val Grasparil (vgrasparil@westmonroepartners.com) or Andrew Brown (abrown@westmonroepartners.com) if you have any questions.

Since it is in the morning, coffee and snacks will be provided. Please RSVP if you are planning on attending so we can make sure to have enough food for everyone.

Learn More

Awesome talk on how wrong we are about motivation

Last Updated on Sunday, 6 December 2009 11:47 Written by Steve Friday, 11 September 2009 02:40

I love this talk about how wrong business is, especially in combination with the Netflix presentation.

Learn More

Apex Architecture

Last Updated on Tuesday, 8 September 2009 09:55 Written by Steve Tuesday, 8 September 2009 09:55

My post on Friday spawned some great discussion, even over the Labor Day weekend. If you were off enjoying summer’s last gasp, go back and check it out. In this post, I want to follow up on that and try to take the Apex architecture discussion to the next level of depth.

As I’ve said many times on this blog, I’m not a computer scientist, so I’m backing into architecture and often the concepts from CS-101 are new to me. I know I’m not the only one, so I’d like to make explicit some of the assumptions that top-notch developers are making every day. I surely am not an expert, and love to have developers set me straight–life is about continuous learning.

I’m going to lay things out as I understand them–if you think I’m making poor assumptions, please comment and set me straight. Also know that one of the reasons I love coding is that it is a creative act. It’s an amazing combination of a highly analytical process and one of inspiration and experimentation. I bet most programmers love that aspect of the job, and so I don’t believe there is a right way to do things, and I hope this may help to unleash creativity, rather than be seen as laying out any kind of Apex dogma that must be followed for fear of wearing the scarlet ‘no software’ mark on your breast.

#1: How hard should you work to have your triggers be absolutely minimal?

I’ve written a bunch of Apex code and also gotten to maintain Apex code written by others. I think writing software for maintainability should be one of the top goals of all coders. I think the most maintainable way to write Apex triggers is to have as little logic in them as possible. If you are trying to figure out how some code works, or doesn’t work, it’s a lot easier to know that the trigger can safely be ignored because it’s only job is to call the class methods that get things done.

But how hard should we try to do that? Should we make sure our trigger has no logic in it at all? If so, it could look something like this:

trigger MyTrigger on MyObject__c (before insert, before update, after insert, after update) {
    MyObjectClass triggerSet = new MyObjectClass(Trigger.new, Trigger.old, Trigger.isBefore, Trigger.Context);
    triggerSet.process();
}

This signature would only have to change if we decide to have this trigger fire in different contexts, like UNDELETE. Of course, it’s not possible now, because there is no Trigger.Context to pass the enum value of the DML context. But, if Jon gets his way and this enum value is created, we have minimized the trigger to 2 lines of code.

But I think there is an efficiency hit. We’re going to instantiate the MyObjectClass no matter what the trigger set includes. If we only want to process records when a certain value changes, wouldn’t it be more efficient to move some logic to the trigger, and only instantiate the object if some of the records in the trigger set need to be processed?

Or is it a negligible difference we shouldn’t worry about?

#2: When should we use static methods vs. object-oriented design?

The trigger above is object-oriented design where an object is instantiated and variables and methods exist for that instance of the object. But the trigger could be written this way too:

trigger MyTrigger on MyObject__c (before insert, before update, after insert, after update) {
    boolean success = MyObjectClass.invoke(Trigger.new, Trigger.old, Trigger.isBefore, Trigger.Context);
}

So which is better? That’s something I’ve never been clear on–George made some great comments on my previous post along these lines.

The static method approach seems to make sense when your methods are straight-forward and you aren’t trying to maintain context across a number of actions. I like static methods for “utility” type functions:

utilities.getRecordTypeName(Id recordTypeId);

utilities.submitMyObjectForApproval(List my Objects);

I personally like the object-oriented approach when invoking a trigger. I think it makes sense to instantiate the object that is going to handle the trigger set, and then kick that off with various methods based on the context and timing of the invocation. It seems to me that writing the code to collect all the data from the trigger is best written once, in the constructor. Here’s some pseudocode for a such a class:

public class MyObjectClass {
     public MyObjectClass()
     {
     }

     public boolean triggerIsBefore;

     public enum context {Insert, Update, Insert, Upsert}

     public context contextPassedFromTrigger;

     public List<MyObject__c]]> triggerOldSet = new List<MyObject__c]]>();

     public List<MyObject__c]]> triggerNewSet = new List<MyObject__c]]>();    

     public MyObjectClass(MyObject__c[] myObjects, MyObject__c[] myOldObjects, boolean isBefore, context triggerContext)
     {
          triggerIsBefore = isBefore;
          contextPassedFromTrigger = triggerContext;

          triggerOldSet = myOldObjects;
          triggerNewSet = myObjects;
     }

     public process()
     }
          // BEFORE INSERT
          if (contextPassedFromTrigger==context.Insert&&isBefore)
          {
               someBeforeInsertMethod()
          }

     {

}

The main difference between static and object-oriented functionality is that you can have multiple independent instances of an object, but not of a static variable. So, if you wanted to deal with 4 different opportunities at once, and maintain data or variables for each and then do some processing, object-oriented is probably the way to go.

Each trigger invocation is it’s own universe. scope-wise, so you don’t have to worry about static invocations conflicting across trigger invocations.

#3: What should go in a static Utils class?

I like object oriented design, and I always compliment that with static classes for “utility” type methods and variables. I think each module of functionality you write should have it’s own utility class that services the other classes you’ve got in that module. I gave the example of the getRecordTypeName() method. Also, all your references to Custom Labels and other strings can be placed in this Utils class, and then they can be accessed statically from your other methods.

#4: What are the limits of class proliferation?

This is a tough one. Let’s say I’ve got some complex functionality. It wouldn’t be at all out of the realm of possibility to have the following:

MyProcess.trigger – my trigger to handle DMLs
MyProcessClass – my main class for handling the trigger
MyProcessUtils – my utils class supporting all this functionality
MyProcessException – a class to extend the exception class for this functionality
MyProcessTest – tests supporting all this code
Other classes for functions that should be instantiatable on their own
VisualForce controllers for any necessary UI

With complex functionality it can get to be a lot of files. So how is this balanced with maintainability? Aren’t more files and more dependencies harder for a maintainer to track down?

It is true that the more classes you’ve got, the harder it will be for someone to find everything and get their head around it. Naming conventions are key to finding related functionality. Everything I write that is related to this process will be started with ‘MyProcess’. Things can get confusing where processes meet and overlap–there isn’t anything we can do about that other than document things well with process maps and other tools.

I think that while there may be a lot of classes in this architecture, and findability my suffer, changability can be enhanced. By encapsulating key functions in their own classes or in well-written methods in existing classes, changes can be more easily made. One thing I’ve learned about writing code is to ‘encapsulate that which is likely to change.’ In the short-term that may seem like a lot of infrastructure for relatively simple code, but when it needs to change it may be a lot easier.

So my main takeaways from this foray into thinking about Apex architecture are:

  1. Move most of your logic out of Apex Triggers and into Apex classes. Don’t stress out if your Triggers have some logic in them, but the less the better in most cases
  2. Decide where you fit on the spectrum between everything static and everything object oriented, and then be consistent. Maintainability will suffer if you’re changing up all the time for similar functions
  3. Use a static utilities class, and be mindful about what goes in there
  4. Don’t stress out about class proliferation as long as your doing it to encapsulate that which may change later, and aren’t forgetting about making things finadable
Learn More
Copyright © 2009 Afterburner - Free GPL Template. All Rights Reserved.
WordPress is Free Software released under the GNU/GPL License.