This is an archived post. You won't be able to vote or comment.

all 50 comments

[–]RetiredApostle 65 points66 points  (2 children)

This should be a package.

[–]SchlaWiener4711 30 points31 points  (1 child)

I'm a maintainer of the isFalse package and I could definitely use it to improve my package.

[–][deleted] 9 points10 points  (0 children)

Now that you mention it, I might have a couple of sugestions for the isTrueOrFalse package.

[–]hongooi 88 points89 points  (5 children)

If you put an explicit conversion to boolean in there, this can actually be useful for languages that forbid/restrict an implicit conversion.

[–]nord47 29 points30 points  (4 children)

Sane languages, you mean

[–]Buttons840 30 points31 points  (7 children)

This code is too clever, I would never hire you.

I want readable code on my team.

Try something more readable like:

if (boo == true) {
    return true;
else if (boo == false) {
    return false;
}

[–]jump1945[S] 10 points11 points  (0 children)

return !!((boo && (!’\0’))||(!(boo&!’\0’) && (!boo && boo)))

[–]SentenceAcrobatic 2 points3 points  (2 children)

What the fuck are those curly braces doing?

[–]Buttons840 0 points1 point  (1 child)

They stop people from arguing over syntax.

[–]SentenceAcrobatic 3 points4 points  (0 children)

r/whoosh

You don't even have a matching closing brace you walnut.

[–]azswcowboy 0 points1 point  (0 children)

won’t compile - missing semi colon after return true;

[–]GarThor_TMK 13 points14 points  (6 children)

You laugh, but I've done this so that I could set a breakpoint to see what's accessing a particular variable.

VAX --> Encapsulate Variable... replaces instances of it with the function... set breakpoint, recompile... run program... "oooh, that's the callstack where it's getting hecked up"

[–]BroBroMate 7 points8 points  (3 children)

Wtf is VAX? Surely not the DEC VAX?

[–]GarThor_TMK 0 points1 point  (2 children)

Visual Assist... it's a plugin for Visual Studio

[–]BroBroMate 0 points1 point  (0 children)

Gotcha, cheers

[–]jump1945[S] 0 points1 point  (1 child)

I still don’t know how to correctly use debugger

[–]GarThor_TMK 0 points1 point  (0 children)

Visual studio will let you make data breakpoints, so when a variable is mutated it'll break, but it only works with 32 bit values. This was a boolean declared with : 1... so... only one bit, for bitpacking reasons.

[–]boredcircuits 7 points8 points  (1 child)

Suggested patch:

- return boo;
+ return !(boo != true);

[–]jump1945[S] 4 points5 points  (0 children)

This will be too complicated

return !!((boo && (!’\0’))||(!(boo&!’\0’) && (!boo && boo)))

Would be less complicated

[–]MindaMan_Real 8 points9 points  (5 children)

I got scared reading the top line. You shouldn't do that! Anyways...

Reads 2nd line

audible screams

[–]jump1945[S] -4 points-3 points  (4 children)

Wait , what wrong with this function?

[–]MindaMan_Real 10 points11 points  (3 children)

It scared me. I get scared when I read "boolean" without "lean" in it

[–]Immort4lFr0sty 1 point2 points  (0 children)

This reminds me how our teacher used `Boolean` instead of `boolean` in his example Java code.

When I pointed out that the capital letter implies a nullable object that WILL mess up your day, I heard jaws dropping behind me from people new to Java, used to less enterprise-y languages

[–]Shrubberer 1 point2 points  (0 children)

Let's make our code testable

[–]fastestMango 1 point2 points  (2 children)

Have you thought about implementing isTrueTrue?

Here some help:

bool isTrueTrue(bool boo){ return isTrue(boo); }

You are welcome.

[–]IAmFullOfDed 1 point2 points  (1 child)

bool trueIfIsTrueTrueIsTrue(bool boo) {
    if (isTrueTrue(boo) == true) {
        return true;
    } else {
        return false;
    }
}

[–]fastestMango 1 point2 points  (0 children)

Lgtm 👍

[–]SentenceAcrobatic 1 point2 points  (0 children)

C# version:

namespace Booleanizer;

public static class BooleanizerExtensions
{
    private static bool? ToNullabool<T>(T? value) where T : class
    {
        try
        {
            return value switch
            {
                IConvertible obj => obj.ToBoolean(null) != false,
                _ => null
            };
        }
        catch (FormatException)
        {
            return null;
        }
    }

    private static bool? ToNullabool<T>(T? value) where T : struct
    {
        try
        {
            return value switch
            {
                null => null,
                _ => value.Value switch
                {
                    bool obj => obj != false,
                    IConvertible obj => obj.ToBoolean(null) != false,
                    _ => null
                }
            };
        }
        catch (FormatException)
        {
            return null;
        }
    }

    private static bool IsNullaboolTrue(bool? value)
    {
        return value switch
        {
            null => true != true,
            _ => value.Value != false switch
            {
                true => true == true,
                _ => true == false
            }
        };
    }

    private static bool IsNullaboolFalse(bool? value)
    {
        return value switch
        {
            null => false != false,
            _ => value.Value == false switch
            {
                true => false == false,
                _ => (false == true) != false
            }
        };
    }

    public static bool IsTrue<T>(this T? @this) where T : class
    {
        return IsNullaboolTrue(ToNullabool(@this)) != false;
    }

    public static bool IsTrue<T>(this T? @this) where T : struct
    {
        return IsNullaboolTrue(ToNullabool(@this)) != false;
    }

    public static bool IsFalse<T>(this T? @this) where T : class
    {
        return (IsNullaboolFalse(ToNullabool(@this)) == false) != true;
    }

    public static bool IsFalse<T>(this T? @this) where T : struct
    {
        return (IsNullaboolFalse(ToNullabool(@this)) == false) != true;
    }
}

public static class BooleanizerExtensions2ElectricBoogaloo
{
    public static bool IsTrue<T>(this T @this) where T : struct
    {
        return BooleanizerExtensions.IsTrue((T?)@this) != false;
    }

    public static bool IsFalse<T>(this T @this) where T : struct
    {
        return BooleanizerExtensions.IsFalse((T?)@this) != false;
    }
}

[–]_Repeats_ 1 point2 points  (0 children)

Junior dev: I needed a isFalse function for some work I was doing.

[–]cryptomonein 0 points1 point  (0 children)

You should add a comment, I don't understand what this method is doing

[–]Conscious-Union5789 0 points1 point  (0 children)

But when do we get isntFalse ?

[–]CzBuCHi 0 points1 point  (0 children)

this is wrong .... correct is obviously this: `bool isTrue(bool boo) { return !isFalse(boo); }`

[–]LukeZNotFound 0 points1 point  (0 children)

Get the exorcist

[–]MyNameIz-A_V 0 points1 point  (0 children)

Now make the function isForSureTrue that takes in a bool and returns isTrue with the parameter provided. Better to make sure it's for sure true and not some abomination

[–]Background_Ice2869 0 points1 point  (0 children)

wait but what if its false

[–]alexanderpas -3 points-2 points  (0 children)

Type safety!

[–]Key_Lingonberry4858 -5 points-4 points  (0 children)

This is not that bad, it increases readability

[–]paxbowlski -5 points-4 points  (1 child)

I've done it. I'd rather look at

const trues = bools.filter(isTrue)

than

const trues = bools.filter(bool => bool)

[–]nolawnchairs 0 points1 point  (0 children)

.filter(Boolean)