Toggle Function in JavaScript












2












$begingroup$


Here is my current toggle function:



$scope.toggleSelect = event => {
$scope.selection = event.target.innerHTML;

if ($scope.selection === 'Select All') {
$scope.emailList = $scope.users.filter(user => !user.report.emailed);
} else {
$scope.emailList = $scope.emailList = ;
}
};


Here are my thoughts and questions about the best practices:





  1. I make an assignment to $scope.emailList in the if and else statement. Instead of separate assignments, is one assignment better? Refactoring to one assignment will pollute the code with unnecessary variables, but it may make the code more readable?



    $scope.toggleSelect = event => {
    $scope.selection = event.target.innerHTML;
    const emailListUpdate;
    if ($scope.selection === 'Select All') {
    emailListUpdate = $scope.users.filter(user => !user.report.emailed);
    } else {
    emailListUpdate = $scope.emailList = ;
    }
    $scope.emailList = emailListUpdate;
    };


    The main benefit I see here is that anybody reading the code can skim to the last line and know the overall purpose. Overall, I don't see this to be a beneficial refactor since I don't think it adds additional readability and potentially makes it harder to follow. I would appreciate thoughts on this.




  2. Ternary or if/else:



    I reviewed a great post about the benefits and use cases of using ternary or if/else. Here is what the code would look like refactored to a ternary:



    $scope.toggleSelect = event => {
    $scope.selection = event.target.innerHTML;
    $scope.emailList = $scope.selection === 'Select All' ? $scope.users.filter(user => !user.report.emailed); : ;

    };


    Quoting from the article linked above:




    An if/else statement emphasizes the branching first and what's to be done is secondary, while a ternary operator emphasizes what's to be done over the selection of the values to do it with.




    I feel the if/else feels more natural, but I'm not sure if that is just related to my lack of experience using ternary.




  3. I have another toggleItem function used when a checkbox is clicked.



    $scope.toggleItem = (event, user) => {
    if (event.target.checked) {
    $scope.emailList.push(user);
    } else {
    $scope.emailList = _.reject($scope.emailList, item => item._id === user._id);
    }



I've thought about combining both of my toggle functions but overall I think it is better to keep them separate instead of trying to make a generic toggle function with multiple use cases.



In the toggleItem function above, I think it is cleaner (and more obvious) to use an if/else instead of a ternary since I am only making an assignment in the else statement.



I'm trying to improve on writing cleaner code, If anyone has any input or thoughts on best practices to use here or how to clean up this code it would be appreciated.










share|improve this question











$endgroup$












  • $begingroup$
    Second snippet second last line should not be there....
    $endgroup$
    – Blindman67
    12 hours ago










  • $begingroup$
    thanks for letting me know!
    $endgroup$
    – HelloWorld
    12 hours ago










  • $begingroup$
    Please provide some context for this code. What exactly does it do? Is it Angular? If so, please add a tag and add some details. See How to Ask.
    $endgroup$
    – 200_success
    12 hours ago
















2












$begingroup$


Here is my current toggle function:



$scope.toggleSelect = event => {
$scope.selection = event.target.innerHTML;

if ($scope.selection === 'Select All') {
$scope.emailList = $scope.users.filter(user => !user.report.emailed);
} else {
$scope.emailList = $scope.emailList = ;
}
};


Here are my thoughts and questions about the best practices:





  1. I make an assignment to $scope.emailList in the if and else statement. Instead of separate assignments, is one assignment better? Refactoring to one assignment will pollute the code with unnecessary variables, but it may make the code more readable?



    $scope.toggleSelect = event => {
    $scope.selection = event.target.innerHTML;
    const emailListUpdate;
    if ($scope.selection === 'Select All') {
    emailListUpdate = $scope.users.filter(user => !user.report.emailed);
    } else {
    emailListUpdate = $scope.emailList = ;
    }
    $scope.emailList = emailListUpdate;
    };


    The main benefit I see here is that anybody reading the code can skim to the last line and know the overall purpose. Overall, I don't see this to be a beneficial refactor since I don't think it adds additional readability and potentially makes it harder to follow. I would appreciate thoughts on this.




  2. Ternary or if/else:



    I reviewed a great post about the benefits and use cases of using ternary or if/else. Here is what the code would look like refactored to a ternary:



    $scope.toggleSelect = event => {
    $scope.selection = event.target.innerHTML;
    $scope.emailList = $scope.selection === 'Select All' ? $scope.users.filter(user => !user.report.emailed); : ;

    };


    Quoting from the article linked above:




    An if/else statement emphasizes the branching first and what's to be done is secondary, while a ternary operator emphasizes what's to be done over the selection of the values to do it with.




    I feel the if/else feels more natural, but I'm not sure if that is just related to my lack of experience using ternary.




  3. I have another toggleItem function used when a checkbox is clicked.



    $scope.toggleItem = (event, user) => {
    if (event.target.checked) {
    $scope.emailList.push(user);
    } else {
    $scope.emailList = _.reject($scope.emailList, item => item._id === user._id);
    }



I've thought about combining both of my toggle functions but overall I think it is better to keep them separate instead of trying to make a generic toggle function with multiple use cases.



In the toggleItem function above, I think it is cleaner (and more obvious) to use an if/else instead of a ternary since I am only making an assignment in the else statement.



I'm trying to improve on writing cleaner code, If anyone has any input or thoughts on best practices to use here or how to clean up this code it would be appreciated.










share|improve this question











$endgroup$












  • $begingroup$
    Second snippet second last line should not be there....
    $endgroup$
    – Blindman67
    12 hours ago










  • $begingroup$
    thanks for letting me know!
    $endgroup$
    – HelloWorld
    12 hours ago










  • $begingroup$
    Please provide some context for this code. What exactly does it do? Is it Angular? If so, please add a tag and add some details. See How to Ask.
    $endgroup$
    – 200_success
    12 hours ago














2












2








2





$begingroup$


Here is my current toggle function:



$scope.toggleSelect = event => {
$scope.selection = event.target.innerHTML;

if ($scope.selection === 'Select All') {
$scope.emailList = $scope.users.filter(user => !user.report.emailed);
} else {
$scope.emailList = $scope.emailList = ;
}
};


Here are my thoughts and questions about the best practices:





  1. I make an assignment to $scope.emailList in the if and else statement. Instead of separate assignments, is one assignment better? Refactoring to one assignment will pollute the code with unnecessary variables, but it may make the code more readable?



    $scope.toggleSelect = event => {
    $scope.selection = event.target.innerHTML;
    const emailListUpdate;
    if ($scope.selection === 'Select All') {
    emailListUpdate = $scope.users.filter(user => !user.report.emailed);
    } else {
    emailListUpdate = $scope.emailList = ;
    }
    $scope.emailList = emailListUpdate;
    };


    The main benefit I see here is that anybody reading the code can skim to the last line and know the overall purpose. Overall, I don't see this to be a beneficial refactor since I don't think it adds additional readability and potentially makes it harder to follow. I would appreciate thoughts on this.




  2. Ternary or if/else:



    I reviewed a great post about the benefits and use cases of using ternary or if/else. Here is what the code would look like refactored to a ternary:



    $scope.toggleSelect = event => {
    $scope.selection = event.target.innerHTML;
    $scope.emailList = $scope.selection === 'Select All' ? $scope.users.filter(user => !user.report.emailed); : ;

    };


    Quoting from the article linked above:




    An if/else statement emphasizes the branching first and what's to be done is secondary, while a ternary operator emphasizes what's to be done over the selection of the values to do it with.




    I feel the if/else feels more natural, but I'm not sure if that is just related to my lack of experience using ternary.




  3. I have another toggleItem function used when a checkbox is clicked.



    $scope.toggleItem = (event, user) => {
    if (event.target.checked) {
    $scope.emailList.push(user);
    } else {
    $scope.emailList = _.reject($scope.emailList, item => item._id === user._id);
    }



I've thought about combining both of my toggle functions but overall I think it is better to keep them separate instead of trying to make a generic toggle function with multiple use cases.



In the toggleItem function above, I think it is cleaner (and more obvious) to use an if/else instead of a ternary since I am only making an assignment in the else statement.



I'm trying to improve on writing cleaner code, If anyone has any input or thoughts on best practices to use here or how to clean up this code it would be appreciated.










share|improve this question











$endgroup$




Here is my current toggle function:



$scope.toggleSelect = event => {
$scope.selection = event.target.innerHTML;

if ($scope.selection === 'Select All') {
$scope.emailList = $scope.users.filter(user => !user.report.emailed);
} else {
$scope.emailList = $scope.emailList = ;
}
};


Here are my thoughts and questions about the best practices:





  1. I make an assignment to $scope.emailList in the if and else statement. Instead of separate assignments, is one assignment better? Refactoring to one assignment will pollute the code with unnecessary variables, but it may make the code more readable?



    $scope.toggleSelect = event => {
    $scope.selection = event.target.innerHTML;
    const emailListUpdate;
    if ($scope.selection === 'Select All') {
    emailListUpdate = $scope.users.filter(user => !user.report.emailed);
    } else {
    emailListUpdate = $scope.emailList = ;
    }
    $scope.emailList = emailListUpdate;
    };


    The main benefit I see here is that anybody reading the code can skim to the last line and know the overall purpose. Overall, I don't see this to be a beneficial refactor since I don't think it adds additional readability and potentially makes it harder to follow. I would appreciate thoughts on this.




  2. Ternary or if/else:



    I reviewed a great post about the benefits and use cases of using ternary or if/else. Here is what the code would look like refactored to a ternary:



    $scope.toggleSelect = event => {
    $scope.selection = event.target.innerHTML;
    $scope.emailList = $scope.selection === 'Select All' ? $scope.users.filter(user => !user.report.emailed); : ;

    };


    Quoting from the article linked above:




    An if/else statement emphasizes the branching first and what's to be done is secondary, while a ternary operator emphasizes what's to be done over the selection of the values to do it with.




    I feel the if/else feels more natural, but I'm not sure if that is just related to my lack of experience using ternary.




  3. I have another toggleItem function used when a checkbox is clicked.



    $scope.toggleItem = (event, user) => {
    if (event.target.checked) {
    $scope.emailList.push(user);
    } else {
    $scope.emailList = _.reject($scope.emailList, item => item._id === user._id);
    }



I've thought about combining both of my toggle functions but overall I think it is better to keep them separate instead of trying to make a generic toggle function with multiple use cases.



In the toggleItem function above, I think it is cleaner (and more obvious) to use an if/else instead of a ternary since I am only making an assignment in the else statement.



I'm trying to improve on writing cleaner code, If anyone has any input or thoughts on best practices to use here or how to clean up this code it would be appreciated.







javascript comparative-review






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 26 mins ago









Jamal

30.4k11121227




30.4k11121227










asked 13 hours ago









HelloWorldHelloWorld

175117




175117












  • $begingroup$
    Second snippet second last line should not be there....
    $endgroup$
    – Blindman67
    12 hours ago










  • $begingroup$
    thanks for letting me know!
    $endgroup$
    – HelloWorld
    12 hours ago










  • $begingroup$
    Please provide some context for this code. What exactly does it do? Is it Angular? If so, please add a tag and add some details. See How to Ask.
    $endgroup$
    – 200_success
    12 hours ago


















  • $begingroup$
    Second snippet second last line should not be there....
    $endgroup$
    – Blindman67
    12 hours ago










  • $begingroup$
    thanks for letting me know!
    $endgroup$
    – HelloWorld
    12 hours ago










  • $begingroup$
    Please provide some context for this code. What exactly does it do? Is it Angular? If so, please add a tag and add some details. See How to Ask.
    $endgroup$
    – 200_success
    12 hours ago
















$begingroup$
Second snippet second last line should not be there....
$endgroup$
– Blindman67
12 hours ago




$begingroup$
Second snippet second last line should not be there....
$endgroup$
– Blindman67
12 hours ago












$begingroup$
thanks for letting me know!
$endgroup$
– HelloWorld
12 hours ago




$begingroup$
thanks for letting me know!
$endgroup$
– HelloWorld
12 hours ago












$begingroup$
Please provide some context for this code. What exactly does it do? Is it Angular? If so, please add a tag and add some details. See How to Ask.
$endgroup$
– 200_success
12 hours ago




$begingroup$
Please provide some context for this code. What exactly does it do? Is it Angular? If so, please add a tag and add some details. See How to Ask.
$endgroup$
– 200_success
12 hours ago










1 Answer
1






active

oldest

votes


















3












$begingroup$

Short and sweet is best.



Your statement




"The main benefit I see here is that anybody reading the code can skim to the last line and know the overall purpose"




That makes no sense at all, you say all that is needed to understand the functions is



$scope.emailList = emailListUpdate;


Nobody jumping into someone else code will just skim, the only people that skim code are those that know the code.



You can make a few assumptions.




  • All that read your code are competent coders.

  • All that read your code have read the project specs.

  • Every line of code will be read by a coder new to the code.


Example



The best code is brief as possible without being a code golf entrant.



Notes




  • Why innerHTML, should it not be textContent????

  • This function is not a toggle. It is based on selection value.

  • The ternary expression is too long, break the line so it does need to be scrolled

  • The ternary has a syntax error. Misplaced ;

  • the ; on the last line after "}" is redundant.


Code, best option.



$scope.selectEmailUsers = event => {
$scope.selection = event.target.textContent;
$scope.emailList = $scope.selection === "Select All" ?
$scope.users.filter(user => !user.report.emailed); : ;
// ^ remove syntax error
}; // << remove the ;





share|improve this answer









$endgroup$













    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "196"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216054%2ftoggle-function-in-javascript%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3












    $begingroup$

    Short and sweet is best.



    Your statement




    "The main benefit I see here is that anybody reading the code can skim to the last line and know the overall purpose"




    That makes no sense at all, you say all that is needed to understand the functions is



    $scope.emailList = emailListUpdate;


    Nobody jumping into someone else code will just skim, the only people that skim code are those that know the code.



    You can make a few assumptions.




    • All that read your code are competent coders.

    • All that read your code have read the project specs.

    • Every line of code will be read by a coder new to the code.


    Example



    The best code is brief as possible without being a code golf entrant.



    Notes




    • Why innerHTML, should it not be textContent????

    • This function is not a toggle. It is based on selection value.

    • The ternary expression is too long, break the line so it does need to be scrolled

    • The ternary has a syntax error. Misplaced ;

    • the ; on the last line after "}" is redundant.


    Code, best option.



    $scope.selectEmailUsers = event => {
    $scope.selection = event.target.textContent;
    $scope.emailList = $scope.selection === "Select All" ?
    $scope.users.filter(user => !user.report.emailed); : ;
    // ^ remove syntax error
    }; // << remove the ;





    share|improve this answer









    $endgroup$


















      3












      $begingroup$

      Short and sweet is best.



      Your statement




      "The main benefit I see here is that anybody reading the code can skim to the last line and know the overall purpose"




      That makes no sense at all, you say all that is needed to understand the functions is



      $scope.emailList = emailListUpdate;


      Nobody jumping into someone else code will just skim, the only people that skim code are those that know the code.



      You can make a few assumptions.




      • All that read your code are competent coders.

      • All that read your code have read the project specs.

      • Every line of code will be read by a coder new to the code.


      Example



      The best code is brief as possible without being a code golf entrant.



      Notes




      • Why innerHTML, should it not be textContent????

      • This function is not a toggle. It is based on selection value.

      • The ternary expression is too long, break the line so it does need to be scrolled

      • The ternary has a syntax error. Misplaced ;

      • the ; on the last line after "}" is redundant.


      Code, best option.



      $scope.selectEmailUsers = event => {
      $scope.selection = event.target.textContent;
      $scope.emailList = $scope.selection === "Select All" ?
      $scope.users.filter(user => !user.report.emailed); : ;
      // ^ remove syntax error
      }; // << remove the ;





      share|improve this answer









      $endgroup$
















        3












        3








        3





        $begingroup$

        Short and sweet is best.



        Your statement




        "The main benefit I see here is that anybody reading the code can skim to the last line and know the overall purpose"




        That makes no sense at all, you say all that is needed to understand the functions is



        $scope.emailList = emailListUpdate;


        Nobody jumping into someone else code will just skim, the only people that skim code are those that know the code.



        You can make a few assumptions.




        • All that read your code are competent coders.

        • All that read your code have read the project specs.

        • Every line of code will be read by a coder new to the code.


        Example



        The best code is brief as possible without being a code golf entrant.



        Notes




        • Why innerHTML, should it not be textContent????

        • This function is not a toggle. It is based on selection value.

        • The ternary expression is too long, break the line so it does need to be scrolled

        • The ternary has a syntax error. Misplaced ;

        • the ; on the last line after "}" is redundant.


        Code, best option.



        $scope.selectEmailUsers = event => {
        $scope.selection = event.target.textContent;
        $scope.emailList = $scope.selection === "Select All" ?
        $scope.users.filter(user => !user.report.emailed); : ;
        // ^ remove syntax error
        }; // << remove the ;





        share|improve this answer









        $endgroup$



        Short and sweet is best.



        Your statement




        "The main benefit I see here is that anybody reading the code can skim to the last line and know the overall purpose"




        That makes no sense at all, you say all that is needed to understand the functions is



        $scope.emailList = emailListUpdate;


        Nobody jumping into someone else code will just skim, the only people that skim code are those that know the code.



        You can make a few assumptions.




        • All that read your code are competent coders.

        • All that read your code have read the project specs.

        • Every line of code will be read by a coder new to the code.


        Example



        The best code is brief as possible without being a code golf entrant.



        Notes




        • Why innerHTML, should it not be textContent????

        • This function is not a toggle. It is based on selection value.

        • The ternary expression is too long, break the line so it does need to be scrolled

        • The ternary has a syntax error. Misplaced ;

        • the ; on the last line after "}" is redundant.


        Code, best option.



        $scope.selectEmailUsers = event => {
        $scope.selection = event.target.textContent;
        $scope.emailList = $scope.selection === "Select All" ?
        $scope.users.filter(user => !user.report.emailed); : ;
        // ^ remove syntax error
        }; // << remove the ;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 12 hours ago









        Blindman67Blindman67

        8,7981521




        8,7981521






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Code Review Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216054%2ftoggle-function-in-javascript%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Сан-Квентин

            8-я гвардейская общевойсковая армия

            Алькесар