Occasionally, I see code that has the same function called in an if statement but differs only in the arguments. Think about whether you really need to call it in an if statement.
Consider an example of a log management screen. Detailed log information is displayed to the system administrator, but detailed logs are not displayed to anyone other than the system administrator.
public void Main(bool isManager, string detail)
{
//Display detailed logs to the administrator.
if (isManager)
{
SetSummary("An unexpected error has occurred. Please check the detailed log.");
SetDetail(detail);
}
else
{
SetSummary("An unexpected error has occurred.");
SetDetail("Please contact the administrator for details.");
}
}
private void SetSummary(string summary)
{
System.Console.WriteLine("Set the outline on the screen." + summary);
}
private void SetDetail(string detail)
{
System.Console.WriteLine("Set details on the screen." + detail);
}
public void main(boolean isManager, String detail) {
//Display detailed logs to the administrator.
if (isManager) {
setSummary("An unexpected error has occurred. Please check the detailed log.");
setDetail(detail);
} else {
setSummary("An unexpected error has occurred.");
setDetail("Please contact the administrator for details.");
}
}
private void setSummary(String summary) {
System.out.println("Set the outline on the screen." + summary);
}
private void setDetail(String detail) {
System.out.println("Set details on the screen." + detail);
}
What is worrisome in this code is that the only difference in the if statement is that the function calls are the same, only the arguments. It is cleaner to change only the arguments and make one function call.
//Display detailed logs for administrators.
string summary = null;
string detail = null;
if (isManager)
{
summary = "An unexpected error has occurred. Please check the detailed log.";
detail = detailLog;
}
else
{
summary = "An unexpected error has occurred.";
detail = "Please contact the administrator for details.";
}
//Separated the function call part from the conditional branch.
SetSummary(summary);
SetDetail(detail);
public void main(boolean isManager, String detailLog) {
//Display detailed logs to the administrator.
String summary = null;
String detail = null;
if (isManager) {
summary = "An unexpected error has occurred. Please check the detailed log.";
detail = detailLog;
} else {
summary = "An unexpected error has occurred.";
detail = "Please contact the administrator for details.";
}
setSummary(summary);
setDetail(detail);
}
private void setSummary(String summary) {
System.out.println("Set the outline on the screen." + summary);
}
private void setDetail(String detail) {
System.out.println("Set details on the screen." + detail);
}
In all cases, function calls cannot be separated, but I think there is room for consideration. What makes me happy with this fix is that the message generation process and the message setting process can be separated. For example, it can be seen that the subsequent message setting process is always called even if the message generation is further branched and modified to output a different message.
Previous article (too many function arguments)
Next article (Getter, Setter inverse problem)
Recommended Posts