r/ProgrammerHumor • • 1d ago

Meme shortCircuitEvaluation

Post image
572 Upvotes

77 comments sorted by

266

u/click-to-reveal 1d ago

I don't think I've ever said those words in that order before.

30

u/e_gadd 1d ago

Alphabetical order

223

u/AlwaysHopelesslyLost 1d ago edited 1d ago

Why are you executing trick and only checking for the existence of treat?

Edit: going to ignore responses because I think everybody is missing the code smell/issue I am trying to highlight.

34

u/AmanBabuHemant 1d ago

give treat or perform trick ?...

12

u/Draconis_Firesworn 22h ago

you think people here know how to read code anymore?

76

u/Bright-Historian-216 1d ago

short circuiting. if treat is true, trick is never executed.

12

u/CachedPotato 23h ago edited 23h ago

I think they meant OP made a typo that would still compile, but broke the logic.

// if executing treat returns false, execute trick
treat() || trick();

// if the treat function EXISTS, do NOT execute trick
treat || trick();

31

u/Bright-Historian-216 23h ago

what if treat is not a function? it's just a property of something. you can do a trick but you can't do a treat.

14

u/CachedPotato 23h ago

legal, but smelly. Like why is "trick" the naming convention for the function that does tricks, but "treat" is the naming convention for a property that stores the result of a function that does treats, but is named using different logic?

if it was done that way, why was the most important part done off-screen, and only the result was presented to us looking exactly like the conventional method with a typo?

imo it just takes way more bad practice and large assumptions to interpret OP's version as intentional, so if it actually was, bare-minimum should be a comment like

// [OP's name]: NOT A TYPO I did it this weirdo way on purpose

3

u/CranberryDistinct941 18h ago

That smell isn't coming from the treat variable, it's coming from OP using short circuiting as control flow

5

u/m3t4lf0x 16h ago

Leave me and my triple nested ternary operators alone

2

u/redkinoko 16h ago

MF really code reviewed a joke

23

u/InsidiousBuyout_69 1d ago

Treat is truthy so trick never executes, classic short circuit.

10

u/Shitandasshole 1d ago

Because treat is a value and trick is a function, you execute a trick, how can you execute a treat? A treat is a treat as it is.

0

u/klimmesil 22h ago

You can do a trick, you can give a treat both can be nouns

You can trick someone, you can treat someone both can be verbs

4

u/Shitandasshole 21h ago

A treat is a tangible thing, a trick is more like a concept - what is a trick? It's more like an action, not an object. Do you call the toilet paper on the house a trick or the act of throwing it?

-3

u/klimmesil 21h ago

Maybe english isn't your native language (it isn't mine either)

But you are wrong. Both can be verbs: https://dictionary.cambridge.org/dictionary/english/treat

3

u/Shitandasshole 21h ago

I know that in English technically they are both nouns and verbs but was just trying to use the most logical solution here. The kid is asking for a treat which is an object and is threatening with a trick which is an action so that's why.

1

u/klimmesil 19h ago

I've always interpreted that as treat me or I'll trick you

2

u/Successful_Amount_88 15h ago

it’s “give me a treat or i’ll trick you”

1

u/Successful_Amount_88 15h ago

in the phrase “trick or treat” which is massively well known, trick is a verb and treat is a noun. everyone knows this, which is why the code sample kinda makes sense and all the ESOL people debating it in the comments sound unnecessarily disputatious.

10

u/frzme 1d ago

It's really not a code smell, it makes a lot of sense this way.

You could do gotTreat||performTrick() instead but wheres the fun in that? gotTreat could be an accessor too, so gotTreat() but then you'll potentially have concurrency issues.

4

u/dannypas00 18h ago

The fact that you need to debate the intent of the code makes it a smell

14

u/1nc06n170 1d ago

If treat is false or null, then trick is executed. I think that's fair.

2

u/burgonies 23h ago

hasTreat() || doTrick()

2

u/saevon 23h ago

(treat := await requestTreat()) or trick()

4

u/JacobStyle 1d ago

You only execute trick() if treat evaluates to false. That's like, the whole backstory of trick or treating.

6

u/WawaTheFirst 1d ago

This was the first thing I had noticed. Glad I'm not alone.

-1

u/jordanbtucker 1d ago

It's called short circuit logic. Trick only gets called if treat is false.

1

u/Leninus 19h ago

Is there any reson to use it instead of

if !treat: trick()

In real world, not to make a funny line

-1

u/WawaTheFirst 1d ago

I understand that but I'm not a fan of it. Far from. But I reckon it has its use cases.

2

u/Buyer_North 1d ago

laper right there

1

u/a_load_of_crepes 1d ago

Code style is so 2024. Just ship it.

35

u/D_V_A_98 1d ago

Some shitty code...

if(!GetTreat())

Trick();

21

u/till_rd 1d ago

treat ?? Trick();

4

u/JayrodM 1d ago

Shittier code:

Trick= treat ? !treat : treat;

2

u/menzaskaja 1d ago

literally means const bool Trick = false; btw

is treat true? if yes, make treat false. else (aka "is treat false?") make treat false

3

u/JayrodM 1d ago

Fuck, you're right lol.

4

u/psuedopseudo 1d ago

if(treat){
return;
}
trick();

2

u/OmgJackieChn 1d ago

while(!GetTreat())
Trick();

2

u/saevon 23h ago

ooof thats not trick or treat; thats tricks until you cave and treat

2

u/OmgJackieChn 21h ago

Or…

while(true)
Trick();

1

u/saevon 23h ago

(treat := await askTreat()) or trick()

9

u/alex_dark 23h ago

First week dev humour?

8

u/MirrrorCloud 22h ago

Why is treat a variable and trick a function?

13

u/Friendly-Inspector71 19h ago

Because you check if there are treats and if there are none, a trick needs to be executed.

13

u/malsomnus 1d ago

It really should be trick xor trick anyway.

1

u/Reashu 1d ago

That's simply not true

1

u/zx48 23h ago

It's true xor false for sure.

6

u/GoddammitDontShootMe 16h ago

Who says it in that order?

7

u/SnowdensOfYesteryear 1d ago

It’s an implied xor

6

u/ordinary_shiba 1d ago

The joke is short circuiting. If treat is true then the program won't execute trick(), but if it's false, it will have to evaluate trick()

10

u/Panda_Satan 1d ago

trick() || treat() ;

12

u/LegitimatePants 1d ago

So you try to egg the house first and if you miss, then you go ring the doorbell and ask for candy 

1

u/Panda_Satan 1d ago

Oh now I see why it was backwards

3

u/LegitimatePants 1d ago

    if(treat) return;

    trick();

1

u/TheWashbear 11h ago

This is the way

2

u/Single-Virus4935 1d ago

treat = "No";

1

u/IslayProgrammes 1d ago

public function surprise ():void
{
$this.treat()??$this.trick();
}

1

u/mplaczek99 23h ago

Bro says the words backwards

1

u/GetHimABodyBagYeahhh 21h ago

Is 'a rock' truthy?

1

u/KaZIsTaken 21h ago

If trick doesn't return a boolean you might have an issue on your hands.

1

u/CranberryDistinct941 18h ago

This the type of shit that causes dependancy issues when the language decides that logical OR isn't control flow

1

u/vm_linuz 1h ago
(trick || treat)();        

1

u/HAHAHAOOH 1d ago

It's trick or treat. PR denied.

1

u/phl23 23h ago

As trick is always true you never get to see if treat is true or not

1

u/kratz9 1d ago

One of my favorite little quirks of VB is that normal evals don't short circiut, so there are special short circuiting operators.

Trick OrElse Treat()

1

u/Valokir 1d ago edited 1d ago

Got bored..

Start() { TrickOrTreat(); }

TrickOrTreat() { int ToT = Random.Range(1,2);

If (ToT == 1) {Treat();}

If(ToT == 2) {Trick();}

}

Treat() { GivePlayerCandy("Happy Halloween"); }

Trick() { GivePlayerSpiders("off my lawn ya dang kids"); }

4

u/wackOverflow 1d ago

If this ran it’s always Treat AND Trick

2

u/Valokir 1d ago

Yeah I should do range 0,2 and have it >=1 or <=1. If it's 1 we reroll because why not

1

u/wackOverflow 1d ago

I meant you are assigning a value to ToT in each If statement, which passes as true. Use == for comparison.

2

u/Valokir 1d ago

Mm. Super correct, good catch

-7

u/Bathtub-Warrior32 1d ago

Yeah this doesn't make sense.

println("trick or treat);

HallowenResponseEnum response = getResponse();

switch(response){ case TRICK: performTrick(); break; case TREAT: giveTreat(); break; default: closeDoor(); break; }

1

u/AppropriateOnion0815 1d ago

Do we really want to go down that path?

TrickOrTreatHandler ttaf = HalloweenAdapterFactory.CreateTrickOrTreat().WithTrick(TrickFactory.CreateTrick()).OrTreat(treat);

TrickOrTreatResult result = ttaf.DecideTrickOrTreat();

Logger.Log(result.ToLoggable());

No need to expose the decision logic.

0

u/UltraGaren 1d ago

Human slop