134

I've recently spent a significant amount of time debugging a problem with a string inside my code.

This produced an error:

"root:password@tcp(127.0.0.1:3306)/employees?readTimeout=15m‌​"

This worked fine:

"root:password@tcp(127.0.0.1:3306)/employees?readTimeout=15m"

As you can see the strings look exactly the same, but when printed out in binary format:

First string:

[114 111 111 116 58 112 97 115 115 119 111 114 100 64 116 99 112 40 49 50 55 46 48 46 48 46 49 58 51 51 48 54 41 47 101 109 112 108 111 121 101 101 115 63 114 101 97 100 84 105 109 101 111 117 116 61 49 53 109]

Second string:

[114 111 111 116 58 112 97 115 115 119 111 114 100 64 116 99 112 40 49 50 55 46 48 46 48 46 49 58 51 51 48 54 41 47 101 109 112 108 111 121 101 101 115 63 114 101 97 100 84 105 109 101 111 117 116 61 49 53 109 226 128 140 226 128 139]

The second string happens to have extra two invisible Unicode characters at the end 226 128 140 226 128 139 that represent ZERO WIDTH NON-JOINER and ZERO WIDTH SPACE.

As it turns out, Stack Overflow inserts invisible characters inside long code comments.

When writing a comment like this:

aaaaaaaaaaaaaaaaaaaa‌​aaaaaaaaaaaaaaaaaaaa‌​aaaaaaaaaaaaaaaaaaaa‌​aaaaaaaaaaaaaaaaaaaa‌

What really happens under the hood is this:

[97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 226 128 140 226 128 139 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 226 128 140 226 128 139 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 226 128 140 226 128 139 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 97 226 128 140]

My question is, why does Stack Overflow do this?

Why is inserting invisible characters into code snippets and effectively creating debugging nightmares considered a good solution?

38
  • 7
    Yes, this is by design to break up long lines so they wrap. Commented Apr 30, 2017 at 15:01
  • 11
    See Occasionally, the Unicode character sequence U+200C U+200B (ZWNJ ZWSP) is inserted into comments for the Meta cross-site dupe. Commented Apr 30, 2017 at 15:02
  • 28
    To be honest, I would much prefer if the break characters were visible, something like <break> instead of inserting invisible magic. Worst thing about it is that once you copy this into your editor, you can't tell it's there. And so your code could bug out at some point in the future and you won't have a clue why that is.
    – Rtsne42
    Commented Apr 30, 2017 at 15:28
  • 4
    They could just insert <wbr> tags instead though
    – SeinopSys
    Commented Apr 30, 2017 at 22:29
  • 3
    @SeinopSys As noted in a comment on the answer to the linked MSE question, <wbr> is not supported in Internet Explorer.
    – user743382
    Commented Apr 30, 2017 at 22:38
  • 25
    @hvd I would argue that functional effects outrank cosmetic ones by far. Even more so if they only affect users of an outdated and discontinued browser (note that Edge does support it).
    – Siguza
    Commented May 1, 2017 at 0:44
  • 12
    or even better: wordbreak:break-all Commented May 1, 2017 at 5:17
  • 7
    @ChristianGollhardt: That was my immediate thought too (with the hyphen, though: word-break: break-all). Even works on IE8. Commented May 1, 2017 at 10:40
  • 6
    Here's a feature request to change the design to stop inserting these, with a specific proposal for what to replace it with (which works on Firefox): meta.stackexchange.com/questions/295425/… Commented May 1, 2017 at 12:01
  • 13
    Copying and pasting text from a comment and expecting it to work in code is the bug here... Commented May 1, 2017 at 17:08
  • 25
    @MikeMcCaughan: You are missing a vital detail here: Even if you understand what the code does, and could replicate it yourself, you have no way to see, that the code you pasted looks different to a compiler/interpreter than to a human reader. The bug really is, that a developer-centric site inserts invisible Unicode voodoo. Commented May 1, 2017 at 21:39
  • 11
    @MikeMcCaughan - Code which is not meant as an answer, nor valid as an edit, can be very handy in a comment. As user2357112 points out in a comment on Christian Gollhardt's answer below, a commenter could well be using code to help with debugging, effectively asking for clarification, which is one of the stated purposes of comments.
    – John Y
    Commented May 1, 2017 at 21:49
  • 4
    @MikeMcCaughan: Except, the question/answer would never reach that state, because the inquiry to copy the code and respond with the results will never successfully come to a conclusion. Commented May 1, 2017 at 22:08
  • 23
    @MikeMcCaughan: You find it ridiculous to use comments for their intended purpose? I must say, I find that a tad bit ridiculous. Commented May 1, 2017 at 22:23
  • 5
    @Sklivvz: you have my sympathy! But in the end, it was a design nonetheless, however it came about, and not an accident caused by someone tripping over the cable ;-) Commented May 1, 2017 at 22:38

1 Answer 1

146

I disagree with [status-bydesign] (which implies it is not subject to change or correction).

Having invisible characters might be ok for other Stack Exchange Communities, but not for Stack Overflow. Our community uses code snippets on a regular basis.

The problem stated in the question, is reason enough to change something.

Maybe the use of word-break:break-all for code comments would help.

But having a code snippet, which is not working because of additional invisible characters, is really bad.

Also remember: HTML is all about Semantics, CSS all about Styling. Changing the semantics to reach a design goal is a second reason against the current implementation.


T.J. Crowder created a feature-request, in case you want to support this.

19
  • 5
    Putting code in a comment awfully looks like answering the question (or at least part of it) with a comment. So maybe should such comment simply be avoided and replaced by an actual answer, even incomplete.
    – Frédéric
    Commented May 1, 2017 at 11:54
  • 6
    @Frédéric not necessary. Sometimes I am asked about some code, but doesn't feel like it should go to the answer/a seperate answer as it is a little bit to localized. Commented May 1, 2017 at 12:02
  • 4
    @Frédéric I don't think that's feasible, I often write follow-up comments that wouldn't be appropriate as a separate answer.
    – Rtsne42
    Commented May 1, 2017 at 13:31
  • 1
    @Frédéric True, I was thinking more in terms of giving follow up comments under someone else's answer. Let's say you come across an answer that has an outdated code snippet and you'd like to post a comment to clarify how the code should look. You can't really make a separate answer out of that, and you can't really edit someone's answer directly. The only thing you can do is add a comment with code inside it.
    – Rtsne42
    Commented May 1, 2017 at 14:24
  • 12
    If the code is good enough to be copied, then it probably should be posted as an answer instead. Comments are not meant to be permanent, they are disposable by their very nature. Commented May 1, 2017 at 15:57
  • 5
    And inserting the zero-width characters is certainly by design; the site developers explicitly put them in there. It's not a bug, it is a specific feature that was crafted to work this way. It's fine to disagree with wether or not it's a good idea to do it this way, but disagreeing with the status is a little.. silly, because that implies that the feature was put in by accident instead. Commented May 1, 2017 at 15:59
  • 14
    @MartijnPieters: status-bydesign "indicates that the specified behavior is intentional and not subject to change or correction", so we're going to disagree with that all we want. The design is actively harmful to user requirements. The design has a bug. We're not going to complain any less about a bug simply because the design says it should be that way. Commented May 1, 2017 at 17:51
  • 36
    @FélixGagnon-Grenier: As opposed to what, retyping it? If someone leaves me a comment saying that I should show them the output of System.out.println(transactionAwarePersistenceManagerFactoryProxy.getTargetPersistenceManagerFactory()) to help them debug my code, am I wrong to copy-paste that instead of retyping? Retyping is a source of endless bugs and something to be avoided wherever possible. Working copy-paste functionality is crucial. Commented May 1, 2017 at 18:19
  • 8
    If the answer is really "don't put long lines of code in comments", then don't allow them to be posted instead of poisoning them with invisible characters. Commented May 1, 2017 at 21:27
  • 17
    @FélixGagnon-Grenier - No one is saying that comments should be answers. But the use-case pointed out by user2357112 is precisely one of the intended uses for comments; namely, asking for clarification. Thus, the invisible characters are actively harming the intended use of comments. (And as noted in T.J. Crowder's feature request, they are also not even achieving the desired goal.)
    – John Y
    Commented May 1, 2017 at 21:41
  • 1
    Not sure why you start with "I disagree with status-bydesign". It is status-bydesign. You disagree with the implementation, not that it is designed this way.
    – Joe
    Commented May 1, 2017 at 21:59
  • 1
    @Joe, I disagree with status-bydesign, which means it is not subject to change or correction Commented May 1, 2017 at 22:16
  • 1
    The design is that it should wrap, the implementation is by adding hidden characters. You say you disagree with the idea that it should wrap? Commented May 2, 2017 at 1:43
  • 1
    @FélixGagnon-Grenier I said I disagree with the current implementation. I never said it should not wrap. To summary, status-bydesign is the same for me, as the current implementation is correct. Commented May 2, 2017 at 1:50
  • 2
    @BoundaryImposition: Turns out the invisible unicode crap happens in chat, too! Not with code blocks in chat, but the correct way to use code blocks in chat is weird and arcane enough that plenty of people are going to use backticks instead. Commented May 2, 2017 at 16:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .