C# ? isn't good enough



  •  I was tracking down a bug in our c# ASP.Net application and found this little gem. I know that the original lead developer was a VB6 developer, but c'mon... and it's used some 30+ times in the code.

     

    static public String IIF(bool condition, String a, String b)
            {
                String x = String.Empty;
                if(condition)
                {
                    x = a;
                }
                else
                {
                    x = b;
                }
                return x;
            }

     



  • This reminds me of all the people trying to "improve" C using #defines



    #define BEGIN {

    #define END }



    Oh my...



  •  Oh, I see the problem: they used a local variable instead of just using multiple exit points.



  •  This actually dovetails nicely with some new functions I've written:

    static public String AND(bool condition1, bool condition2, String a, String b)
    {
    String x = String.Empty;
    if(condition1)
    {
    if (condition2)
    {
    x = a;
    }
    else
    {
    x = b;
    }
    }
    else
    {
    x = b;
    }
    return x;
    }


    static public String OR(bool condition1, bool condition2, String a, String b)
            {
                String x = String.Empty;
                if(condition1) 
                {
                    x = a;
                }
                else
                {
                    if (condition2)
                    {
                        x = a;
                    }
                    else
                    {
                        x = b;
                    }
                }
                return x;
            }
    

    static public String XOR(bool condition1, bool condition2, String a, String b)
            {
                String x = String.Empty;
                if(condition1) 
                {
                    if (condition2)
                    {
                        x = b;
                    }
                    else
                    {
                        x = a;
                    }
                }
                else
                {
                    if (condition2)
                    {
                        x = a;
                    }
                    else
                    {
                        x = b;
                    }
                }
                return x;
            }
    


  • I get it! He should have used object instead of String!



  • Well, does C# have a ternary operator? I know Java has one, but I haven't ever had a reason to check if C# has one. If it doesn't, it's not completely without merit, but I'd rather just use a normal if statement.



  •  Why initialize it to String.Empty? And if it is the latest C# he should've used var instead of string, I think it would then be a valid function for any type, and type-safe :P

     

    by the way: return condition? a:b;



  • @JohnWestMinor said:

    Well, does C# have a ternary operator? I know Java has one, but I haven't ever had a reason to check if C# has one. If it doesn't, it's not completely without merit, but I'd rather just use a normal if statement.

    Yup, C#'s got ? just like Java (and, I think, most other modern lanugages).



  • @bstorer said:

     Oh, I see the problem: they used a local variable instead of just using multiple exit points.

    dont bring that up again, everyone knows you should do both!

    [CODE]static public String IIF(bool condition, String a, String b)
    {
    String x = String.Empty;
    if(condition)
    {
    x = a;
    return x;
    }
    else
    {
    x = b;
    return x;
    }
    return x; //really really return it gaizzz!! X.X

        }
    

    [/CODE]



  • @bstorer said:

     This actually dovetails nicely with some new functions I've written:

    static public String AND(bool condition1, bool condition2, String a, String b)
    {
    String x = String.Empty;
    if(condition1)
    {
    if (condition2)
    {
    x = a;
    }
    else
    {
    x = b;
    }
    }
    else
    {
    x = b;
    }
    return x;
    }


    static public String OR(bool condition1, bool condition2, String a, String b)
            {
                String x = String.Empty;
                if(condition1) 
                {
                    x = a;
                }
                else
                {
                    if (condition2)
                    {
                        x = a;
                    }
                    else
                    {
                        x = b;
                    }
                }
                return x;
            }
    

    static public String XOR(bool condition1, bool condition2, String a, String b)
            {
                String x = String.Empty;
                if(condition1) 
                {
                    if (condition2)
                    {
                        x = b;
                    }
                    else
                    {
                        x = a;
                    }
                }
                else
                {
                    if (condition2)
                    {
                        x = a;
                    }
                    else
                    {
                        x = b;
                    }
                }
                return x;
            }
    

     

     Eek!  Night of the predicate logic whores!



  • @JohnWestMinor said:

    Well, does C# have a ternary operator? I know Java has one, but I haven't ever had a reason to check if C# has one. If it doesn't, it's not completely without merit, but I'd rather just use a normal if statement.

    Me, too. I actually prefer this to the ternary version.

    Not that the ternary operator is necessarily awful, but it's one of those things that encourages bad code in the future. if you ever have four or five of them in a single line, and you're trying to figure out what the holy hell the line does, well... you soon get to "fuck it, let's rewrite it without the ternary operator" sentiment. It's easier and quicker just to never use it in the first place.



  •  @blakeyrat said:

    Me, too. I actually prefer this to the ternary version.

    Not that the ternary operator is necessarily awful, but it's one of those things that encourages bad code in the future. if you ever have four or five of them in a single line, and you're trying to figure out what the holy hell the line does, well... you soon get to "fuck it, let's rewrite it without the ternary operator" sentiment. It's easier and quicker just to never use it in the first place.

    As opposed to the easy to read version of four or five IIF() calls in a single line.



  • @blakeyrat said:

    Me, too. I actually prefer this to the ternary version.

    Even if C# didn't have a ternary operator or if you're harbouring some religious hatred of it, pushing this logic into an "IIF" function doesn't get you the same result as either using the ternary operator or writing an if/else statement.  It's not a matter of personal preference, it's a matter of correct code.

    Which can and will get you into a lot of trouble when you start writing [code]IIF(foo != null, foo.Bar, null) ...[/code]

    @blakeyrat said:

    Not that the ternary operator is necessarily awful, but it's one of those things that encourages bad code in the future. if you ever have four or five of them in a single line, and you're trying to figure out what the holy hell the line does, well... you soon get to "fuck it, let's rewrite it without the ternary operator" sentiment. It's easier and quicker just to never use it in the first place.

    And four or five nested "IIF" functions are obviously a huge improvement.



  • @Xyro said:

    As opposed to the easy to read version of four or five IIF() calls in a single line.

    1. I don't know what "IIF" is. A quick Googling brings up some VB-specific function? I've never worked with VB.

    2) The difference is that virtually any alternatives to the ternary operator are easy to split into multiple lines. If you try that with cascaded ternary operators, you quickly run into parenthesis hell and end up breaking everything.

    Anyway, if you want to use it, use it. I'm fine with that. I'm not dictating, just preaching. :) I don't use it in my own code.



  • @blakeyrat said:

    Me, too. I actually prefer this to the ternary version.

    Not that the ternary operator is necessarily awful, but it's one of those things that encourages bad code in the future. if you ever have four or five of them in a single line, and you're trying to figure out what the holy hell the line does, well... you soon get to "**** it, let's rewrite it without the ternary operator" sentiment. It's easier and quicker just to never use it in the first place.

    Well, one thing that helps is knowing that ?: is right-associative, meaning you can do things like:

    str = num == 1 ? "one" :
          num == 2 ? "two" :
          num == 3 ? "three" :
                     "invalid";
    

    In my opinion, explicit parenthesis here actually makes it harder to tell what's going on:

    str = num == (1 ? "one" :
         (num ==  2 ? "two" :
         (num ==  3 ? "three" :
                     "invalid")));
    


  • @blakeyrat said:

    I don't know what "IIF" is. A quick Googling brings up some VB-specific function? I've never worked with VB.
     

    IIF is VB's ternary.

    The WTF here is being so nervous about using a new language that you implement syntactic constructs from your old language in the new one -- poorly.



  • @joeyadams said:

    @blakeyrat said:
    you soon get to "**** it, let's rewrite it without the ternary operator" sentiment. It's easier and quicker just to never use it in the first place.

    ...Did you really just censor the word "fuck" from someone's post? Seriously?



  • @dhromed said:

    IIF is VB's ternary.
    And more importantly, it's the method for which the source was presented in the OP.



  • @toth said:

    @joeyadams said:
    @blakeyrat said:
    you soon get to "**** it, let's rewrite it without the ternary operator" sentiment. It's easier and quicker just to never use it in the first place.

    ...Did you really just censor the word "****" from someone's post? Seriously?

    FTFY



  • @bstorer said:

    @toth said:
    @joeyadams said:
    @blakeyrat said:
    you soon get to "**** it, let's rewrite it without the ternary operator" sentiment. It's easier and quicker just to never use it in the first place.

    ...Did you really just censor the word "****" from someone's post? Seriously?

    FTFY



  • @belgariontheking said:

    @bstorer said:
    @toth said:
    @joeyadams said:
    @blakeyrat said:
    you soon get to "**** it, let's rewrite it without the ternary operator" sentiment. It's easier and quicker just to never use it in the first place.

    ...Did you really just censor the word "****" from someone's post? Seriously?

    FTFY
     



  • @bstorer said:

    @belgariontheking said:

    @bstorer said:
    @toth said:
    @joeyadams said:
    @blakeyrat said:
    you soon get to "**** it, let's rewrite it without the ternary operator" sentiment. It's easier and quicker just to never use it in the first place.

    ...Did you really just censor the word "****" from someone's post? Seriously?

    FTFY
     

     



  • @blakeyrat said:

    Not that the ternary operator is necessarily awful, but it's one of those things that encourages bad code in the future. if you ever have four or five of them in a single line, and you're trying to figure out what the holy hell the line does, well... you soon get to "fuck it, let's rewrite it without the ternary operator" sentiment. It's easier and quicker just to never use it in the first place.
     

    And sometimes a hammer is used to pound on a screw (too lazy to go to the garage to get a screwdriver, stuck in a place without a screwdriver, etc.). Therefore, it's easier and quicker just to never use a hammer in the first place.



  • @bstorer said:

     This actually dovetails nicely with some new functions I've written:

    static public String AND(bool condition1, bool condition2, String a, String b)
    {
    String x = String.Empty;
    if(condition1)
    {
    if (condition2)
    {
    x = a;
    }
    else
    {
    x = b;
    }
    }
    else
    {
    x = b;
    }
    return x;
    }


    static public String OR(bool condition1, bool condition2, String a, String b)
            {
                String x = String.Empty;
                if(condition1) 
                {
                    x = a;
                }
                else
                {
                    if (condition2)
                    {
                        x = a;
                    }
                    else
                    {
                        x = b;
                    }
                }
                return x;
            }
    

    static public String XOR(bool condition1, bool condition2, String a, String b)
            {
                String x = String.Empty;
                if(condition1) 
                {
                    if (condition2)
                    {
                        x = b;
                    }
                    else
                    {
                        x = a;
                    }
                }
                else
                {
                    if (condition2)
                    {
                        x = a;
                    }
                    else
                    {
                        x = b;
                    }
                }
                return x;
            }
    


  • @morbiuswilters said:

    @bstorer said:

    @belgariontheking said:

    @bstorer said:
    @toth said:
    @joeyadams said:
    @blakeyrat said:
    you fucking soon get to "FUCK it, let's fucking rewrite it without the fucking ternary fucking operator" sentiment. Fuck. It's fucking easier and fucking quicker just to never fucking use it in the first fucking place.

    ...Fuck, did you fucking really just fucking censor the fucking word "FUCK" from someone's fucking ****? Fucking seriously? What the fucking fuck?

    FuckedTFY

     
     

    FFuck'sS!



  • @joeyadams said:

    @blakeyrat said:
    Me, too. I actually prefer this to the ternary version.

    Not that the ternary operator is necessarily awful, but it's one of those things that encourages bad code in the future. if you ever have four or five of them in a single line, and you're trying to figure out what the holy hell the line does, well... you soon get to "**** it, let's rewrite it without the ternary operator" sentiment. It's easier and quicker just to never use it in the first place.

    Well, one thing that helps is knowing that ?: is right-associative, meaning you can do things like:

    str = num == 1 ? "one" :
    num == 2 ? "two" :
    num == 3 ? "three" :
    "invalid";

    In my opinion, explicit parenthesis here actually makes it harder to tell what's going on:

    str = num == (1 ? "one" :
    (num == 2 ? "two" :
    (num == 3 ? "three" :
    "invalid")));

     

    If you use code like that you're doing it wrong in the first place. This is exactly where switch is for. If you're using a non-value type... you're doing it wrong too.

    All good:

    switch(num) {
       case 1:
         str = "one";
       break;
       /* etc. */
       default:
          str = "invalid";
    }
    

    switch(object.someEnum){
    case someEnum.firstcase:
    str = "blah";
    break;
    /* etc. */
    default:
    str = "blup";
    break;
    }

    if(someVar == foo && (somecondition || someotherconsdition)){
    // 1
    }
    else if(someVar == foo){
    // 2
    }
    else if(someothercondition){
    // 3
    }
    else{
    // 4
    }

    I wouldn't want to see any of those with the ? operator...

     

     

    fixed your code! - dh



  • @dhromed said:

    @morbiuswilters said:

    @bstorer said:

    @belgariontheking said:

    @bstorer said:
    @toth said:
    @joeyadams said:
    @blakeyrat said:
    you fucking soon get to "FUCK it, let's fucking rewrite it without the fucking ternary fucking operator" sentiment. Fuck. It's fucking easier and fucking quicker just to never fucking use it in the first fucking place.

    ...Fuck, did you fucking really just fucking censor the fucking word "FUCK" from someone's fucking ****? Fucking seriously? What the fucking fuck?

    FuckedTFY

     
     

    FFuck'sS!

     

    Keep this up and Gamefreak will make a pokemon named "Fuck." It will be the mascot of everything AWESOME.



  •  @demausdauth said:

    static public String IIF(bool condition, String a, String b)
            {
                String x = String.Empty;
                if(condition)
                {
                    x = a;
                }
                else
                {
                    x = b;
                }
                return x;
            }

     

     it should obviously be:

            public static T IIF<T>(bool condition, T trueValue, T falseValue)
            {
                return condition ? trueValue : falseValue;
            }



  • C'mon, the real problem here is it encourages weird side effects.

    The differece between these two constructs is not always obvious:

    s = IIF(x==y, FuncWithSideEffectThatReturnsAString1(), FuncWithSideEffectThatReturnsAString2());

    s = x == y ? FuncWithSideEffectThatReturnsAString1() :
    FuncWithSideEffectThatReturnsAString2();

    For the first one both side effects take place. And even if you avoid side effects like a good and proper software engineer, you are still calling both functions and only using the result from one of them. The second line avoid wasting time running both function.

     


  • Discourse touched me in a no-no place

    @jmucchiello said:

    And even if you avoid side effects like a good and proper software engineer, you
    are still calling both functions and only using the result from one of them.
    Stop using functions in (pretend) ternary operators then. Write the code 'long hand' and stop trying, but failing, to implement features from other languages that aren't available in the one you're using now.



  • Is it not enough for one person to reply "No, C# is not good enough." and close the thread. . . . .


  • Discourse touched me in a no-no place

    @Medezark said:

    Is it not enough for one person to reply "No, C# is not good enough." and close the thread. . . . .

    "no". Happy?



  • @bstorer said:

    previously quoted boolean functions
    In [url]http://dmitry.baranovskiy.com/post/javascript-without-if[/url]:

    Boolean.prototype.ifTrue = function (f) {
        this && f();
    return this;
    };
    Boolean.prototype.ifFalse = function (f) {
    this || f();
    return this;
    };

    // so you can write

    (4 < 5).ifTrue(function () {
    alert("It is true.");
    }).ifFalse(function () {
    alert("It isn’t true.");
    });



  • @jmucchiello said:

    C'mon, the real problem here is it encourages weird side effects.

    The differece between these two constructs is not always obvious:

    s = IIF(x==y, FuncWithSideEffectThatReturnsAString1(), FuncWithSideEffectThatReturnsAString2());

    s = x == y ? FuncWithSideEffectThatReturnsAString1() : FuncWithSideEffectThatReturnsAString2();

    For the first one both side effects take place. And even if you avoid side effects like a good and proper software engineer, you are still calling both functions and only using the result from one of them. The second line avoid wasting time running both function.

    What about this?


Log in to reply
 

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