in Salesforce - consider what is better in following scenario - you have trigger. Trigger has separate handling class with proper methods (beforeInsert, beforeUpdate etc which are called in trigger). As you have multiple record types, you have another helper classes for different record types which are then called in handler class.
What is better - to create new instance of every helper class which will be filtering by record types and then execute proper logic or have classes with static methods called directly in handler. Or maybe there's another way?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Great question! This is a common architectural decision in Salesforce Apex development, especially as codebases grow and logic becomes more complex. Let’s break down the options and their pros/cons, and then I’ll suggest a best practice approach.
Option 1: Instance Helper Classes
You instantiate a helper class for each record type, and call instance methods.
Example:
apex
if(recordType == 'A') {
new RecordTypeAHelper().doSomething(records);
} else if(recordType == 'B') {
new RecordTypeBHelper().doSomething(records);
}
Pros:
OOP Principles: Allows for inheritance, interfaces, and polymorphism.
Stateful: Can maintain state if needed (rare in triggers, but possible).
Testability: Easier to mock/stub in unit tests.
Cons:
Overhead: Slightly more memory usage due to object instantiation (usually negligible in Apex).
Verbosity: More code to instantiate objects.
Option 2: Static Helper Methods
You call static methods directly on helper classes.
Performance: Marginally faster (no object creation).
Common in Apex: Most Salesforce helper classes use static methods.
Cons:
No State: Cannot maintain state between method calls.
Harder to Mock: More difficult to mock in tests (though not impossible).
Option 3: Strategy Pattern (Recommended for Complex Logic)
If your logic per record type is complex and may grow, consider using the Strategy Pattern. This means you define an interface (e.g., IRecordTypeHandler) and have each record type helper implement it. The handler class then selects the appropriate implementation at runtime.
Example:
apex
public interface IRecordTypeHandler {
void handle(List<SObject> records);
}
public class RecordTypeAHandler implements IRecordTypeHandler {
public void handle(List<SObject> records) { /* logic */ }
}
public class RecordTypeBHandler implements IRecordTypeHandler {
public void handle(List<SObject> records) { /* logic */ }
}
// In your handler:
IRecordTypeHandler handler;
if(recordType == 'A') {
handler = new RecordTypeAHandler();
} else if(recordType == 'B') {
handler = new RecordTypeBHandler();
}
handler.handle(records);
Pros:
Extensible: Easy to add new record types.
Testable: Can easily mock handlers.
OOP Best Practice: Clean separation of concerns.
Cons:
More Boilerplate: Slightly more code to set up.
Best Practice Recommendation
For simple logic: Use static helper methods. This is the Salesforce convention and is easy to read and maintain.
For complex or growing logic: Use the Strategy Pattern with interfaces and instance classes. This is more scalable and maintainable as your org grows.
Avoid mixing both approaches unless you have a clear reason.
Extra Tip:
If you have logic that is shared between record types, consider having a base helper class or utility class with static methods that can be called from your record type-specific helpers.
Summary Table
Approach
Use When...
Pros
Cons
Static Methods
Logic is simple, unlikely to grow
Simple, fast, common
Less flexible, harder to mock
Instance Classes
Logic is complex or stateful
OOP, testable, extensible
More code, slight overhead
Strategy Pattern
Many record types, complex logic
Scalable, clean, testable
More setup, boilerplate
In most Salesforce orgs, static helper methods are sufficient. But if you anticipate growth or want to follow OOP best practices, go with the Strategy Pattern.
If you want a code sample for a specific approach, let me know!