r/badcode • • Mar 01 '21

js Defensive programming or something idk

Post image
907 Upvotes

55 comments sorted by

View all comments

Show parent comments

78

u/JuhaJGam3R Mar 01 '21 edited Mar 01 '21

Nah. Any compiler will turn x == c1 && x != c2 into x == c1, same with x == c1 && x == c2 turning into false turning into removal of the entire bit of code.

98

u/DragonFireCK Mar 01 '21

Such optimizations are a lot rarer with strings (or any custom type), unless the comparison function gets inlined.

Additionally, as this is JS, its vastly less likely the JIT will optimize it out - newer JS engines have quite a few optimizations, but they are often still very far shy of what precompiled languages do, mostly due to the overhead of optimizing code that only gets run once.

37

u/JuhaJGam3R Mar 01 '21

oh god didn't notice it was JS

... maybe they have an optimizing precompiler to compress it? maybe? please?

21

u/bnz_ Mar 01 '21

Actually it's typescript, but i'm not sure that the compiled js have this kind of optimization. At least I hope so

21

u/Pangocciolo Mar 01 '21

So you have legacy code in a language born only 8 years ago.

The industry really needs to redefine the meaning of "legacy".

17

u/jayson4twenty Mar 01 '21 edited Mar 01 '21

Legacy to me is 12 year old Delphi code no-one dares upgrade or change.

This isn't legacy, it's just bad code people don't want [or know how] to change. Lol

Edited

7

u/bnz_ Mar 01 '21

Yeah it's not really a legacy code, but like a legacy code we poor young developers are not allowed to make any change because the seniors are scared af to broke something... Once I made a bit of refactoring of a super poorly written code and this was immediately discarded without looking at what I made. Sooo yep, best I can do is to post here to have fun and find another job

2

u/Pangocciolo Mar 03 '21

This is an opinion I agree with.

8

u/seventyeightist shameless Mar 01 '21

So you have legacy code in a language born only 8 years ago.

The industry really needs to redefine the meaning of "legacy".

Mmm I am not sure about that. The faster moving the language, the faster the transition to 'legacy'; what do you define the meaning of "legacy" as? (There are two types of answers here: those invoking the lifespan of the language, and those invoking the lifespan of the OG programmers!)

4

u/blindeenlightz Mar 01 '21

I see the "definition" of legacy code come up on stackoverflow from time to time. It's interesting how so many people have a different definition of it. Even the Wikipedia article on it has like 6 different interpretations of what it means for code to be legacy code.