patternjavascriptMinor
How should I name a function which shows or hides an element?
Viewed 0 times
hideshowfunctionnameshowselementshouldwhich
Problem
I have a function to display or hide some elements. What I want to do is to manage the display of those elements regarding a value.
How should I replace the term
How should I replace the term
toggle? Toggling for me means 'show the element if it's hidden, or hide it if it shown.' (yes, I mean toggling visibility)function togglePassengers() {
var nbPassengers = $("#nbPassengersForTravel").val(),
$passengers = $('#d_passenger').find('fieldset');
$passengers.each( togglePassenger );
function togglePassenger(i){
$passenger = $(this);
if( ++i ---------").val('-1');
}
}Solution
Naming
I think
A function parameter should not be a single letter except in a very limited number of cases. But I don't think that this is not one of them; a reader does not automatically understand what
I would also rename
What I think your function does is show the first n passengers, and hide the rest. If that is the case, I would not use
Misc
I think
togglePassenger is not the only naming problem in your code. A function parameter should not be a single letter except in a very limited number of cases. But I don't think that this is not one of them; a reader does not automatically understand what
i actually does.I would also rename
nbPassengers, because it is unclear what nb means.What I think your function does is show the first n passengers, and hide the rest. If that is the case, I would not use
toggle, because as you said, there really is no toggling going on. You could go with something like updateVisibility to avoid this confusion, although it's a bit unconcrete. Whatever name you do choose, I would add a comment to the function explaining what it does.Misc
- I would use curly brackets even for one line statements to avoid future bugs
- your spacing is sometimes inconsistent (eg spacing around variable in function calls)
Context
StackExchange Code Review Q#77730, answer score: 4
Revisions (0)
No revisions yet.