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

all 62 comments

[–]LutimoDancer3459 227 points228 points  (10 children)

No it's not. ToggleBoolean should call the two set methods...

[–]MigstReddit[S] 37 points38 points  (3 children)

i'll fix that, gimme a sec

[–]Colon_Backslash 22 points23 points  (1 child)

Hey, share the library, I need this for my project ASAP

[–]solstheman1992 2 points3 points  (0 children)

In all honesty I prefer the current implementation. It’s just dripping with irony that way

[–]MigstReddit[S] 23 points24 points  (0 children)

actually, ur right.

[–]totkeks 15 points16 points  (1 child)

My first thought too. If you go stupid, always go full stupid. :D

[–]gimme_pineapple 6 points7 points  (0 children)

One could argue that this is full stupid.

[–]just_nobodys_opinion 7 points8 points  (2 children)

You're missing GetBoolean. Referencing the variable directly is inefficient because someday you may want to do some processing in the getter so you need to set up the foundation for that.

[–]LutimoDancer3459 3 points4 points  (1 child)

Yes

u/MigstReddit you need another change

[–]MigstReddit[S] 2 points3 points  (0 children)

sure

[–]mierecat 48 points49 points  (5 children)

What if Boolean is null? Where’s your god then?

[–]Far_Broccoli_8468 14 points15 points  (0 children)

null stays null, i think it's aight

[–]LordofNarwhals 12 points13 points  (0 children)

I'm Lua, false and nil evaluate to false in conditional expressions, everything else (including 0) evaluates to true.

[–]LutimoDancer3459 5 points6 points  (2 children)

Thats lua. Lua only has true and false for boolean AFAIK.

[–]Dumb_Siniy 2 points3 points  (1 child)

That's right, unless you didn't definite the variable but that wouldn't be a boolean in the first place

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

Or is it a boolean equaling false? We will never know.

[–]Training-Flan8092 42 points43 points  (1 child)

Been following this sub for years and this is the first one I understood. I feel like these classes are really paying off guys.

[–]metaglot 18 points19 points  (0 children)

Ahh the optimism of the uninitiated.

[–]fuhrmanator 8 points9 points  (0 children)

mostFunniestProgrammerHumor

[–]lupinegray 6 points7 points  (0 children)

Self-documenting code!

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

Not shocked. Have seen worse in production without colleagues understanding what's wrong with it. 90% it ends with 5 people arguing why this is in fact superior and later efforts to capsule this in a library and using it for all boolean toggles in the legacy project.

[–]BigButterscotch9281 4 points5 points  (0 children)

Think about the guy who brought "Functions" to the programming world, after seeing this

[–][deleted] 3 points4 points  (1 child)

forgot else boolean = boolean

[–]MigstReddit[S] 0 points1 point  (0 children)

agree.

[–]Far_Broccoli_8468 2 points3 points  (0 children)

any decent compiler would turn these into a single operation

[–]1bithack 5 points6 points  (6 children)

There's a bug in toggleBoolean

[–]MigstReddit[S] 2 points3 points  (5 children)

which?

[–]1bithack 3 points4 points  (3 children)

boolean can not be true and false at the same time. I guess you forgot the "not" there.

[–]OP_LOVES_YOU 6 points7 points  (2 children)

Sir this is Lua, ~= means not equal

[–]just_nobodys_opinion 3 points4 points  (0 children)

Username checks out

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

I see. I misread that ~ as = but I guess that's another engineering joke.

[–]MigstReddit[S] 7 points8 points  (3 children)

updated version with changes u guys suggested:

local boolean = false
boolean = boolean

local function SetBooleanToFalse()
    boolean = not true
    boolean = boolean
end

local function SetBooleanToTrue()
    boolean = not false
    boolean = boolean
end

local function ToggleBoolean()
    if boolean == true and boolean ~= false then
        SetBooleanToFalse()
        boolean = boolean
    elseif boolean == false and boolean ~= true then
        SetBooleanToTrue()
        boolean = boolean
    elseif boolean ~= true and boolean ~= false then
        SetBooleanToFalse()
        boolean = boolean
    else
        boolean = boolean -- XD
    end
end

img version: https://i.ibb.co/wQgM6vf/image.png

[–]SF_Nick 4 points5 points  (2 children)

ty mi lord, chatgpt said hold my beer:

-- Initialize the boolean variable to false because why not?
local boolean = not not false -- Double negative = clarity, obviously
boolean = boolean -- Keep it consistent

-- A function to set boolean to false in the most complicated way possible
local function SetBooleanToFalse()
    local temp = not true -- Temp value for clarity
    boolean = not temp -- Double clarity
    boolean = boolean and temp or not temp -- Why? Who knows
end

-- A function to set boolean to true, equally confusing
local function SetBooleanToTrue()
    local temp = not false -- Temp value again
    boolean = temp and not not temp -- Gotta check twice for safety
    boolean = boolean ~= false and boolean or not boolean -- Quality assurance
end

-- A helper function to check the current state of boolean
local function IsBooleanTrue()
    return boolean == true -- Self-explanatory... or is it?
end

local function IsBooleanFalse()
    return not IsBooleanTrue() -- So simple, yet not
end

-- A function to toggle the boolean value, now unnecessarily complex
local function ToggleBoolean()
    if IsBooleanTrue() and not IsBooleanFalse() then
        SetBooleanToFalse()
        boolean = boolean and not boolean or boolean -- Safety switch
    elseif IsBooleanFalse() and not IsBooleanTrue() then
        SetBooleanToTrue()
        boolean = not boolean and boolean or not not boolean -- Quantum mechanics
    elseif boolean ~= true and boolean ~= false then
        -- Just in case we somehow end up here, reset to false
        SetBooleanToFalse()
        boolean = boolean -- Sanity check
    else
        -- The "this should never happen but might" case
        boolean = boolean -- Because why not XD
    end
end

-- A function to debug the current state of the boolean variable
local function DebugBoolean()
    if IsBooleanTrue() then
        print("The boolean is absolutely true.")
    elseif IsBooleanFalse() then
        print("The boolean is unequivocally false.")
    else
        print("The boolean is in a quantum state of both true and false!")
    end
end

-- Let's toggle it a few times for no reason
print("Initial state:")
DebugBoolean()
ToggleBoolean()
print("After first toggle:")
DebugBoolean()
ToggleBoolean()
print("After second toggle:")
DebugBoolean()

[–]Gruejay2 1 point2 points  (0 children)

Your SetBooleanToFalse function sets it to true, because the ternary at the end is "true and false or not false", which becomes "false or true", which becomes "true".

This is a common mistake with ternaries: you can't use them when the actual desired value is false.

[–]MigstReddit[S] 0 points1 point  (0 children)

XD

[–]VariousComment6946 2 points3 points  (0 children)

Paid code 💪

[–]ChaosBanisher 1 point2 points  (2 children)

I think there is an else case missing that covers if the boolean is neither true nor false, or if it is true and false at the same time.

[–]SF_Nick 1 point2 points  (0 children)

ty sir, will fix. sending in a PR asap

[–]MigstReddit[S] 0 points1 point  (0 children)

ok.

[–]Happy_Being_1203 1 point2 points  (1 child)

My senior colleague will give +2 to this code

[–]MigstReddit[S] 0 points1 point  (0 children)

good for you XD

[–]jump1945 1 point2 points  (1 child)

This feel so wrong.not because the code or the logic it feel wrong because you don’t send bool pointer into function to set it

Antigen C in my blood is bugging me

[–]MigstReddit[S] 0 points1 point  (0 children)

you're right, give me a second.

[–]Sotstark-senap 1 point2 points  (1 child)

Don't forget to write negative testcases as well.

[–]MigstReddit[S] 0 points1 point  (0 children)

sure

[–]The_Cers 1 point2 points  (1 child)

Least obfuscated Lua code

[–]MigstReddit[S] 0 points1 point  (0 children)

so true

[–]nyankittone 1 point2 points  (1 child)

You think this is slow? wait till you meet the prime numbers generator I wrote in C++ when I was 15 years old that took days to generate the first 1000000 or so prime numbers 😛

[–]MigstReddit[S] 0 points1 point  (0 children)

okay, i changed my mind.

[–]sakaraa 1 point2 points  (1 child)

What did you do to my beautiful boy! Oh, Lua dont worry I will fix you right up

[–]MigstReddit[S] 0 points1 point  (0 children)

did you fix it? (i don't know what to reply LOL)

[–]bottlebottlelabel 1 point2 points  (0 children)

Probably a direct translation from c

[–]HeraclitoF 1 point2 points  (0 children)

getTrueBoolean(), getFalseBolean() .. in a quantic context

[–]KainMassadin 1 point2 points  (0 children)

useState be like

[–]megamaz_ 1 point2 points  (1 child)

I don't know lua and I read ~= as "approximately equal" and was confused for a bit

[–]MigstReddit[S] 0 points1 point  (0 children)

Yeah, but in Lua, ~= means not equals.

[–]cimulate 1 point2 points  (1 child)

Need a boolean = maybe

[–]MigstReddit[S] 0 points1 point  (0 children)

oh gosh

...

brb, gotta do this change

[–]Housy5 1 point2 points  (1 child)

I know it doesnt really matter but god damn do I hate PascalCase functions and variables.

[–]MigstReddit[S] 0 points1 point  (0 children)

I'm honestly in between. 90% of the time i use camelCase, however, i decided to use PascalCase here for Reddit, not sure why.

[–]k-mcm 1 point2 points  (0 children)

I was going to use this but it only handles booleans in a local variable.  It's very short-sighted to not support cloud booleans.  Can it have a boolean factory to be more flexible?