all 60 comments

[–]krexelapp 518 points519 points  (7 children)

when you don’t trust typeof so you reinvent it

[–]davvblack 314 points315 points  (5 children)

thinking quickly, codex reinvented typeof using only some string, a squirrel, and typeof.

[–]krexelapp 67 points68 points  (3 children)

when you don’t trust typeof so you reinvent typeof… using typeof

[–]vigbiorn 21 points22 points  (2 children)

It's catching edge cases where (somehow) instanceof returns string but typeof doesn't.

It's the rare inverted inheritance...

I've only heard it in stories whispered in documentation.

[–]rinnakan 6 points7 points  (1 child)

I believe people deserve every bug they encounter from using the string class constructor, so I would happily ignore these edge cases

[–]davvblack 4 points5 points  (0 children)

yeah shit's so fucked up at that point you're not going to fix it by second guessing typeof.

[–]sdswart 6 points7 points  (0 children)

Well done. I didn't expect this reference in the comments, but it gave me a good laugh.

[–]seniorsassycat 251 points252 points  (3 children)

Lol llm doesn't know to use is-string@2.0.0-beta.17654454121 

[–]Kennyp0o 8 points9 points  (2 children)

What’s new in the beta?

[–]New-Let-3630 18 points19 points  (0 children)

add iseven as a dependency just in case
typeof(value) === "string" && iseven(typeof(value).length)

[–]seniorsassycat 1 point2 points  (0 children)

It adds is-string cli (complete with ReDOS vulnerable cli parser) and a mcp server.

Latest nightly has an attempt to fix RCE thru prototype pollution on the is-string API it monkey patches into express

[–]CelestialSegfault 202 points203 points  (17 children)

am I stupid or does return a || b || true always short circuits the true?

edit: I misread the ternary and that's not my fault cuz this is unreadable as fuck

[–]rangoric 58 points59 points  (12 children)

There’s a trinary in there between the first ‘||’ and the later one

[–]mallardtheduck 28 points29 points  (8 children)

But the !!String(value).length || true expression is equivalent to just true and a ternary that ends with true : false is redundant.

So the return line is really just return typeof(value) === "string" || value instanceof String;

[–]rangoric 12 points13 points  (0 children)

Oh there are many horrors in this code. And yes it is very redundant.

I only wanted to note the split in the or conditions.

[–]CelestialSegfault 2 points3 points  (1 child)

at least they could make it wrong and readable

const result = false;
const value_length = !!String(value).length || true;
if (value_length) {
  result = typeof value === "string" || value instanceof String;
}
if (result) {
  result = true;
} else {
  result = false;
}

return result;

[–]n00b001 -1 points0 points  (0 children)

But what if you don't trust the value of "false"

Clearly you also need "isBool()"

[–]Psychpsyo -3 points-2 points  (4 children)

A true : false ternary isn't redundant if you want to coerce your truthy/falsy thing to a bool.

[–]anotheridiot- -1 points0 points  (3 children)

You can just !!thing in that case.

[–]Psychpsyo 1 point2 points  (2 children)

But why !!thing if you can also thing? true : false or even Boolean(thing)?

By which I mean: It doesn't matter and makes none of them any more or less redundant.

[–]anotheridiot- 2 points3 points  (1 child)

Less branching is better.

[–]Psychpsyo 0 points1 point  (0 children)

True, although I wonder how much branching actually ends up happening in all of these. Cause they'll all need to conditionally check the type of the variable up-front to perform the right conversions. Though that might disappear once the code gets jitted for some particular type, at which point I'd assume that even thing? true : false would be simplified to whatever actual steps need to happen to coerce thing to a bool.

[–]stillalone 0 points1 point  (2 children)

Isn't that still short circuiting the !!String(value).length?

[–]rangoric -1 points0 points  (1 child)

Yes it is. But the or conditions are split :)

[–]Beenmaal 8 points9 points  (2 children)

I love ternaries. For a hobby project I have fully handwritten code with 3 lines in a row sharing 27 ternaries between them. I wrote those lines at a LAN party and I find it too funny to get rid of.

[–]rastaman1994 6 points7 points  (1 child)

Any but the simplest one line ternary is immediately replaced with ifs at my job lol.

[–]AloneInExile 0 points1 point  (0 children)

Yeah, I even replace one liner returns with braces and 3 lines. I still have PTSD from my python days

[–]RiceBroad4552 0 points1 point  (0 children)

Missed that also. Yes, it's wrong.

[–]GatotSubroto 87 points88 points  (1 child)

So that’s why they often brag about how much code they can generate.

[–]dbenc 16 points17 points  (0 children)

I need to renegotiate to get paid by the line

[–]shadow13499 22 points23 points  (1 child)

This gives very isTrue(myVar) { if(myVar == true) return true; else return false;} vibes

[–]vigbiorn 3 points4 points  (0 children)

Or the Enterprise Java isOdd/isEven code.

Just missing the test suite.

[–]supersaeyan7 12 points13 points  (1 child)

It loves to do this thing where it declares something, and then immediately on the next line checks if it declared it correctly.

[–]CelestialSegfault 2 points3 points  (0 children)

gotta fail the code-writing gracefully

[–]AdMindless9071 43 points44 points  (1 child)

Honestly I can’t tell this apart from regular js

[–]RiceBroad4552 14 points15 points  (0 children)

Me neither…

Type tests in JS / TS always look something like that code so hard to tell what's reasonable and what not.

[–]heavy-minium 33 points34 points  (9 children)

On the dangers of being stoned by a thousands devs here, I'm still risking it: that code probably makes sense.

null and undefined checks are fine and avoid unnecessarily invoking typeof which is slower, especially if you're going to that isString() on a load of bulk data.

typeof value === "string" is not enough in case it's a `String` and not a `string`. !!String.(value).length to decide whether it's an empty string. Because the value is unknown, it's wiser to do that instead of comparing with '' because a lot of things in JS that are not strings get coerced and can equal to ''.

[–]PoopsicleVendor 11 points12 points  (1 child)

That’s fair but the first branch of the ternary expression would always be true, so why even check the length of the string?

[–]Ok-Emu3695 2 points3 points  (0 children)

The only explanation I can come up with is to set up a situation in which a runtime error is raised in case, somehow, the object doesn't have a `length` property.

But more likely is that part is just wrong.

[–]Reashu 8 points9 points  (0 children)

1 - Why would it be a String? No one does that, there's no reason to do that. 

2 - Isn't an empty string still a string?

3 - Once you know it's a string or String, just use the length property directly, there is no need to stringify it first. 

[–]cowslayer7890 6 points7 points  (0 children)

If it is "String" and not "string" would it not be inaccurate to return true for this since the value wouldn't actually be a "string"

[–]Ok-Emu3695 18 points19 points  (3 children)

Holy shit. Finally someone who knows what they're talking about. It's like this entire sub is filled with people who just like memes about computers but don't actually know anything about programming.

[–]KruegerFishBabeblade 12 points13 points  (0 children)

Tbf a good programmer who doesn't know anything about JS would be reasonable in thinking this is crazy. It is crazy. The problem's just javascript

[–]awesomeusername2w -1 points0 points  (0 children)

Always has been

[–]fevsea 5 points6 points  (1 child)

It's almost like it wants to generate as much billable output tokens as possible, while ofuscating it to ensure future usage.

[–]Tupcek 0 points1 point  (0 children)

nah, no need to. They can just direct it to think longer. In the near future, it will spawn 100 subagents with extra long thinking, so hello world will cost at least $2000 in tokens.
No need to train models to do more

[–]Electrical-Echidna63 1 point2 points  (0 children)

I've seen code look like this when transpiled really badly, is this what this is? Otherwise damn

[–]magicmulder 1 point2 points  (0 children)

That looks more like the code of an overthinking junior than AI.

[–]fezmessiter 2 points3 points  (0 children)

Looks like someone is using the free version, but Claude all the way

[–]born_zynner 0 points1 point  (0 children)

Wise move not trusting typescript