I have several controllers. In each controller class I defined the same set of helper methods:
private void info(String msg)
{
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, msg));
}
private void error(String msg)
{
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, msg));
}
Credit: ConnectorController.cls
I try to follow coding best practices, thus I need to avoid code duplication. Now I am thinking how to do it right way. I am considering two options:
Create an abstract class and then inherit all controller classes from it. But I don't feel that's a correct approach since info/error methods bear utility nature. That said, I still wanted to know whether my thinking is correct.
Create a utility class and make those methods static. Then call those methods from the controller.
This question may be considered as too broad or opinion-based, but as long as best practices are concerned I don't believe many different views will pop up.
msg+''
because ifmsg
is null, then you'll get an exception). – Renato Oliveira Jul 31 '18 at 14:09