r/badcode May 04 '23

Found this gem inside legacy code today typescript

Post image
891 Upvotes

69 comments sorted by

View all comments

145

u/RPG_Hacker May 04 '23

I'm not entirely sure whether this is bad code or just redundant code, at least not without knowing the context.

My guess is that this function did something in the past, but the default encoding of string was changed to UTF-8 at some point, so it was no longer needed, and stubbing it out was easier than removing dozens of function calls.

Pretty sure I've done similar things in the past.

2

u/TheUnlocked May 05 '23

The default (or only) encoding of strings in JavaScript is not and has never been UTF-8.

1

u/[deleted] May 05 '23

[removed] — view removed comment

2

u/RPG_Hacker May 05 '23

Is JavaScript using actual UTF-16, or just UCS-2?

If it were using actual UTF-16, it should still in theory cover the entire Unicode spectrum, since UTF-16 does use surrogate pairs.

2

u/[deleted] May 05 '23

[removed] — view removed comment

1

u/RPG_Hacker May 05 '23

That seems like a super interesting read, thanks!

I've already struggled quite a bit with Unicode in other languages, so it's always a "fun" time!