Need help with getHelp



  • I recently came across this gem of a function. It was part of the dynamic help processor which displayed help messages based on some criteria. What are those criteria? Who knows? The shadow knows...

    getHelp=function(x){return(x.t)?(h&&h[x.t])?(x.p)?(h[x.t][x.p])?(x.a&&h[x.t][x.p][x.a])?h[x.t][x.p][x.a].help||(x.nd?'':sg.noA):
    (x.o&&h[x.t][x.p][x.o])?h[x.t][x.p][x.o].help||(x.nd?'':sg.noO):h[x.t][x.p].help||(x.nd?'':sg.noP):
    (x.nd?'':sg.noP):(h[x.t].help||(x.nd?'':sg.noN)):'':(x.nd?'':sg.noH);}

    Anyone want to take the time to try to format this?  

    Or does anyone want to write a getMoreHelp() function which will provide help and/or documentation for the getHelp() function? 🙂 



  • My attempt at cleaning the code.

    getHelp = function(x) {
    	var retVal;
    	(x.t)?
    		(h&&h[x.t])?
    			(x.p)?
    				(h[x.t][x.p])?
    					(x.a&&h[x.t][x.p][x.a])?
    						h[x.t][x.p][x.a].help || (x.nd?'':sg.noA)
    						:(x.o&&h[x.t][x.p][x.o])?
    							h[x.t][x.p][x.o].help||(x.nd?'':sg.noO)
    							:h[x.t][x.p].help||(x.nd?'':sg.noP)
    						:(x.nd?'':sg.noP)
    					:(h[x.t].help||(x.nd?'':sg.noN))
    				:''
    			:(x.nd?'':sg.noH);
    	return retVal;
    } 
    

    I fecked up the indentation a bit, hope it helps some. WTF@Nested trinary operators. Didn't feel like converting to if/else



  • Formatting really doesn't help with understanding. Here's it formatted as if the ?: operation were if/else blocks.

    getHelp=
    function(x) {
    return
    (x.t)?
    (h&&h[x.t])?
    (x.p)?
    (h[x.t][x.p])?
    (x.a&&h[x.t][x.p][x.a])?
    h[x.t][x.p][x.a].help||(x.nd?'':sg.noA)
    :
    (x.o&&h[x.t][x.p][x.o])?
    h[x.t][x.p][x.o].help||(x.nd?'':sg.noO)
    :
    h[x.t][x.p].help||(x.nd?'':sg.noP)
    :
    (x.nd?'':sg.noP)
    :
    (h[x.t].help||(x.nd?'':sg.noN))
    :
    ''
    :
    (x.nd?'':sg.noH)
    ;
    }
    EDIT: ARGH Lingerance beat me to it by one minute! Curses!!



  • I'm impressed you could format it that quickly. It took me a good half hour.




  • getHelp = function(x) {
    var retVal;
    if (x.t) {
    if (h && h[x.t]) {
    if (x.p) {
    if (h[x.t][x.p]) {
    if (x.a && h[x.t][x.p][x.a]) {
    retVal = h[x.t][x.p][x.a].help || (x.nd ? '' : sg.noA)
    } else {
    if (x.o && h[x.t][x.p][x.o]) {
    retVal = h[x.t][x.p][x.o].help || (x.nd? '' : sg.noO);
    } else {
    retVal = h[x.t][x.p].help || (x.nd ? '' : sg.noP);
    }
    }
    } else {
    retVal = (x.nd ? '' : sg.noP);
    }
    } else {
    retVal = (h[x.t].help || (x.nd ? '' : sg.noN));
    }
    } else {
    retVal = '';
    }
    } else {
    retVal = (x.nd ? '' : sg.noH);
    }
    return retVal;
    }

    Might have fecked this one worse, but at least it looks better.


  • @paradochs said:

    I'm impressed you could format it that quickly. It took me a good half hour.
    Practice is all you need when dealing with the conditional operator. I love the little guy and use it whenever I feel that it would be needed, but whoever wrote this horror seems to love it too much.



  • @Welbog said:

    @paradochs said:

    I'm impressed you could format it that quickly. It took me a good half hour.
    Practice is all you need when dealing with the conditional operator. I love the little guy and use it whenever I feel that it would be needed, but whoever wrote this horror seems to love it too much.

     

    Place I used to work explicitly banned the operator in the coding standard because someone, at some point, got too friendly with it.

    Although, I can't imagine that he produced anything like this. 



  • There are a small number of places where it is valuable, for example:

    printf("The answer was %s\n", result ? "right" : "wrong");

    However it is used in cases where it does nothing more than make the code less readable. The following two examples should produce the same code on any decent modern compiler:

    [code]Value += DoubleStep ? 2 : 1;[/code]

    [code]if (DoubleStep)
    ____Value += 2;
    else
    ____Value += 1;[/code] 

     

    Edit: ___ = tab!

    {bad editor!} 



  • @GettinSadda said:

    There are a small number of places where it is valuable, for example:

    printf("The answer was %s\n", result ? "right" : "wrong");

    Anytime you use it and it isn't nested or used in a compound expression, I think it is readable.

    It's just a shorthand for "if" when you want conditional assignment only, and not conditional execution.

    Use it freely.



  • I'm gonna have to say Not a WTF on this one. It's obviously minified code, and definitely looks like Javascript. Many people offer minified versions of javascript libraries (look at Google's script files) to save on bandwidth and speed up page rendering. 



  • @savar said:

    It's just a shorthand for "if" when you want conditional assignment only, and not conditional execution.
     

    Strictly speaking, if you believe that then you don't understand the way C works (and thus C++ etc), but that's overly pedantic!

    Almost everything in C is a expression that gets evaluated. Evaluating expressions may cause quite a bit of code to be executed.

    foo() && bar();

    is an expression that can be broken down into two sub expressions: foo() and bar() where the second is conditionally executed (it is executed if the first part is logically true). The result is then the logical and of the two answers (which will always be false if the first is false). Note that in this case the result is discarded.

    There are probably people that will write:

    a > b && a++;

    rather than:

    if (a > b)  a++;

    Which is nasty. However I often write:

    if (pPtr &&  pPtr->Test()) ...

    This is much easier and should be understandable.

    Feel free to flame! 



  • @JamesKilton said:

    I'm gonna have to say Not a WTF on this one. It's obviously minified code, and definitely looks like Javascript. Many people offer minified versions of javascript libraries (look at Google's script files) to save on bandwidth and speed up page rendering. 

     

    That is a use I can live with... 



  • @JamesKilton said:

    I'm gonna have to say Not a WTF on this one. It's obviously minified code, and definitely looks like Javascript. Many people offer minified versions of javascript libraries (look at Google's script files) to save on bandwidth and speed up page rendering. 

    That many levels of nested conditionals isn't really excusable, minified or not.



  • @GettinSadda said:

    There are a small number of places where it is valuable, for example:

    printf("The answer was %s\n", result ? "right" : "wrong");

    However it is used in cases where it does nothing more than make the code less readable. The following two examples should produce the same code on any decent modern compiler:

    <font face="Lucida Console" size="2">Value += DoubleStep ? 2 : 1;</font>

    <font face="Lucida Console" size="2">if (DoubleStep)
    ____Value += 2;
    else
    ____Value += 1;</font> 

     

    Edit: ___ = tab!

    {bad editor!} 

     

    How about:

     value += (DoubleStep+1)

    or:

    value += Step 



  • You'd be screwed if DoubleStep is anything other than zero or one.

     

    In C anything that is not zero is regarded true (afaik)



  • @paradochs said:

    I'm impressed you could format it that quickly. It took me a good half hour.

    It takes about five minutes to run it through indent and tweak the options to an agreeable result. 



  • @Lingerance said:

    WTF@Nested trinary operators.

     

    It's "ternary operator" not "trinary operator". There is nothing "trinary" about it, given that it's a flow control statement, based on the outcome of a boolean/binary evaluation.

    I don't know where the misnaming came from, but it seems to have gained some popularity. I'm looking at YOU, PHP! 



  • Ok, so if you can replace this:

    if( a>b ) a++;

    With this:

    a>b && a++;

    Then should I replace this:

    if( a>b ) a++; else ohnoes();

    With this?:

    !(a>b && (a++||1))  && ohnoes();



  • @JamesKilton said:

    I'm gonna have to say Not a WTF on this one. It's obviously minified code, and definitely looks like Javascript. Many people offer minified versions of javascript libraries (look at Google's script files) to save on bandwidth and speed up page rendering. 
     

    Yes, it was minified, but not obfuscated. The original source (which I finally found) has it the exact same, only with line breaks and tabs. 



  • @JamesKilton said:

    I'm gonna have to say Not a WTF on this one. It's obviously minified code, and definitely looks like Javascript. Many people offer minified versions of javascript libraries (look at Google's script files) to save on bandwidth and speed up page rendering. 

    And yet none of them ever think to enable compression of the http stream, which takes one line in the server config and doesn't gibberify the file.

    Idiots. 



  • @savar said:

    Anytime you use it and it isn't nested or used in a compound expression, I think it is readable.

    It's just a shorthand for "if" when you want conditional assignment only, and not conditional execution.

    Use it freely.

    Ugh. I just have to think back to one particular occasion in my current Java student project...

    Somewhere along the way we decided to use Checkstyle to ensure that our code is readable. The goal was to have zero warnings with Checkstyle on. Unfortunately, Checkstyle demands that the ternary operator is evil and should never ever EVER be used. Even less unfortunately, I used it to do something an if/else block can't easily emulate: I used it to set a final variable.

    The variable would be set upon object instantiation and would depend on the user's OS. The line looked like the following (indentation modified to better fit into the page):

    private final String userConfigPath =
    System.getProperty("user.home") + System.getProperty("file.separator") +
    (System.getProperty("os.name").startsWith("Windows") ? "" : ".") + "foobar.properties"

    As far as ternary operators go, this one is very legible. But no, Checkstyle educated us that the ternary operator is evil, thus I had to replace it with a proper if/else block. Of course that would've meant changing the content of a final variable, which doesn't work. In the end we decided to use this much superior and incredibly readable code: 

    private String userConfigPath;

    // ...

    private Constructor() {
    userConfigPath = getUserConfigPath();
    //...
    }

    // ...

    private String getUserConfigPath() {
    String path = System.getProperty("user.home") + System.getProperty("file.separator");

    if (!(System.getProperty("os.name").startsWith("Windows"))) {
    path += ".";
    }
    path += "foobar.properties";

    return path;
    }

    Boy, am I glad this software helped improve the readability of my code!

    (Yes, I'm aware that it's WTFy to use a getter method to set a variable; a better name would've been initializeUserConfigPath. Maybe I'll refactor it at some point.)



  • What's so bad about using a plain boring local variable?

    private final String userConfigPath;

    // ...

    private Constructor() {

    String path = System.getProperty("user.home") + System.getProperty("file.separator");

    if (!(System.getProperty("os.name").startsWith("Windows"))) {
    path += ".";
    }
    path += "foobar.properties";
        userConfigPath = path;
    //...
    }

    Or, since that variable doesn't seem to depend on any instance properties:

     

    private static final String userConfigPath;

    // ...

    static {

    String path = System.getProperty("user.home") + System.getProperty("file.separator");

    if (!(System.getProperty("os.name").startsWith("Windows"))) {
    path += ".";
    }
    path += "foobar.properties";
        userConfigPath = path;
    //...
    }


Log in to reply
 

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