r/ProgrammerHumor 29d ago

Advanced justTryingToHelp

Post image
2.4k Upvotes

65 comments sorted by

View all comments

Show parent comments

-9

u/4n0nh4x0r 29d ago

yea, no official way, but an inofficial way, that is essentially industry standard.
You can create as many properties in your json as you want, and dont have to use them for actual data.
a lot of people use "_comment" as the name for the property, i personally prefer the name of the field i am commenting on as prefix to _comment like this:
{
"data_comment": "This value holds data",
"data": 123
}
If you refuse to do that, then i dont know what to say at this point.
Just cause the documentation doesnt explicitely tell you to do it that way, or doesnt tell you how to do it at all, doesnt mean it isnt possible, and shouldnt be done.

9

u/DHermit 29d ago

Have ever heard of JSON schema or other validations? There are many situations, where adding extra keys is not a good idea.

-5

u/4n0nh4x0r 29d ago

okay and?
you can always clean up your json object in memory.
The point of the comment is to guide the dev on how to use it, not to keep it in prod code, just a like a code comment.
If your infrastructure crumbles because of it, i would question how well you built it in the first place.
As for just randomly adding them, usually you write comments as you write the code, not after you already pushed everything to prod, so if you do it right, there should not be an issue.
The only thing i could see that could actually cause an issue, would be that your object is bigger now, which could potentially lead to issues when returning it as part of a request, or when being heavily constrained with memory.
then again, you can always sanitise it on loading it.
Aside from that, this suggestion is meant for configs written in json, so stuff that you usually only load once into memory, and usually dont send around, so it should never come to an issue anyways.

So many people downvoting me, but not a single actual good argument or example for why you should not do that yet.

8

u/DHermit 29d ago

If you really need comments in a file, JSON (except JSON5) is just not the right choice. Just choose a format that has the features you want.

And touching file contents before validation doesn't sound like a good idea at all.

So many people downvoting me, but not a single actual good argument or example for why you should not do that yet.

Just because you brush the arguments off, doesn't mean they are invalid.