Calling show() actually hides



  • I've been digging through Google's Closure these days. So I had a DOM element that was hidden in a page, and wanted to make it visible. They have a namespace called goog.style that has a method for that, called showElement.

    Calling that method and passing only the element you want to show causes it to... hide. Ok, so it's kinda well documented here, you have to pass a second argument that's a boolean which tells whether the element will be visible or not. But there's nothing saying that I HAVE to use the second argument. The absence of a hideElement method made me think at first that showElement would display an element by default. The source code shows why it doesn't happen.

    So is this really a trollish way to develop an API, or am I TRWTF for trusting common-sense rather than paranoically reading every documentation and source LOC before using third party stuff?



  • Well, of course it should follow the Principle of Least Surprise... but I've never used javascript... to me I have no reason to think that the second parameter is optional.



  • @Renan said:

    So is this really a trollish way to develop an API
    Yes. It's a bad default value



  • @Renan said:

    Calling that method and passing only the element you want to show causes it to... hide.
     

    You're getting a small taste of what people got when Windows 95 came out....

     Customer:  "So when I'm through using the computer, how do I stop Windows?"

     Expert:  "First, you click that button marked 'start'...."

     Customer:  "But I don't want to start, I want to stop!"

     Expert:  "Right.  And the first thing you do when you want to stop is to click start."

     Customer:  "I click start when I want to stop?"

     Expert:  "Of course you do."

     Customer:  "So what's the fellow's name on first base?"

     Expert:  "No, what is on second."

     Customer:  "I'm not asking you who's on second!"

     Expert:  "Who's on first!"

     Customer:  "He's on third; we're not talking about him...."



  • @Renan said:

    Calling that method and passing only the element you want to show causes it to... hide. Ok, so it's kinda well documented here, you have to pass a second argument that's a boolean which tells whether the element will be visible or not. But there's nothing saying that I HAVE to use the second argument. The absence of a hideElement method made me think at first that showElement would display an element by default. The source code shows why it doesn't happen.

    Maybe they're big fans of the ShowWindow function of the Windows API? Not only can this function show windows, it can also hide, maximize and minimize them, plus a few combinations and variations of these. In total there are 13 different operations this function can do. However, being C, there's no default values so the second parameter is mandatory and you're forced to look it up.

    If I'd write such a function (and I have), I'd give it a more descriptive name like set_visible or some such.



  • Since the function mucks with the "display" property, I would have called it "displayElement". But I guess this sort of logical thinking is why Google employs retards and not me.



  • @The_Assimilator said:

    Since the function mucks with the "display" property, I would have called it "displayElement"
     

    Still confusing if it has the same semantics, because in many English languages, the ordering "displayElement" would imply that the function would display the element - that is, cause it to be visible.  A more accurate, but verbose, name would be "setDisplayState" and require an argument (or, if no argument is provided, leave the state the unchanged).  That would be the most intuitive in my mind.



  • @tdb said:

    If I'd write such a function (and I have), I'd give it a more descriptive name like set_visible or some such.
     

    I'd have made three functions, [url="http://api.jquery.com/show/"]show()[/url], [url="http://api.jquery.com/hide/"]hide()[/url], and [url="http://api.jquery.com/toggle/"]toggle([bool visible])[/url]



  • The thing is that the second parameter isn't optional. JavaScript just doesn't have a way to implement required parameters.

    (Edit for clarification.)



  • @MiffTheFox said:

    The thing is that the second parameter isn't optional. JavaScript just doesn't have a way to implement required parameters.

    [code]
    function show(visible) {

    if (typeof(visible) === "undefined")

    {

    while (true) { alert("YOU MORON YOU FORGOT THE PARAMETER"); }
    

    }

    else

    {

    // do regular stuff here
    

    }

    }
    [/code]



  • @da Doctah said:

    @Renan said:

    Calling that method and passing only the element you want to show causes it to... hide.
     

    You're getting a small taste of what people got when Windows 95 came out....

     Customer:  "So when I'm through using the computer, how do I stop Windows?"

     Expert:  "First, you click that button marked 'start'...."

     Customer:  "But I don't want to start, I want to stop!"

     Expert:  "Right.  And the first thing you do when you want to stop is to click start."

     Customer:  "I click start when I want to stop?"

     Expert:  "Of course you do."

     Customer:  "So what's the fellow's name on first base?"

     Expert:  "No, what is on second."

     Customer:  "I'm not asking you who's on second!"

     Expert:  "Who's on first!"

     Customer:  "He's on third; we're not talking about him...."

    Obviously these 2 were not huge fans...


  • @ekolis said:

    <font size="2" face="Lucida Console">function show(visible) {

    if (typeof(visible) === "undefined")

    {

    while (true) { alert("YOU MORON YOU FORGOT THE PARAMETER"); }
    

    }

    else

    {

    // do regular stuff here
    

    }

    }
    </font>

    Bug report: Calling show(undefined) fails, complains about missing parameter.

     



  • @Lorne Kates said:

    I'd have made three functions, show(), hide(), and toggle([bool visible])

     

    Why would you be allowed to pass a parameter to toggle? Its behavior should be static. If the element is shown, hide it; if not, show it. Otherwise it's not toggling.

     



  • @heterodox said:

    Why would you be allowed to pass a parameter to toggle? Its behavior should be static. If the element is shown, hide it; if not, show it. Otherwise it's not toggling.

     

    toggle(condition) is a shortcut for an if block.

    $(this).toggle(condition);

    [b]versus[/b]

    if (condition)
    {
       $(this).show();
    }
    else
    {
       $(this).hide();
    }

     



  • @Lorne Kates said:

    [quote user="heterodox"]

    Why would you be allowed to pass a parameter to toggle? Its behavior should be static. If the element is shown, hide it; if not, show it. Otherwise it's not toggling.

     

    toggle(condition) is a shortcut for an if block.

    $(this).toggle(condition);

    versus

    if (condition)
    {
       $(this).show();
    }
    else
    {
       $(this).hide();
    }

     

    [/quote]

    Or if you want to make someone somewhere hate you, (condition ? $(this).show : $(this).hide)().



  • @Lorne Kates said:

    @heterodox said:

    Why would you be allowed to pass a parameter to toggle? Its behavior should be static. If the element is shown, hide it; if not, show it. Otherwise it's not toggling.

     

    toggle(condition) is a shortcut for an if block.

    $(this).toggle(condition);

    versus

    if (condition)
    {
       $(this).show();
    }
    else
    {
       $(this).hide();
    }

     

    And that is not toggling.



  • @Spectre said:

    And that is not toggling.
     

    It is if you call it without the optional bool parameter, as you'd expect.

    $(this).toggle(); // show or hide, depending on if this is already hidden or not.



  • @Renan said:

    am I TRWTF for trusting common-sense rather than paranoically reading every documentation and source LOC before using third party stuff?

    Rules of Systems Programming (from back in the mainframe days):

    1. Trust nothing and no-one.
    2. Constants aren't.
    3. Defaults won't.
    4. Don't believe documentation you didn't write.
    5. Don't assume that anything will work 'as you expect' until and unless you've tried it yourself
      (preferably on a testing system, not in production).


  • @Lorne Kates said:

    @Spectre said:

    And that is not toggling.
     

    It is if you call it without the optional bool parameter, as you'd expect.

    $(this).toggle(); // show or hide, depending on if this is already hidden or not.

    But it is not if you do provide a argument. As such, the function with argument should be called setVisibility(arg1) or the likes. The other functions should just be shorthands for that function.



  • @MiffTheFox said:

    Or if you want to make someone somewhere hate you, (condition ? $(this).show : $(this).hide)().

    Way too verbose.

     $(this)condition ? 'show' : 'hide';

     

     



  • @Watson said:

    [quote user="MiffTheFox"]

    Or if you want to make someone somewhere hate you, (condition ? $(this).show : $(this).hide)().

    Way too verbose.

     $(this)condition ? 'show' : 'hide';

     

     

    [/quote]

    Ah, forgot JavaScript did that. I would have used variable variables except they're a PHP thing.



  • @dtech said:

    But it is not if you do provide a argument. As such, the function with argument should be called setVisibility(arg1) or the likes. The other functions should just be shorthands for that function.

    I'm glad someone else thinks so; I stopped arguing because I was afraid somehow I fundamentally misunderstood what toggling meant or was going insane. :p



  • @Sutherlands said:

    @da Doctah said:

    @Renan said:

    Calling that method and passing only the element you want to show causes it to... hide.
     

    You're getting a small taste of what people got when Windows 95 came out....

     Customer:  "So when I'm through using the computer, how do I stop Windows?"

     Expert:  "First, you click that button marked 'start'...."

     Customer:  "But I don't want to start, I want to stop!"

     Expert:  "Right.  And the first thing you do when you want to stop is to click start."

     Customer:  "I click start when I want to stop?"

     Expert:  "Of course you do."

     Customer:  "So what's the fellow's name on first base?"

     Expert:  "No, what is on second."

     Customer:  "I'm not asking you who's on second!"

     Expert:  "Who's on first!"

     Customer:  "He's on third; we're not talking about him...."

    Obviously these 2 were not huge fans...

    Could someone please explain the part about the base? Baseball reference? Zero wing reference? Making out/kissing? I've seen the "start to stop" wtf before, but not first base.

    Never mind, tweaked my google search and found a skit by Abbott and Costello.



  • @Zemm said:

    Never mind, tweaked my google search and found a skit by Abbott and Costello.

    What rock have you been living under that you've never in your life heard the "Who's on First" sketch?


     



  • @Zylon said:

    @Zemm said:

    Never mind, tweaked my google search and found a skit by Abbott and Costello.

    What rock have you been living under that you've never in your life heard the "Who's on First" sketch?

    You uncultured swine!!

     

    Oddly, my wife's entire family hadn't heard of it.  Her dad caught every episode of Homestar Runner though.



  • @Zylon said:

    @Zemm said:

    Never mind, tweaked my google search and found a skit by Abbott and Costello.

    What rock have you been living under that you've never in your life heard the "Who's on First" sketch?

     

    The third rock from Sol. Us down-underers don't always get your fancy hoomore! I've never seen any Abbott and Costello skit. In fact I'd associate those names with Tony Abbott and Peter Costello!

     



  • @Zemm said:

    The third rock from Sol. Us down-underers don't always get your fancy hoomore!

    I also live in Australia, and my entire family and all my friends know about "Who's on First".

    You must just live in the sucky parts of Aus.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.