Syntax highlighting of strings

Maybe this is a fiddly little point, but I think it's the sort of detail that's worth paying attention to.

The theory is:

Syntax highlighting of strings could be improved to make reading easier

In the following code example, look at the way the string is 'highlighted':

return string.Format("<a href=\"{0}\">{0}</a>", link);

The red highlighting covers not just the literal string itself, but also the quote characters and the escape characters: the 'meta' guff that decorates the string.

So in the previous example, there's no clue given to the naked eye that some of the quote marks are "literal" quote marks, and others are delimiters to mark the beginning or end of the string.

Instead, syntax highlighting gives up, right when it could be most helpful. And it's left as a parsing exercise for the programmer to determine what is and isn't really a string.

Here's a different way the syntax highlighting could be implemented:

return string.Format("<a href=\"{0}\">{0}</a>", link);

Now colour (actually -- saturation) is used to differentiate between the literal parts of the string, and the 'meta' parts of the string (the escape characters and the quote delimiters).

It looks strange at first glance, because it's an alien concept -- but i think that if you were used to this type of highlighting, it would allow you to "see" what escaping is going on in a string, far more readily.

More importantly -- it allows you to selectively 'see past' the escaping.

If you look at the second example, you can 'train' your eyes to focus on just the literal string itself -- until you see basically:

return string.Format("<a href=\"{0}\">{0}</a>

And thus you can reason about the text you've written, or proof-read it, in greater isolation from things that only the parser needs to worry about (the mechanics of escape characters etc.)

 

My book "Choose Your First Product" is available now.

It gives you 4 easy steps to find and validate a humble product idea.

Learn more.

(By the way, I read every comment and often respond.)

Your comment, please?

Your Name
Your Url (optional)
Note: I may edit, reuse or delete your comment. Don't be mean.