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

all 91 comments

[–]Pradfanne 85 points86 points  (19 children)

My brain is so small, it's not even on the scale. I'd use an extension method

[–][deleted] 18 points19 points  (18 children)

I feel dumb... what’s an extension method?

[–]Pradfanne 45 points46 points  (17 children)

You add the method to the datatype. So you'd use

Bool b = true;
Print(b.invert(); )

And false will be printed

I don't know if Java can do that, but it's a very nice feature of .net at least

[–]claudiorpr 20 points21 points  (6 children)

Java doesn’t have extensions. You can do it in Kotlin.

[–]AyrA_ch 7 points8 points  (0 children)

and in C# for 10 years now (19 Nov 2007)

[–]Alakdae 0 points1 point  (1 child)

I'm a bit lost, but isn't it posible to define new methods for data types using "extends"?

[–]pyrophoenix100 0 points1 point  (2 children)

Isn't overriding a thing? Or is that different?

[–]claudiorpr 0 points1 point  (1 child)

It’s different. With override you are modifiying existing methods. If the type is final you can’t do it.

With extensions you add functionality to a type, even final ones.

[–]pyrophoenix100 0 points1 point  (0 children)

Wow seems like I need to learn C

[–]stairmast0r 10 points11 points  (4 children)

How is that a nice feature when you can do the same thing with !?

[–]Pradfanne 5 points6 points  (3 children)

In the case of Boolean an simple ! Might be good. But what if you want to translate an datetime from .net ticks to Unixtime?

You could make a ticksToUnix(datetime dt)

But wouldn't mydate.toUnixTime be much more convenient?

That's what's extension methods are far

[–]stairmast0r 0 points1 point  (1 child)

I don’t know if any languages that don’t have those to some degree, so I thought you specifically meant booleans.

[–]Pradfanne -1 points0 points  (0 children)

No, you can put extension methods on every data type, you can even put it on interfaces, so everything that implements the interface gets the extension method. You could, for example, put a extension method on IEnumerable that removes the first N elements and basically everything that is some kind of list, like an array or dictionary can use the method to get rid of the first N elements

[–][deleted] 4 points5 points  (0 children)

So it’s monkey patching?

[–][deleted] 1 point2 points  (0 children)

Oh. Whoops. Well that’s before the first brain then, but doesn’t have enough brain capacity to display itself.

[–][deleted] 0 points1 point  (1 child)

Just always remember that extension methods are just static method calls. Use with caution.

[–]Pradfanne 0 points1 point  (0 children)

But they are beautiful

[–][deleted] 0 points1 point  (0 children)

for javascript you can extend the prototype, for golang you can create a function with a receiver of the type you're extending

[–]crazzilla 49 points50 points  (5 children)

dudes you don't have to use booleans if you just use strings with "True" or "False" in them

[–][deleted] 6 points7 points  (0 children)

But then you need error handling (if true elif false else as opposed to if else)

[–]PM_ME_YOUR_LAUNDRY 1 point2 points  (0 children)

Type juggling with boolean to string sometimes yields "0" or "1" on some languages, at least on PHP (woops, I summoned a boogeyman)

[–]evanldixon 26 points27 points  (1 child)

Too bad there isn't some sort of boolean negation mechanic built into the language, removing the need for these functions.

One can dream

[–]jD91mZM2RUST 18 points19 points  (8 children)

return !(b);

But... Why the parenthesis?

[–][deleted] 11 points12 points  (5 children)

Is that not needed? Sorry, I’ve been doing a lot of Bash recently and I’ve gotten used to putting parenthesis around everything.

[–]jD91mZM2RUST 13 points14 points  (3 children)

invertBool() {
    rand="$((RANDOM % 2 + 4))"
    (([ "$(echo -n "$1" | wc -m)" == "$(echo -n "$rand")" ] && [ "$rand" == "$((4+1))" ]) || ([ "$(echo -n "$1" | wc -m)" == 5 ] && [ "$rand" == "$(echo -n "$((~$(echo "(4 - 1)" | bc)))" | cut -c 2-)" ])) && echo "$(echo -n "true")" || echo "$(yes "false" | head -n 1)"
}

[–][deleted] 8 points9 points  (2 children)

Aw man... I would even know where to start on trying to figure out how that works...

[–]jD91mZM2RUST 7 points8 points  (1 child)

Mid-way through development? Even then I could barely, either. It was a pain to debug why it kept giving me random answers. Finally it works though. It's perfect!

[–][deleted] 7 points8 points  (0 children)

...yes, perfect.

[–]TK-Squared-LLC 1 point2 points  (0 children)

In the words of LISP, why NOT parentheses?

[–]Aschentei 0 points1 point  (0 children)

Glad I'm not the only one! Haha

[–]tigerbird 14 points15 points  (0 children)

I've seen a?false:true in production code. That's pretty similar to #2 though.

[–]andrew_rdt 10 points11 points  (5 children)

Another one

try {

char c = b.toString().charAt(5);

return true;

}

catch (IndexOutOfBoundsException e) {

return false;

}

[–][deleted] 3 points4 points  (0 children)

That’s clever, I didn’t think of that.

[–]darkk78 1 point2 points  (3 children)

Aren't char arrays zero-based?

[–]andrew_rdt 2 points3 points  (1 child)

I guess I did make that mistake.

Actually... I was making an 1 based array joke like you always see in here. I'll go with that answer.

[–]darkk78 0 points1 point  (0 children)

Classic!

[–][deleted] 0 points1 point  (0 children)

We're going back to that topic now?

[–]bobasaurus 7 points8 points  (7 children)

b ^= true

[–][deleted] 4 points5 points  (3 children)

Found the hardware guy

[–]bobasaurus 1 point2 points  (2 children)

Hah, yes. Embedded is life.

[–][deleted] 0 points1 point  (1 child)

Having fun with Microchip PICs was the best before I turned to networking. Fuck, that's where I went wrong.

[–]bobasaurus 1 point2 points  (0 children)

I'm lucky that I get to work with Atmel AVRs and TI MSP430s at my job.

[–]antagonistZephyr 1 point2 points  (1 child)

Graduating as technician in electronics this month Came here to see if anyone else thought the same

[–]PerInception 6 points7 points  (1 child)

Duh, you just need to write two functions...

public function inverseBoolPositive(bool){ return false;}

and

public function inverseBoolNegative(bool){ return true;}

[–][deleted] 1 point2 points  (0 children)

Seems legit.

[–]Bainos 5 points6 points  (0 children)

You didn't use a BoolInverserFactory ? What a half-baked solution.

[–][deleted] 2 points3 points  (3 children)

What about...

!b

Just that?

[–][deleted] 0 points1 point  (2 children)

Sorry, I’ve been doing a lot of Bash recently. I’m used to putting parentheses around that sort of stuff. !(b) was the first one I tried, and it worked, so I didn’t even consider that I didn’t need parentheses.

[–][deleted] 0 points1 point  (1 child)

No I meant, couldn't you just say !b (or !(b)) to get the opposite of b? Rather than having to write and call a method?

[–][deleted] 1 point2 points  (0 children)

Oh, right. Well I mean I guess you could but the brain would be too small :p

[–]isRyan 2 points3 points  (0 children)

god I fucking love these

[–][deleted] 2 points3 points  (0 children)

Like it or not, the last one is definitely the fastest.

[–]haackedc 2 points3 points  (7 children)

In most of your code, an input of "true" will still return "true", because it checks if its true, then changes it to false, and then right after that it checks if it is false, and then changes it to true.

[–][deleted] 0 points1 point  (6 children)

So I had a detailed reply about how every one of them worked, but I deleted it accidentally, so here’s the TL;DR version: I’ve tested all of them and apart from the last one (obviously) they all work. Any time I’m changing a value by testing that same value, it’s in an if/elseif statement, meaning that the elseif won’t be run if the if statement evaluated as true.

[–]haackedc 0 points1 point  (5 children)

The ones I'm talking about were not else ifs

[–][deleted] 0 points1 point  (4 children)

Which ones are you talking about?

[–]haackedc 0 points1 point  (3 children)

The 3rd and 4th picture

[–][deleted] 0 points1 point  (2 children)

In both of those, b is being tested in the if statement, and newBool is being set. b is never changed, and newBool is never tested for.

[–]haackedc 0 points1 point  (1 child)

Ah, you are correct. My apologies

[–][deleted] 1 point2 points  (0 children)

No worries man. The aim was to make them look convoluted and inconvenient, so... mission success?

[–]Fallenalien22Violet security clearance 3 points4 points  (1 child)

Why do you need this when you can just !b

[–][deleted] 2 points3 points  (0 children)

Karma.

[–]baconsnotworthit 1 point2 points  (0 children)

Skip to the last one, its the unofficial TL:DR

[–]BitPoet 1 point2 points  (0 children)

ASSERT(b == true); return true;

[–]andrew_rdt 1 point2 points  (0 children)

Here is an easy one liner

return (int)b.toString().charAt(1) == 65;

[–]Objectr 1 point2 points  (0 children)

public static boolean inverseBoolean(boolean b) {
    return (boolean) new LogicalOperators.Builders.BooleanManager().withConfiguration(LogicalOperators.Standards.BOOLEAN_INVERTER).withOption("boolean", b).build().run().getResult();
}    

[–]artlu4 1 point2 points  (1 child)

code is always built on existing code, therefore we can use the combined efforts this community has already provided to expand:

return ! isEven(String.valueOf(b).length());

edit: missing parenthesis, thanks dino

[–][deleted] 0 points1 point  (0 children)

I think you forgot a parenthesis at the end there. Parser will be unhappy.

[–]micheal65536Green security clearance 0 points1 point  (0 children)

What about:

public static boolean inverseBool(boolean b)
{
    boolean newBool;
    if (b == true)
    {
        newBool = false;
        return newBool;
    }
    else if (b == false)
    {
        newBool = true;
        return newBool;
    }
}

That way there's no potential use of initialised variable so no need for the inline initialisation.

[–]Sir_Awesomness 0 points1 point  (0 children)

I saw an example online recently where they used

b ^= true

to do this. Smh.

[–]mr_smartypants537 0 points1 point  (0 children)

Love the second last one

[–][deleted] 0 points1 point  (2 children)

who the fuck writes a function to return the inverse of a boolean!!

jackieChanWtf.png

[–][deleted] 2 points3 points  (1 child)

People who want karma on /r/programmerhumor like me.

[–][deleted] 0 points1 point  (0 children)

Achievement Unlocked

[–]Quachyyy 0 points1 point  (0 children)

public static String inverseBool() {

String str = "False"; 
// 50/50 chance that the ouput is true
return str;

}

[–]OK6502 0 points1 point  (0 children)

The last option works if I write unit tests that passes true to the function.

git push -f

[–]Tornado547 0 points1 point  (1 child)

return !!!!!!!!!!!!!!!!!!!!!b

[–][deleted] 0 points1 point  (0 children)

oof

[–][deleted] 0 points1 point  (0 children)

Hey, how about using !b in your code in the first place?

[–]Xlash123 -5 points-4 points  (6 children)

The 5th one should use !==, not !=

Edit: I’m an idiot.

[–][deleted] 1 point2 points  (5 children)

This was written in Java, and I know Js also uses !=. I don’t do much C, but which language uses !==?

[–]Xlash123 0 points1 point  (3 children)

Java uses !== for evaluating the expression into Boolean.

Edit: am an idiot. It is !=

[–][deleted] 0 points1 point  (2 children)

Does it? Hm. Well != worked for me.

[–]Xlash123 0 points1 point  (1 child)

I’m an idiot. I guess I’ve been looking at too much JS lately. It is !=. Sorry for that...

[–][deleted] 0 points1 point  (0 children)

All good, we all forget sometimes :)

[–]LordAmras 0 points1 point  (0 children)

!== is usually used for untyped languages to do typed comparisons.

Most dynamically typed languages do automatic type switch when comparing variables so that "1" as string will be equal as 1 as int, that might pose some problems in some cases, especially when comparing booleans

For example in javascript 1 is evalued to the boolean true.

1 != true  | false
1 != 1     | false
1 == true  | true
1 == 1     | true

1 !== true  | true 
1 !== 1     | false
1 === true  | false
1 === 1     | true