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?
It’s not wrong that "🤦🏼♂️".length == 7 (2019)
21–30 of 315 posts
Re: It’s not wrong that "🤦🏼♂️".length == 7 (2019)
#22Re: It’s not wrong that "🤦🏼♂️".length == 7 (2019)
#23I 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...
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…
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)
#25if 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)
#265 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.
Re: It’s not wrong that "🤦🏼♂️".length == 7 (2019)
#27Earlier 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
Re: It’s not wrong that "🤦🏼♂️".length == 7 (2019)
#28Earlier 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?
Re: It’s not wrong that "🤦🏼♂️".length == 7 (2019)
#29HN 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.
( '\u200b'.repeat(7) )
Edit: HN does not, in fact, handle it.
Re: It’s not wrong that "🤦🏼♂️".length == 7 (2019)
#30Maybe 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.
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.