One of Tim‘s co-workers needed to handle a simple condition: if a control in their web app was enabled, show it, otherwise hide it.
Now, if you or I were writing that, we might write some awfully verbose code, like:
if (on)
{
d.show();
}
else
{
d.hide();
}
It’s an obvious solution, and while there are probably better ways to structure the program, it’s a perfectly viable approach. But only a weak, underskilled programmer would want to do the obvious thing. Tim’s co-worker wants you to know that they’re clever, so they wrote it this way:
d[on?'show':'hide']();
This is a “clever” abuse of a “feature” of JavaScript: all objects are really just maps. The ternary chooses a key: if we’re on, use the key show, otherwise use the key hide. Then we invoke the function at that key.
No one changed the code to make it more clear- it’s spammed all over the codebase- but they did add a comment everywhere this pattern appeared, just to avoid future confusion.
Continuously monitor your servers for configuration changes, and report when there’s configuration drift. Get started with Otter today!
Remy Porter
Source link
