Hacker News new | ask | show | jobs
by burgerbrain 5166 days ago
> "I saw people were using them to hold parsing directives"

What could possibly make somebody want to do that? Are there any examples around of people doing that?

12 comments

You can imagine something like this:

    {
       /* if IE */
       browser: "IE"
       /* else */
       browser: "standard"
       /* endif */
    }
Pretty terrible and still possible (but admittedly harder) without comments.
If you are storing this kind of implementation logic in your data, I hope I never have to work with you (not aimed at parent posting, but rather the global "you"
Unfortunately it's all too common in mobile development - mobile is the new "bad old days" of user agent sniffing hell.
You typically don't store this sort of thing in data, though.

Then again, we have certain types of logic stored in a database table, loaded through fixtures... so my two cents may be worth much less than what they appear.

Happens already. Two examples:

1. Internet Explorer has conditional comments - http://msdn.microsoft.com/en-us/library/ms537512%28v=vs.85%2...

2. Sprockets' directive processor uses directives in comments. https://github.com/sstephenson/sprockets

It's not hard to imagine if you sorta hold your breath and let yourself get a little dizzy and think hard about XML you've had the pleasure of messing with.

I quickly get visions of version numbers, customized namespace declarations, typedefs, strftime date format strings...

or say Javadoc @ directives....sigh
Ahhh doclets. I certainly don't miss those things!
The CDDB/FreeDB format requires you to parse comments... http://www.jwz.org/doc/cddb.html

And people do all kinds of nonsense with HTML comments. A very bad idea is often the fastest to implement.

I completely believe him. Even though XML is really verbose I saw quite a few folks adding these types of things in XML comments.

So this wouldn't surprise me one bit. Would be interesting to see some real-world examples though.

Yep, it made me rage a bit when I found that in a project I'm working on. Comments are for people, people!
And so instead, people embed parsing directives in the JSON itself.
Yes, but that works. Taking in such JSON and then immediately spewing it back out doesn't change the underlying meaning. Transforms from such JSON to some other format (perhaps even another JSON format) must explicitly choose what to do with "comments", instead of accidentally just discarding them. Given that parsing directives are going to exist somewhere, this is the correct place for them.

(Better yet is to create an explicit place for metadata. I almost reflexively use {"metadata": null, "payload": ...} now instead of putting my payload right at the top level, because wouldn't you know it, sooner or later some metadata always seems to wander in. And if it doesn't... shrug. If you're in a place where you can afford JSON in the first place the extra cost is probably way below the noise threshold for you.)

If you don't parse the metadata, it doesn't matter whether it's in JSON or in a comment. You lose the intended meaning either way.
But the point is that if you have comments in your JSON, the first time you do some sort of "for key in data" to transform the data and spit it back out, the comments are gone; you may never even realize they were there to start with.

If you do that with the metadata explicitly stored as a separate key-value pair in your blob, then this doesn't happen; the meta data is never silently discarded when you, say, take all the key value pairs in the JSON blob and send them out down the wire to a client. If you want to strip the meta-data, you have to do that.

I know this isn't Python, but I think the Zen of Python is on point here: "Explicit is better than implicit."

> But the point is that if you have comments in your JSON, the first time you do some sort of "for key in data" to transform the data and spit it back out, the comments are gone; you may never even realize they were there to start with.

If you've stored comments as regular data, you haven't lost them but you've just transformed them in the output.

Your criticism appears to be based on a transform written with an incomplete understanding of the source data. I'd submit the problem lies in the incomplete understanding of the source data, not the fact the source data had comments. If your transform didn't "know" comments were possible, what else did it not "know"?

TANSTAAFL.

Here's an example of using javascript comments to set options on a parser. It isn't JSON, but it is pertinent.

/* jslint nomen: true, debug: true, evil: false, vars: true */

Weren't pascal comments delimited by `{` and `}`, and weren't borland pascal compiler directives of the form `{$X}`?
Yes, this was pretty common in Pascal.

Also, Emacs uses comments to set file-local options. There's a long tradition of overloading comments to achieve metalinguistic ends. JavaDoc and Doxygen are great examples.

Even when handed a decent macro language with whizzy namespaces and a great DOM, I imagine that some people will still stoop to gross and convenient hacks.

This brings to mind Closure Compiler's use of comment annotations to add type checking, etc to javascript.

https://developers.google.com/closure/compiler/docs/js-for-c...

As others have pointed out, this is fairly common, but no-one seem to have pointed to this one:

http://en.wikipedia.org/wiki/C_preprocessor

The C preprocessor processes comments?
Well that was pretty stupid. I had gotten into my head that hashes were comments too in C.
Not JSON, but here's a truly horrible example of Internet Explorer using specially formed comments to take different actions: http://www.quirksmode.org/css/condcom.html
They really aren't that bad, definitely very handy for injecting IE specific stylesheets, without having to rely on javascript.
I'm sure they're handy, but just crashingly inelegant.
Not to mention that the only reason for needing IE specific stylesheets is because IE doesn't follow CSS standards properly!!!
Uh... this was downvoted because? Certainly not because it's wrong.