Toggle Function in JavaScriptAjax plugin that binds to click and page loadJavaScript custom addEvent function to add event handlersChanging html element with click event: how to restore default state of element using jQuery if/else statementA better looking 'treeview' - dealing with lots of checkboxesDocumenting an intToRuleset function line by lineDetecting gyroscope data using promisesTurn script into a function which extracts the div ID and places it insideHandling arguments in a Python-like range() function in JavaScriptAbstraction of google analytics with click events and forms submission handlingpass argument to jQuery .on() event handler callback

A social experiment. What is the worst that can happen?

Loading commands from file

How do I find all files that end with a dot

Creepy dinosaur pc game identification

Is there a name for this algorithm to calculate the concentration of a mixture of two solutions containing the same solute?

Why did the Mercure fail?

Are the IPv6 address space and IPv4 address space completely disjoint?

Did arcade monitors have same pixel aspect ratio as TV sets?

What was the exact wording from Ivanhoe of this advice on how to free yourself from slavery?

Non-trope happy ending?

How should I respond when I lied about my education and the company finds out through background check?

Store Credit Card Information in Password Manager?

How to bake one texture for one mesh with multiple textures blender 2.8

Is the U.S. Code copyrighted by the Government?

Open a doc from terminal, but not by its name

The IT department bottlenecks progress. How should I handle this?

How could a planet have erratic days?

How can "mimic phobia" be cured or prevented?

Create all possible words using a set or letters

When were female captains banned from Starfleet?

Should I outline or discovery write my stories?

On a tidally locked planet, would time be quantized?

How do you respond to a colleague from another team when they're wrongly expecting that you'll help them?

It grows, but water kills it



Toggle Function in JavaScript


Ajax plugin that binds to click and page loadJavaScript custom addEvent function to add event handlersChanging html element with click event: how to restore default state of element using jQuery if/else statementA better looking 'treeview' - dealing with lots of checkboxesDocumenting an intToRuleset function line by lineDetecting gyroscope data using promisesTurn script into a function which extracts the div ID and places it insideHandling arguments in a Python-like range() function in JavaScriptAbstraction of google analytics with click events and forms submission handlingpass argument to jQuery .on() event handler callback













1












$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) =>  











1












$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) =>  









1












1








1





$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) => improve this answer









$endgroup$











    share" 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" 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

        名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局

        Is my guitar’s action too high? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)Strings too stiff on a recently purchased acoustic guitar | Cort AD880CEIs the action of my guitar really high?Μy little finger is too weak to play guitarWith guitar, how long should I give my fingers to strengthen / callous?When playing a fret the guitar sounds mutedPlaying (Barre) chords up the guitar neckI think my guitar strings are wound too tight and I can't play barre chordsF barre chord on an SG guitarHow to find to the right strings of a barre chord by feel?High action on higher fret on my steel acoustic guitar

        香港授勳及嘉獎制度 目录 勳章及獎狀類別 嘉獎等級 授勳及嘉獎提名 統計數字 多次獲頒勳章或獎狀的人士 爭議 褫奪機制 参考文献 外部連結 参见 导航菜单統計數字一九九七年七月二日(星期三)香港特別行政區的授勳制度六七暴動領袖獲大紫荊勳章 董建華被斥為肯定殺人放火董建華授勳楊光 議員窮追猛打蘋論:顛倒是非黑白的大紫荊董讚楊光有貢獻避談暴動董拒答授勳楊光原因撤除勳銜撤除勳銜撤除勳銜特首掌「搣柴」生殺權行為失當罪 隨時「搣柴」失長糧政府刊憲 許仕仁郭炳江遭「搣柴」去年中終極上訴失敗 許仕仁郭炳江撤勳章太平紳士猛料阿Sir講古—— 「搣柴」有故一九九八年授勳名單一九九九年授勳名單二○○三年授勳名單二○○八年授勳名單二○○七年授勳名單政府總部禮賓處 - 授勳及嘉獎香港特別行政區勳章綬帶一覽(PDF)(非官方)