Reputation: 1793
I'm refactoring a program that has a lot of nested calls such as
ServiceManagement.getGlobalizationService()
.createExportConfiguration(
exportConfiguration,
getAdminWebClientSession().getUser().getToken()
.getTokenValue());
I would love some advice as to what sort of refactoring would be appropriate here as well as the best and simplest place to start said refactoring, right now I find that to be an overly complicated and unclear block.
Upvotes: 0
Views: 554
Reputation: 26856
First thing: Are you completely sure that none of these calls can ever (and I mean EVER) return null? If you are not COMPLETELY sure then you should be doing:
GlobalizationService gs = ServiceManagement.getGlobalizationService();
if (gs!=null) {
....
}
Or you could catch NullPointerException.
If you are completely sure that isn't necessary then there is a useful slight refactorization for readability:
TokenValue tv = getAdminWebClientSession().getUser().getToken().getTokenValue();
ServiceManagement.getGlobalizationService()
.createExportConfiguration(exportConfiguration,tv);
You might consider applying the Law of Demeter to get the TokenValue directly from the AdminClientSession, but in the general case that refactorization is not necessarily useful. Alternative application of the Law of Demeter would have TokenValue or Token passed to the method in which this code lies, but again this is not necessarily a good idea.
Upvotes: 2