Perhaps this isn't the place for this -- but it looks like a good place to start. I'm using Apex PMD code analysis (within Visual Studio Code) and it's throwing off the following for an interface implementation I am developing:
Avoid empty block statements.
The interface (Obviously, a trigger framework) looks like this:
public interface TriggerHandler {
void beforeInsert(List<SObject> newItems);
void beforeUpdate(Map<Id, SObject> newItems, Map<Id, SObject> oldItems);
void beforeDelete(Map<Id, SObject> oldItems);
void afterInsert(Map<Id, SObject> newItems);
void afterUpdate(Map<Id, SObject> newItems, Map<Id, SObject> oldItems);
void afterDelete(Map<Id, SObject> oldItems);
void afterUndelete(Map<Id, SObject> oldItems);
Boolean isDisabled();
}
Let's say I have a very simple implementation where I only want to check an account name before it's inserted:
public class AccountTriggerHandler implements TriggerHandler {
public static Boolean triggerDisabled = false;
public Boolean isDisabled() {
return triggerDisabled;
}
public void beforeInsert(List<SObject> newItems) {
accountNameCheck((List<Account>)newitems);
}
public void beforeUpdate(Map<Id, SObject> newItems, Map<Id, SObject> oldItems) {}
public void beforeDelete(Map<Id, SObject> oldItems) {}
public void afterInsert(Map<Id, SObject> newItems) {}
public void afterUpdate(Map<Id, SObject> newItems, Map<Id, SObject> oldItems) {}
public void afterDelete(Map<Id, SObject> oldItems) {}
public void afterUndelete(Map<Id, SObject> oldItems) {}
/**
* Check the accounts to make sure their name does not contain the text "test".
* If any do, reject them.
*/
private void accountNameCheck(List<Account> accountList) {
// Reject any Accounts which have the word "Test" in the name
for (Account acc : accountList) {
if (acc.Name.toLowerCase().containsOnly('test')) {
acc.Name.addError('You may not use the word "test" in the account name');
}
}
}
}
I don't plan to on any other account trigger actions at this time, so I simply leave the other methods empty. Doing so triggers the Apex PMD notice above. How would I set things up so I can avoid the Apex PMD error?
NOTE: Not sure why, but if I change:
public class AccountTriggerHandler implements TriggerHandler
to
public inherited sharingclass AccountTriggerHandler implements TriggerHandler
the PMD notice/error goes away.
For implementations of interfaces with a number of methods, there's always liable to be empty methods
. I think the premise here is probably flawed. Can you expand on what you mean here, and perhaps provide an example to illustrate your point? If you're leaving interface methods unimplemented, that sounds like a case to break up your interface, or to use inheritance or composition to provide a default implementation.