Complicated conditional branching-I want to give you a chance to write good code. 1 [C # refactoring sample]

Complicated conditional branching

The above Specific examples described in are as follows. (Source code is C #) specification) Consider comment management on bulletin boards. The posted comment is displayed in the comment list, and each comment is edited, deleted, approved, remanded, detailed display, and each button is controlled under the following conditions.

  1. If the login person is the poster, the comment can be edited, deleted, and the details can be displayed. It cannot be edited or deleted after approval.
  2. The first administrator can approve the comment after posting (primary approval), send it back, and display the details. Only detailed display is possible after approval.
  3. The second administrator can approve (secondary approval), remand, and display details only for comments approved by the first administrator. Only detailed display is possible after approval.

Source code) * Since I want to see only conditional branching, button activation control is omitted.


        private const string AuthorId = "Poster ID";
        private const string FirstManagerId = "First administrator ID";
        private const string SecondManagerId = "Second administrator ID";

        /// <summary>
        ///Control the activity of the button.
        /// </summary>
        /// <param name="loginUserId">Login person ID</param>
        /// <param name="firstApproverId">Primary approver ID(Null if not approved)</param>
        /// <param name="secondApproverId">Secondary approver ID(Null if not approved)</param>
        private void SetButtonControls(string loginUserId, string firstApproverId, string secondApproverId)
        {
            //Each button is inactive and initialized.

            if (loginUserId == AuthorId && firstApproverId == null && secondApproverId == null)
            {
                //Edit button, delete button, detail view button, activation
            }
            else if (loginUserId == FirstManagerId && firstApproverId == null)
            {
                //Approval button, remand button, detail display button activation
            }
            else if (loginUserId == FirstManagerId && firstApproverId != null)
            {
                //Detailed display button activation
            }
            else if (loginUserId == SecondManagerId && firstApproverId != null && secondApproverId == null)
            {
                //Approval button, remand button, detail display button activation
            }
            else if (loginUserId == SecondManagerId && firstApproverId != null && secondApproverId != null)
            {
                //Detailed display button activation
            }
        }
java
    private static final String AUTHOR_ID = "Poster ID";
    private static final String FIRST_MANAGER_ID = "First administrator ID";
    private static final String SECOND_MANAGER_ID = "Second administrator ID";

    protected void setButtonControls(String loginUserId, String firstApproverId, String secondApproverId) {
        //Each button is inactive and initialized.

        if (AUTHOR_ID.equals(loginUserId) && firstApproverId == null && secondApproverId == null) {
            //Edit button, delete button, detail view button, activation
        } else if (FIRST_MANAGER_ID.equals(loginUserId) && firstApproverId == null) {
            //Approval button, remand button, detail display button activation
        } else if (FIRST_MANAGER_ID.equals(loginUserId) && firstApproverId != null) {
            //Detailed display button activation
        } else if (SECOND_MANAGER_ID.equals(loginUserId) && firstApproverId != null && secondApproverId == null) {
            //Approval button, remand button, detail display button activation
        } else if (SECOND_MANAGER_ID.equals(loginUserId) && firstApproverId == null && secondApproverId != null) {
            //Detailed display button activation
        }

    }

It's as per the specifications, but it's hard to read because it's all else if. Also, since the negative statement of the same conditional statement is written in the if statement, it is redundant. It is better to arrange if statements in the same row.


        private void SetButtonControls(string loginUserId, string firstApproverId, string secondApproverId)
        {
            //Each button is inactive and initialized.

            //There were many people around me who didn't know how to write this.
            bool firstApproved = firstApproverId != null;
            bool secondApproved = secondApproverId != null;

            if (loginUserId == AuthorId)
            {
                if (!firstApproved && !secondApproved)
                {
                    //Edit button, delete button, active
                }

                //Detailed display button activity
            }
            else if (loginUserId == FirstManagerId)
            {

                if (!firstApproved)
                {
                    //Approve button, remand button, activation
                }

                //Approval details button activation
            }
            else if (loginUserId == SecondManagerId)
            {
                if (firstApproved && !secondApproved)
                {
                    //Approval button, remand button
                }

                //Detailed display button activation
            }
        }
java
    private static final String AUTHOR_ID = "Poster ID";
    private static final String FIRST_MANAGER_ID = "First administrator ID";
    private static final String SECOND_MANAGER_ID = "Second administrator ID";

    protected void setButtonControls(String loginUserId, String firstApproverId, String secondApproverId) {
        //Each button is inactive and initialized.

        boolean firstApproved = firstApproverId != null;
        boolean secondApproved = secondApproverId != null;

        if (AUTHOR_ID.equals(loginUserId)) {

            if (!firstApproved && !secondApproved) {
                //Edit button, delete button, active
            }

            //Detail display button activation

        } else if (FIRST_MANAGER_ID.equals(loginUserId)) {

            if (!firstApproved) {
                //Approve button, remand button, activation
            }

            //Approval details button activation

        } else if (SECOND_MANAGER_ID.equals(loginUserId)) {

            if (firstApproved && !secondApproved) {
                //Approval button, remand button
            }

            //Detailed display button activation
        }
    }

Problems related to how to write source code basically occur due to individual skill differences such as "I can't think of such a way of writing". I would like to write sample code regularly, including the meaning of reviewing the source code.

Refactoring code 2

I received a reference code in the comment section, so I will post it. In addition, some variable names have been modified so that the handling of Bool type can be unified with other articles. There is no if statement, and the button activity control is easy to understand.

        private void SetButtonControls(string loginUserId, string firstApproverId, string secondApproverId)
        {
            //First approved
            bool firstApproved = firstApproverId != null;

            //Second approved
            bool secondApproved = firstApproved && secondApproverId != null;

            //String comparison can be Equals,==But string comparison is possible.
            bool isAuthor = AuthorId == loginUserId;
            bool isFirstManager = FirstManagerId == loginUserId;
            bool isSecoundManager = SecondManagerId == loginUserId;

            //Button activation control

            //Edit button
            bool editButtonEnabled = isAuthor && !firstApproved;

            //Delete button
            bool deleteButtonEnabled = isAuthor && !firstApproved;

            //Approval button
            bool approvalButtonEnabled = (isFirstManager && !firstApproved) || (isSecoundManager && !secondApproved);

            //Details button
            bool detailButtonEnabled = isAuthor || isFirstManager || isSecoundManager;
        }

java
    private static final String AUTHOR_ID = "Poster ID";
    private static final String FIRST_MANAGER_ID = "First administrator ID";
    private static final String SECOND_MANAGER_ID = "Second administrator ID";

    protected void setButtonControls(String loginUserId, String firstApproverId,
            String secondApproverId) {

        //First approved
        boolean firstApproved = firstApproverId != null;

        //Second approved
        boolean secondApproved = firstApproved && secondApproverId != null;

        boolean isAuthor = AUTHOR_ID.equals(loginUserId);
        boolean isFirstManager = FIRST_MANAGER_ID.equals(loginUserId);
        boolean isSecoundManager = SECOND_MANAGER_ID.equals(loginUserId);

        //Button activation control

        //Edit button
        boolean editButtonEnabled = isAuthor && !firstApproved;

        //Delete button
        boolean deleteButtonEnabled = isAuthor && !firstApproved;

        //Approval button
        boolean approvalButtonEnabled = (isFirstManager && !firstApproved) || (isSecoundManager && !secondApproved);

        //Details button
        boolean detailButtonEnabled = isAuthor || isFirstManager || isSecoundManager;
    }

Next article (code collection that is a little worrisome)

Table of Contents

Recommended Posts

Complicated conditional branching-I want to give you a chance to write good code. 1 [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