Live data from Hacker News

It’s not wrong that "🤦🏼‍♂️".length == 7 (2019)

hsivonen.fi

21–30 of 315 posts

Re: It’s not wrong that "🤦🏼‍♂️".length == 7 (2019)

#21
post #8

Earlier quoted context omitted.

I think he meant to bring out defensiveness with that quip. He never says that it's a big deal, just that it's the worst way to get the length of a string containing emoji, presumably of the mainstream languages.

Why is it the worst way though?

[deleted]

Re: It’s not wrong that "🤦🏼‍♂️".length == 7 (2019)

#23
post #6

I think this is a really a naming convention issue. Len() is ambiguous, you really want either num_chars() or utfxx_len(). Of course, the issue of what counts as a character is confusing in its own right...

In Python len() on a bytes type gives you the number of bytes, and len() on a str type gives you the number of codepoints. I think that makes sense, as strings are only intended to deal with text, and you should never have to worry about byte indexing at all.

Re: It’s not wrong that "🤦🏼‍♂️".length == 7 (2019)

#24

> Python 3’s approach is unambiguously the worst one, though. Did I miss the part where he explains this take? It's made up of 5 valid unicode code units. For a language where you're not supposed to need to know the byte size semantics, the correct length should be 5. What am I missing? The close second being 17, because length in bytes. Is another fine way to represent this data, e.g. what a successful write of some…

It is wrong that "{emoji}".length == 7 -- but it's wrong because there's no such thing as the 'length' of a string out of context.

A string should be viewed as an opaque data type with views into it depending on what you're trying to do. You can have its length in the context of storage/retrieval/transmission (UTF-8 byte count), its length in the context of parsing (code points), its length in the context of editing (grapheme clusters) or length in the context of display (a bounding box in points when used in conjunction with a specific font and paragraph style attributes).

Claiming to provide an out-of-context length is strictly wrong because there's no such thing. This is where people get confused.

The attribute shouldn't be 'length' it should be something like 'countOfCodePoints' or exposed via a `CodePoints` type view.

It's particularly bad because so often (esp. for western programmers) 'countOfCodePoints' == 'countOfBytesInUTF8' == 'countOfGraphemeClusters' == """length""" so it's hella easy to accidentally write buggy software. Especially for people who don't know the above about unicode, which let's face it, most people don't. Not until they have to explain to their designer why they can't limit a label to '10 characters.' ("What do you mean there's no such thing as a character, and what am I trying to do?").

This is basically the tl;dr of the article but it's also my personal opinion.

All of this isn't about 'wrong' so much as 'imprecise and overloaded terminology making it easy to write buggy software through poor abstractions.'

If python explained which length you were getting, then this article wouldn't exist.

Re: It’s not wrong that "🤦🏼‍♂️".length == 7 (2019)

#25
this one one of those things that people point to when comparing languages, but in reality rarely matters. with Go, you just get the number of bytes, which the the correct default thing to do:

https://godocs.io/builtin#len

if the language default was anything other than this, THAT WOULD BE WRONG and unexpected. I would prefer the default to be the dumb, fast thing. then if I want the slow, fancy thing, I can import some first or third party package.

Re: It’s not wrong that "🤦🏼‍♂️".length == 7 (2019)

#27

Earlier quoted context omitted.

That makes more sense. I'm sure there could be some language out there where a empty strings length could be 7.

I have to admit, I gave that thought a bit too much time before I clicked the link and discovered what the title should have said

Based on the title, I assumed it was gonna be something about smart quotes, and I thought, "of course, another reason to hate smart quotes!"

Re: It’s not wrong that "🤦🏼‍♂️".length == 7 (2019)

#28
post #8

Earlier quoted context omitted.

I think he meant to bring out defensiveness with that quip. He never says that it's a big deal, just that it's the worst way to get the length of a string containing emoji, presumably of the mainstream languages.

Why is it the worst way though?

> Note about Python 3 added on 2019-09-09: Originally this article claimed that Python 3 guaranteed UTF-32 validity. This was in error. Python 3 guarantees that the units of the string stay within the Unicode code point range but does not guarantee the absence of surrogates. It not only allows unpaired surrogates, which might be explained by wishing to be compatible with the value space of potentially-invalid UTF-16, but Python 3 allows materializing even surrogate pairs, which is a truly bizarre design. The previous conclusions stand with the added conclusion that Python 3 is even more messed up than I thought!

Re: It’s not wrong that "🤦🏼‍♂️".length == 7 (2019)

#29

HN discards emojis in the title. The original emoji was https://emojipedia.org/man-facepalming-medium-light-skin-ton... which consists of 5 Unicode code points. Also please make sure to read the first heading after the title, which summarizes the whole point of this essay.

That makes more sense. I'm sure there could be some language out there where a empty strings length could be 7.

Not sure if HN will handle it, but this empty string's length is 7: ' '

( '\u200b'.repeat(7) )

Edit: HN does not, in fact, handle it.

Re: It’s not wrong that "🤦🏼‍♂️".length == 7 (2019)

#30
post #26

Maybe not wrong, but it's the worst option. 5 is the number of code points, and 17 is the number of bytes. Both are reasonable answers. 7 is the number of code units for utf-16. Seems like the least useful option.

That means 7 is also a measure of bytes, just slightly more awkward. So it's roughly on par with 17.

For 5, the idea is that while you might want to iterate code points, the total number of code points is less useful than either grapheme count or byte count. I think that argument makes sense.

Post reply on HN