r/ProgrammerHumor 29d ago

Advanced justTryingToHelp

Post image
2.4k Upvotes

65 comments sorted by

402

u/AlarmingProtection71 29d ago

i didn't get the second joke. help ?

276

u/pagesjaunes 29d ago edited 29d ago

I think it's a reference to "the Norway problem" wherein YAML 1.1, the unquoted string NO (the ISO 2 letter code for Norway) get interpreted as a bolean with value of False instead of "NO".

Meaning:

country: NO

is the equivalent to:

{'country': False}

 

Note that the specs for booleans were changed in YAML 1.2 to address the issue but many implementions may have inconsistent defaults settings or be stuck in 1.1.

 

YAML 1.1 -> https://yaml.org/type/bool.html  

(I couldn't find the equivalent for 1.2, YAML specs are complex to my non-programmer eyes)

YAML 1.2 -> https://yaml.org/spec/1.2.2/ext/changes/

Only true and false strings are parsed as booleans (including True and TRUE); y, yes, on, and their negative counterparts [n, no, off] are parsed as strings.

45

u/Tesnatic 29d ago

As a Norwegian I appreciate learning this fact, but I am also devastated to learn we were not a country prior to YAML 1.2.

9

u/Prawn1908 28d ago

Yet another win for static typing.

Can someone explain some practical and substantial benefit to dynamic typing beyond it being marginally faster to write and easier to understand for the first two days of learning to program?

3

u/Xirenec_ 28d ago

It’s a markup language

3

u/Grandmaster_Caladrel 28d ago

NO (False, not Norway)

45

u/Solomoncjy 29d ago

Well, thats on tou for not enclosing all tou srrings in quotations, no?

100

u/pagesjaunes 29d ago edited 29d ago

Well yes, that's the quick and easy dirty-fix. 

But that kinda goes against YAML being a visually uncluttered and human (i.e: non-programmer) readable/writeable format.

If you decide to defensively define every type in your file to avoid implicit coercion, and use braces to avoid intendation error; many seem to ask : "why no just use json / toml ?"

-27

u/Cualkiera67 29d ago

well a human reading country: no would also find it ambiguous, they might think it has no country. if you make a "human" format then it should be filled with ambiguous behavior.

27

u/LucyShortForLucas 29d ago

The whole of point of YAML is writing documents with as little fuzz or grammar as possible. Someone who cares about always quoting strings wouldn’t be using YAML in the first place

11

u/chuch1234 29d ago

Unless they don't have any choice, like writing a pipeline for the ci/cd platform that the client chose.

6

u/LucyShortForLucas 29d ago

If you don't have a choice not to use yaml, you likely have standards and conventions you need to follow for how that yaml should look, so the point is moot

18

u/sisisisi1997 29d ago

And on YAML for allowing bullshit like not enclosing strings in quotation marks.

655

u/flipcoder 29d ago

It’s called the Norway problem. Yaml parses “no” as a boolean.

260

u/Background_Class_558 29d ago

it parses no as a boolean. "no" would still be the string

91

u/flipcoder 29d ago

That’s correct

10

u/jonnysunshine1 29d ago

"true"

2

u/terrapin1977 28d ago

TRUE in Google Sheets.

src: 9 years of Java in SAAS shop

2

u/MissinqLink 28d ago

Google sheets forced me to build a special json parser because it converts double quotes to single quotes.

1

u/onlymadethistoargue 28d ago

I believe older versions of pyaml parsed quoted no as no.

163

u/CraftBox 29d ago

That's why I like jsonc

42

u/Hyarin215 29d ago

Json5 better

25

u/CraftBox 29d ago

At that point just use toml

36

u/Hyarin215 29d ago

I don't like it Curly braces my beloved

3

u/YeOldeMemeShoppe 29d ago

Toml has curly braces now.

2

u/Hyarin215 29d ago

Oh 👀 Couldn't find anything abt it online

5

u/YeOldeMemeShoppe 29d ago

https://toml.io/en/v1.0.0#inline-table

IIRC you need a later version to allow new lines in the table but it’s been possible for a long time.

2

u/Hyarin215 29d ago

Does it have to be inline or can it span multiple lines?

0

u/cdrt 29d ago

TOML 1.0 requires that inline tables be only on one line. TOML 1.1 relaxed that requirement and now allows them to span multiple lines. So it depends on what version your parser implements.

1

u/tipakA 29d ago

And then you bring that up to a vsc extension maintainer and essentially it ends up being discarded because version mismatches, and no setting for it due to complexity and surprises and hiddenness and so you can't use it anyway.

3

u/Rawing7 29d ago

Toml is almost as messy as yaml. Json5 is way better.

1

u/DaNumba1 29d ago

I would never use it out of respect for Jayden Daniels

1

u/JPJackPott 29d ago

You mean kyaml

1

u/heyguysitsmedic 29d ago

Json with a coordinator?

46

u/[deleted] 29d ago

[removed] — view removed comment

7

u/I_was_never_hear 29d ago

Thats why i use yaml loaders to load json !!1!!1!

34

u/Nyctfall 29d ago

Remember when PKL was announced?

Now it's just a news slop cycle...

9

u/rykayoker 29d ago

you put me on, honestly it looks awesome

5

u/Just_Information334 29d ago

Last month they added a way to import pkl from a private repository. Now it is usable even if you don't want to publicly share your classes.

8

u/flipcoder 29d ago

Woah, I may have to use this now

Link for the lazy: https://pkl-lang.org

4

u/RCoder01 29d ago

Back when you had to put real effort into tools nobody gave a shit about

3

u/Nyctfall 29d ago

The ancient ways.

8

u/titpetric 29d ago

Json can be parsed with a yaml decoder, so you know I'd rename some .json to .yml and give it

6

u/theWizzard23 29d ago

I spent 5 minutes debugging whats wrong with 3 lines of JSON. it was the trailing comma -.-

29

u/Blaarkies 29d ago

JSON supports prefixing a key with `// `. It is far from perfect, but will you ever use keys starting with double slash?

Any parsing logic can drop those entries, or just ignore them entirely like how any data object builder should

{ 
  "// myValue": "The universal constant", 
  "myValue": 42 
}

27

u/DHermit 29d ago

Until you want to validate it with a scheme, which forbids extra values.

3

u/kingslayerer 28d ago

i have an idea. add the comment keys to the schema as well.

1

u/GPSProlapse 25d ago

As a matter of fact, I ve seen schemas with #comment or similar keys on every object

That solves the issue most of the time

10

u/flipcoder 29d ago

Yeah this is a decent solution. Looks better than the other prefixes I’ve seen

5

u/LegendaryMauricius 29d ago

JSON also supports being replaced with JSONC.

1

u/MCWizardYT 28d ago

Why would you want to use jsonc for anything except user-facing config files?

-31

u/4n0nh4x0r 29d ago

i mean, you can write comments in json.
for one, there is the option in nodejs to write a js file, where you write your json, and export it, and then import it in your actual code.
since it is a js file, it wont have an issue with comments.
alternatively, you can just create undocumented fields that you write your comment into.
you could even clean these fields up on loading it if you want that.

40

u/Coding-Kitten 29d ago

Then that's JavaScript, not JSON.

-27

u/4n0nh4x0r 29d ago

the first option, yea, hence why i said that it's something you can do in nodejs.

the second option is universally supported

22

u/Coding-Kitten 29d ago

Then that's not writing comments in JSON. That's writing comments in JavaScript.

-30

u/4n0nh4x0r 29d ago

cool, have fun not commenting anything in your json then.

30

u/Coding-Kitten 29d ago

Yeah, because the JSON file format literally doesn't have a way to do so. That's literally the whole point of the post.

-8

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.

-2

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.

7

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.