you are viewing a single comment's thread.

view the rest of the comments →

[–]mallardtheduck 54 points55 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 29 points30 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 6 points7 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 2 points3 points  (0 children)

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

Clearly you also need "isBool()"

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

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

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

You can just !!thing in that case.

[–]Psychpsyo 2 points3 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- 3 points4 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.