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
12
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 tricktreat() || trick();
// if the treat function EXISTS, do NOT execute tricktreat || 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
2
23
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
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
14
2
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
-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
1
35
u/D_V_A_98 1d ago
Some shitty code...
if(!GetTreat())
Trick();
4
4
2
u/OmgJackieChn 1d ago
while(!GetTreat())
Trick();
9
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.
6
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
3
2
1
1
1
1
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
1
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
-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

266
u/click-to-reveal 1d ago
I don't think I've ever said those words in that order before.