The Switch Boolean



  • I don't know if this is a well-established anti-pattern already, but I happened across this construct just now (VB.NET):

    
    ...
    Select Case (someBooleanVariable)
        Case True
            ...some logic...
            Exit Select
        Case False
            ...some other logic...
    End Select
    
    

    Conditionals are just so...done, you know?



  • I like it.  This pattern can be generalized by using fall-throughs. 

     Or, "Case True to False" in VB.



  • Two things make me think that snippet was written by someone who came from a C-like language and turned their bad C style into even worse VB style.  First is the "Exit Select"; it's unnecessary and seems to be someone forcing the "break;" concept into VB.  Second, the parentheses around the condition seems to be an unnecessary carry over from the C switch syntax.



  • @Jaime said:

    Two things make me think that snippet was written by someone who came from a C-like language and turned their bad C style into even worse VB style.  First is the "Exit Select"; it's unnecessary and seems to be someone forcing the "break;" concept into VB.  Second, the parentheses around the condition seems to be an unnecessary carry over from the C switch syntax.

    You mean, like,


    if (i==1 && j==2) then
    begin
    do_something();
    end; end;




    is valid Ruby code but looks like a braindead crossing of Pascal with C?


  • Considered Harmful


    <xsl:variable name="foo" as="xs:boolean" select="true()"/>
    <xsl:choose>
    <xsl:when test="$foo">
    <xsl:message>True</xsl:message>
    </xsl:when>
    <xsl:when test="not($foo)">
    <xsl:message>False</xsl:message>
    </xsl:when>
    <xsl:otherwise>
    <xsl:message>FILE_NOT_FOUND</xsl:message>
    </xsl:otherwise>
    </xsl:choose>



  • Thank God he didn't write Select ... Case True ... Case False ... Case Else ... End Select, you know, just in case.



  • @toth said:

    I don't know if this is a well-established anti-pattern already, but I happened across this construct just now (VB.NET):

    
    ...
    Select Case (someBooleanVariable)
        Case True
            ...some logic...
            Exit Select
        Case False
            ...some other logic...
        Case FileNotFound
            ...what is this 'logic' of which you speak?...
    End Select
    
    

    Conditionals are just so...done, you know?

     

     

    FTFY.

     



  • @joe.edwards said:

    <xsl:variable name="foo" as="xs:boolean" select="true()"/>
    <xsl:choose>
        <xsl:when test="$foo">
            <xsl:message>True</xsl:message>
        </xsl:when>
        <xsl:when test="not($foo)">
            <xsl:message>False</xsl:message>
        </xsl:when>
        <xsl:otherwise>
            <xsl:message>FILE_NOT_FOUND</xsl:message>
        </xsl:otherwise>
    </xsl:choose>
    

     

    At least its simple for us java guys to parse. Gota do some code reuse, you know those booleans get confusing!

     



  • VB lets you do Select Case on booleans? I wonder what the logic is behind that one? On Nullable(Of Boolean), that might make sense...



  • @ekolis said:

    VB lets you do Select Case on booleans? I wonder what the logic is behind that one? On Nullable(Of Boolean), that might make sense...
    I'm afraid I don't see what's so wrong with it. Sure, it's superfluous, but that doesn't mean you couldn't have good reasons to use it. For example, if that Select Case was in the middle of a piece of code with a whole load of other, non-boolean, Select Cases, it would be much more readable to style it the same way. You might as well say that all Select Case use could be replaced by use of Elseif.



  • Well no, not really, at least not in .Net.  According to the spec, switch can be optimized by the compiler, whereas an if-else-if cannot (you can by reordering them, but that's another story).



  •  What if the boolean variable is nullable? Does VB.NET support nullable booleans like C# does?



  • Nullable(Of Boolean) - see above :)



  • @TheChewanater said:

    @toth said:

    I don't know if this is a well-established anti-pattern already, but I happened across this construct just now (VB.NET):

    
    ...
    Select Case (someBooleanVariable)
        Case True
            ...some logic...
            Exit Select
        Case False
            ...some other logic...
        Case FileNotFound
            ...what is this 'logic' of which you speak?...
    End Select
    
    

    Conditionals are just so...done, you know?

     

     

    FTFY.

     

    Oh my god, seriously? Keep this kind of shit on the front page comments.



  • @jmucchiello said:

     What if the boolean variable is nullable? Does VB.NET support nullable booleans like C# does?

     

    Thats what FILE_NOT_FOUND is for. Ugh where's your TDWTF history?

     



  • It's compatible with Microsoft's tri-state boolean with three unsupported values.

    http://msdn.microsoft.com/en-us/library/aa432714%28office.12%29.aspx.



  • @Manos said:

    tri-state boolean with three unsupported values
     

    I read the comment underneath as "To use the MsoTriState enumerations is a WTF job..."

    Also, five values in something called TriState - love it!



  • @Manos said:

    It's compatible with Microsoft's tri-state boolean with three unsupported values.

    .

     

    I think VHDL may 1-up (in fact, 4-up) Microsoft there. Although it has regular old true/false booleans, you more commonly use "std_logic" types which represent the hardware concept of a boolean, and has nine possible values. You can loosely translate them into English as true, false, true unless proven otherwise, false unless proven otherwise, simultaneously true and false (don't use this one, it can set fire to your circuit), lack of information, lack of useful information, somewhere between true and false, and irrelevant. Hardware has a lot of failure modes, after all. (Unfortunately, it does not have native support for files, and thus cannot fail to find them.)

    The actual hardware meanings (in the same order): high voltage low impedance, low voltage low impedance, high voltage medium impedance, low voltage medium impedance, short circuit (i.e. simultaneously connected to high and low voltages), whatever voltage uninitialised memory turned out to be when it started up, impedance so high the voltage is irrelevant, medium voltage (not really advised in a digital circuit), and "don't care" (a placeholder value used in circuit design when you want the optimiser to choose a value to put there to simplify the circuit, as it really doesn't matter).

    I've never deliberately used more than about three of these at a time. Most of them are there either to model failure modes, or there just in case someone happens to need them someday...



  • @ais523 said:

    @Manos said:

    It's compatible with Microsoft's tri-state boolean with three unsupported values.

    .

     

    I think VHDL may 1-up (in fact, 4-up) Microsoft there. Although it has regular old true/false booleans, you more commonly use "std_logic" types which represent the hardware concept of a boolean, and has nine possible values. You can loosely translate them into English as true, false, true unless proven otherwise, false unless proven otherwise, simultaneously true and false (don't use this one, it can set fire to your circuit), lack of information, lack of useful information, somewhere between true and false, and irrelevant. Hardware has a lot of failure modes, after all. (Unfortunately, it does not have native support for files, and thus cannot fail to find them.)

    The actual hardware meanings (in the same order): high voltage low impedance, low voltage low impedance, high voltage medium impedance, low voltage medium impedance, short circuit (i.e. simultaneously connected to high and low voltages), whatever voltage uninitialised memory turned out to be when it started up, impedance so high the voltage is irrelevant, medium voltage (not really advised in a digital circuit), and "don't care" (a placeholder value used in circuit design when you want the optimiser to choose a value to put there to simplify the circuit, as it really doesn't matter).

    I've never deliberately used more than about three of these at a time. Most of them are there either to model failure modes, or there just in case someone happens to need them someday...

    I doubt that MS Office interfaces directly with hardware. This "boolean" is from MS Office Developer Reference.



  • @Zemm said:

    @Manos said:

    tri-state boolean with three unsupported values
     

    I read the comment underneath as "To use the MsoTriState enumerations is a WTF job..."

    Also, five values in something called TriState - love it!

    Here's another one. The GetMessage Function returns a BOOL which can be nonzero, zero, or -1. Apparently -1 is not a nonzero value. The FILE_NOT_FOUND will be included in the next service pack.



  • @ais523 said:

    Unfortunately, it does not have native support for files, and thus cannot fail always fails to find them.
     

    FTFY.



  • @Manos said:

    It's compatible with Microsoft's tri-state boolean with three unsupported values.

    .

    From what I understand, Boolean algebra is not solely restricted to two-element Boolean algebra in a mathematical sense. Obviously in computer science it's a bit different, but if this can't reasonably be called a boolean under normal usage of the term, then what's the correct term for a boolean-style with more than two, but not unlimited, discrete possible values ?


  • Humor!



  • @blakeyrat said:

    Humor!

    AAAAAAAAAH

     

    You bitch. You scared the fuck out of me.



  • @astonerbum said:

      

    Thats what FILE_NOT_FOUND is for. Ugh where's your TDWTF history?

     

    What about the part of TDWTF history where that shit stopped being funny like 3 years ago?



  • @tOmcOlins said:

    @astonerbum said:
    Thats what FILE_NOT_FOUND is for. Ugh where's your TDWTF history?

    What about the part of TDWTF history where that shit stopped being funny like 3 years ago?

    I must have missed that memo. Could you take a picture of it on a wooden table, print it, scan it, and email it to me as a bitmap embedded in a Word document? thnx



  • That's it, I'm going to break-out the clown again.



  • @blakeyrat said:

    That's it, I'm going to break-out the clown again.
    Noooo, no, anything but clowns, please!



  • @ekolis said:

    VB lets you do Select Case on booleans? I wonder what the logic is behind that one? On Nullable(Of Boolean), that might make sense...

    Yes, because

    
    If someVar Is Nothing Then
        ...logic...
    Else If someVar Then
        ...logic...
    Else
        ...logic...
    End If
    
    

    is much harder to read than a switch.

    I don't even know whether VB.NET lets you compare a nullable value to Nothing, come to think of it, so the first two branches might have to be wrapped in a If someVar.HasValue. But the switch would have the same issue.

    EDIT: WTF is up with the tags?



  • @toth said:

    @ekolis said:
    VB *lets* you do Select Case on booleans? I wonder what the logic is behind that one? On Nullable(Of Boolean), that might make sense...

    Yes, because

    
    If someVar Is Nothing Then
        ...logic...
    Else If someVar Then
        ...logic...
    Else
        ...logic...
    End If
    
    

    is much harder to read than a switch.

    I don't even know whether VB.NET lets you compare a nullable value to Nothing, come to think of it, so the first two branches might have to be wrapped in a If someVar.HasValue. But the switch would have the same issue.

     

    Your sample code with the "Else If" construct is how we had to implement the Case construct in Stratus's less-than-full-featured version of PL/1.  After you've shown the beginning programmers how to work around the shortcoming in the available language, you just spend a few extra days hammering home the point that they don't have to indent each case another level just because there's another else in front of it.

    "if-then-else" is merely a degenerate form of case anyway.

    Unless something new has been added in the last thirty years, Nassi-Schneiderman diagrams don't have a built-in way of representing more-than-two-way decisions, but it's pretty easy to figure out how you'd draw them.

     



  • @da Doctah said:

    "if-then-else" is merely a degenerate form of case anyway.

    
    bool valid, satisfactory;
    if (x >= MIN_VAL && x <= MAX_VAL) {
    	valid = true;
    	satisfactory = true;
    } else if (joke.isRuined()) {
    	valid = false;
    	satisfactory = true;
    } else {
    	valid = false;
    	satisfactory = false;
    }
    if (satisfactory) switch (valid) {
    	case true:
    		signal(window, USER_INPUT_ACCEPTABLE);
    		break;
    	case false:
    		signal(window, USER_INPUT_UNACCEPTABLE | LOL_RUINED_JOKE);
    		break;
    } else {
    	throw new TagException();
    }
    


  • @Manos said:

    Microsoft's tri-state boolean with three unsupported values
     

    TRWTF is a data type the use of which requires a visit to the registry



  • @Zemm said:

    @Manos said:

    tri-state boolean with three unsupported values
     

    I read the comment underneath as "To use the MsoTriState enumerations is a WTF job..."

    Also, five values in something called TriState - love it!

     

    Woah, deja vu!

    [quote user="M. Webster"]@sakasune said:

    @tim said:
    sorry guys, fact is stranger than fiction:

    http://msdn.microsoft.com/en-us/library/aa432714(office.12).aspx


    I could have used that...and I love how three of them "not supported"


    "To use the MsoTriState enumerations in a WSF job..."

    I think they misspelled "WTF".

    [/quote]

    ... and again ...

    [quote user="Quietust"]Forget a boolean with three values - Micrososft also has a tristate with five values:

    http://msdn.microsoft.com/en-us/library/aa432714%28office.12%29.aspx[/quote]

    There may be more but I stopped searching after finding those two.



  • @blakeyrat said:

    Humor!

     Congrats, you just found the SFW version of goaste...



  • @DaveK said:

    There may be more but I stopped searching after finding those two.

    The other ones are unsupported anyway.


Log in to reply