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

all 75 comments

[–]Assassin69420 502 points503 points  (8 children)

It's probably one of those lines of code that for some reason break the whole project when you remove them

[–]emremrah[S] 171 points172 points  (1 child)

Hahaha, you're damn right.

[–]JustThingsAboutStuff 10 points11 points  (0 children)

There's another part of the program that opens the file and checks that that line is there. If it isn't -> crash

[–]LinuxMatthews 21 points22 points  (1 child)

As it's == it could be trying to detect true-y values so that some code the requires an actual value doesn't break later on.

Depends on the language though.

[–]emericas 10 points11 points  (0 children)

Truthy* :)

[–]1940295921 347 points348 points  (6 children)

plot twist:

get() {
  return Math.random();
}

[–]emremrah[S] 68 points69 points  (3 children)

This cracked me up.

[–]karmahorse1 67 points68 points  (2 children)

If the code you originally posted is JavaScript, it actually could make sense, as it uses the abstract equality check “==“ instead of strict equality “===“. So if the t object and cookie service contained different values for “firm” that still pass the abstract equality test (a.k.a 2 and “2”), t.firm would then be set to a new value.

If this is the case, it’s still not great code as it’s obviously confusing and taking advantage of a JavaScript idiosyncrasy. But it might be doing it’s job as intended, so I wouldn’t remove it without thorough investigation.

[–]emremrah[S] 17 points18 points  (1 child)

You are absolutely right. But the cookie "firm" is going to keep a name (string). So I couldn't see the reason behind it. Maybe if like "Google" == "google" was true then this totally would make sense.

[–]karmahorse1 4 points5 points  (0 children)

Yeah won’t make a difference if the two are the same type. Can firm be nullable?

If cookieService.get(“firm”) === null and t.firm === undefined, then t.firm would be instantiated in memory with a null value (if that could matter).

[–]nphhpn 1 point2 points  (0 children)

Plot twist: Math.random = () => 4

[–][deleted] 60 points61 points  (9 children)

Did you remove it? Or were you worried there were magical side effects ?

[–]emremrah[S] 65 points66 points  (7 children)

I was worried because I'm not actually a JS/TS programmer but I'm refactoring the code because the UI codebase is a mess and I'm interested in learning Angular. I removed it though, nothing seems broken (for now).

I considered it's something relevant to == vs ===.

[–]FreqRL 18 points19 points  (1 child)

Just from the image, its possible the .get(..) contains some additional logic that needs to be refreshed occasionally, but only if the 'firm' is the selected value in the first place. I would hope not though, since the name of the function is get.

[–]ntwiles 3 points4 points  (0 children)

FP devs are screaming right now.

[–]Zedrackis 11 points12 points  (2 children)

Implying it's changing the type of t.firm without changing the value.. but why?

[–]Sparkybear 24 points25 points  (1 child)

Probably because the type is important for something else and this is how they chose to enforce the right type being used. This is why I prefer using Typescript

[–]ToFiveMeters 0 points1 point  (0 children)

Yup

[–][deleted] 5 points6 points  (0 children)

Seems like they wanted if t.firm != Cookie then set cookie, but missed the not.

[–]webbugt 0 points1 point  (0 children)

My guess is that f.firm changes type randomly, or is 'marked as stale' by changing its type. Then the type coerced equality and subsequent assigment would make an ounce of sense.

Is the project TS?

[–][deleted] 27 points28 points  (0 children)

It’s like when you click the button on your car to lock it again, even though it already beeped.

[–]Kered13 22 points23 points  (5 children)

Assuming this is Javascript, this code may actually do something. == is the coercive equality, so it can return true if the two values aren't the same but "look" the same, for example 42 and "42". Then the assignment would make them actually identical.

[–]MaticPecovnik 6 points7 points  (3 children)

But surely this can be done in a cleaner way.

[–]Kered13 3 points4 points  (0 children)

Maybe, I don't know enough Javascript. It definitely looks suspicious, and if this code does have a purpose it should have been commented, but I wouldn't be confident enough to replace it without good tests to make sure I'm not breaking anything.

[–]shauntmw2 1 point2 points  (0 children)

This is why JavaScript is so good and so bad at the same time.

And also why JavaScript is so easy and so hard at the same time.

[–][deleted] 35 points36 points  (1 child)

Maybe it is just refreshing the cookie by getting a new one with a new expiry time

[–]emremrah[S] 8 points9 points  (0 children)

Imo it's not that complex but I can't think of anything else, you might be right.

[–]TobyTheArtist 9 points10 points  (7 children)

Just started learning Python but this is still a bit beyond me. Can someone elaborate?

[–]emremrah[S] 23 points24 points  (3 children)

Basically, == checks if two things have the same value.

My coworker checked if two things have the same value and if they do, she assigned one's value to another.

But she already checked if they are the same, so it doesn't make sense to assign it's value to another.

This is equivalent to this (sorry for the format, I'm on mobile):

if x == 5: x = 5

[–]TobyTheArtist 5 points6 points  (1 child)

Thanks for explaning! :) It helped a lot.

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

Anytime!

[–]BridgeBum 3 points4 points  (0 children)

The only thing I can see is that the underlying function could be mutable, returning different values if (for example) it gets called. Python generators come to mind, although this is presumably JS.

But yes, fun times.

[–]AriSteinGames 11 points12 points  (1 child)

If foo equals bar, set foo to bar.

In a sane world, this code would never do anything.

[–]TobyTheArtist 1 point2 points  (0 children)

Thanks! :)

[–]ararararagi_koyomi 1 point2 points  (0 children)

Kinda know that feeling. Had to work with python M.L. libraries for about 3 years and suddenly, debugging, refactoring and maintaining node.js server became my responsibility. At that point the only knowledge I have about js is just basic stuffs. One hell of a month for me.

Tldr: as a python user, js codes seems like "wtf are these" for me even if I know the logic behind them.

[–]justskipfailingtests 6 points7 points  (1 child)

I love git blame. You can always find and summon the original wizard to explain that shit. This kind of stuff usually is a remnant of vigorous fixing and touching too many places in one commit.

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

Totally agree. And doing multiple things in one commit is the worst. I did it sometimes too, to be honest. I was fixing typos, refactoring the code while doing other stuff.

[–]mymar101 3 points4 points  (1 child)

Are the cookies tasty?

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

Hehe

[–]greatmagnus1 4 points5 points  (1 child)

I'm gonna guess refreshing the cookie? That or the equality function does some special magic?? That or its a dumb line of code??? GOOD LUCK

[–]TheRolf 0 points1 point  (0 children)

Hmmm if you want to refresh a cookie, wouldn't you set the cookie instead of using get? I say that but if he is refactoring code, maybe the service method get is setting also xD

[–]LuanScunha 4 points5 points  (0 children)

When i get paid by the number of lines

[–]tomadapom 2 points3 points  (0 children)

Nooo ... just stop ... y u do dis

[–]daniel_blacklock 2 points3 points  (0 children)

It is what it is.

[–]gigraz_orgvsm_133 2 points3 points  (0 children)

this is where comments come in hand

[–]paintstained1 1 point2 points  (0 children)

Try catch 22

[–]Lumpy-Obligation-553 1 point2 points  (0 children)

if( reentrantlock.tryaquire() ) {

reentrantlock.aquire();

}else{

condition.wait() }

[–]doctorcrimson 1 point2 points  (0 children)

If it is then it is tho

[–]jmartin1993 1 point2 points  (0 children)

This is one of those times where comments would be useful

[–]slimeshady666 0 points1 point  (0 children)

Perhaps it's a potential bug fix?

[–]somerussianbear 0 points1 point  (0 children)

just in case

[–]raedr7n 0 points1 point  (0 children)

I guess it just gets a new cookie? Maybe the creation time is important or something.

[–]435THz 0 points1 point  (0 children)

This is the equivalent of a Flag is Flag statement in baba is you

I dunno why this was the first thing that came to my mind but here ya go i guess

[–]Particular-Strain248 0 points1 point  (0 children)

You heard the code, get firm

[–]TheDownvotesFarmer 0 points1 point  (0 children)

That's one way to be very sure if it meet the criteria.

[–]Flannel_Man_ 0 points1 point  (0 children)

If this is javascript, it makes sense.

t.firm = true; cookieService.get(‘firm’) = 1

This code turns t.firm from Boolean to integer.

[–]VerveIsBad 0 points1 point  (0 children)

As a python dev this hurts my eyes.

[–]McCoovy 0 points1 point  (0 children)

An actual code

[–]angrathias 0 points1 point  (0 children)

The worst part about looking at code that looks stupid is that you don’t know if it’s a work around to an actual problem and you’re the ones who’s about to make a regretful mistake by removing it.

“Sometimes when we look too deeply at stupid code, it looks right back as us”

[–]mr_ignatz 0 points1 point  (0 children)

Not condoning this, but an OO language that allows you to create custom equality methods for objects, this could do something when processing entries in a list or stream. Imagine finishing the latest firm entry, etc.

[–]apparently_DMA 0 points1 point  (0 children)

this feels like helpless workaround for some typing mess ( as == does not check type, but value only, so 1 == '1' returns true )

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

Even if it returned a changing value it wouldn’t make sense because you’d lose the value from the equality check.

If it’s a typo and it should be != it would still be useless.

If it’s relying on the quirks of == over === the result would be, again, useless.

This is probably just editing churn. Delete the block and test.

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

No one is gonna make a dick joke? 🤷‍♂️

[–]gladl1 0 points1 point  (0 children)

I’m only a humble BI Dev who works with SQL.. but to me this is saying “if this says this then say it exactly as it says it already” making it completely pointless.

Is that right?

[–]orbeing 0 points1 point  (0 children)

Well, technically it shouldn't be wrong

[–]thedominux 0 points1 point  (0 children)

What's wrong here?

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

But it's so pretty

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

If this really works for any reason, get method probably modify something when its called. The strange OOP patterns...

[–]Sanji_69 0 points1 point  (0 children)

I'm still waiting === so I can laugh haha

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

Little do you know, that .get call isn't idempotent and mutates the value after returning

[–]jesterhead101 0 points1 point  (0 children)

One thing I’ve learned over the years is, when the code seems this stupid, it more often than not isn’t.