Reputation: 2210
I have the following method (splitted because was too long):
private String replacePlaceholders(IncidentFullDTO incident, String text) {
text = replacePlaceholdersPL(incident, text);
text = replacePlaceholdersENG(incident, text);
text = replaceAdministratorProcessor(incident, text);
text = replaceRateSummary(incident, text);
return text;
}
Each method (like replacePlaceholdersPL) returns a new "text". All these methods are very similar. Example:
private String replacePlaceholdersPL(AAA incident, String text) {
if (text.equals(PlaceholdersEnum.CURRENT_DATE.getPlaceholder())) {
text = text.replace(PlaceholdersEnum.CURRENT_DATE.getPlaceholder(), getCurrentDate());
} else if (...) {
text = text.replace(PlaceholdersEnum.DATE_TIME_START_INCIDENT.getPlaceholder(), formatDate(incident.getIncidentDate()));
} else if(...) {}
} else if(...) {}...
How to write this code with clean code principles? I want to split this code to not to invoke unnecessarily all these methods like replacePlaceholdersENG when the previous method changed the "text" variable... I get the "text" variable from the docx file and I want to replace this text with the other text value
Upvotes: 1
Views: 343
Reputation: 23
Not sure what you mean by clean code principles but you can create a class and set incident and text as instance variables. Each function call will have the updated text.
class PlaceHolder {
Incident incident;
String text;
public PlaceHolder(Incident incident, String text){
this.incident = incident;
this.text = text;
}
public String getResult() {
replaceA();
replaceB();
....
return this.text;
}
public void replaceA(){}
....
}
Upvotes: 1