[JAVA] Is it possible to separate function calls and conditionals? --I want to give you a chance to write good code. 9 [C # refactoring sample]

Function called after conditional branching

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.

Sample code

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);
        }

java
    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);
    }

Things to worry about

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.

After refactoring

            //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);

java
    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);
    }

Summary

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)

Table of Contents

Recommended Posts

Is it possible to separate function calls and conditionals? --I want to give you a chance to write good code. 9 [C # refactoring sample]
Too many function arguments-I want to give you a chance to write good code. 8 [C # refactoring sample]
Easy Null Check-I want to give you a chance to write good code. 6 [C # refactoring sample]
A little worrisome code collection-I want to give you a chance to write good code. 2 [C # refactoring sample]
Bool type handling-I want to give you a chance to write good code. 7 [C # refactoring sample]
A flowing interface? -I want to give you an opportunity to write good code. 3 [C # refactoring sample]
Wasteful processing of collections-I want to give you a chance to write good code. 5 [C # refactoring sample]
Easy Null Check-I want to give you a chance to write good code. 6 [C # refactoring sample]
A little worrisome code collection-I want to give you a chance to write good code. 2 [C # refactoring sample]
Bool type handling-I want to give you a chance to write good code. 7 [C # refactoring sample]
Too many function arguments-I want to give you a chance to write good code. 8 [C # refactoring sample]
Wasteful processing of collections-I want to give you a chance to write good code. 5 [C # refactoring sample]
A flowing interface? -I want to give you an opportunity to write good code. 3 [C # refactoring sample]
Is it possible to separate function calls and conditionals? --I want to give you a chance to write good code. 9 [C # refactoring sample]
How to write good code
I want to write a nice build.gradle
I want to write a unit test!
I want to simply write a repeating string
7 things I want you to keep so that it doesn't become a fucking code
I want to summarize Apache Wicket 8 because it is a good idea
7 things I want you to keep so that it doesn't become a fucking code
I want to make a function with kotlin and java!
I want to write a nice build.gradle
I want to write a unit test!
I want to simply write a repeating string
I don't know, so I'm going to write a list (you don't have to read it)
If hash [: a] [: b] [: c] = 0 in Ruby, I want you to extend it recursively even if the key does not exist.