564

Meme transcription:

Panel 1: Bilbo Baggins ponders, “After all… why should I care about the difference between int and String?

Panel 2: Bilbo Baggins is revealed to be an API developer. He continues, “JSON is always String, anyways…”

you are viewing a single comment's thread
view the rest of the comments
[-] bleistift2@sopuli.xyz 17 points 4 months ago

Or even funnier: It gets parsed in octal, which does yield a valid zip code. Good luck finding that.

[-] xthexder@l.sw0.com 5 points 4 months ago

Well shit, my zip code starts with a 9.

[-] bleistift2@sopuli.xyz 5 points 4 months ago

I’m not sure if you’re getting it, so I’ll explain just in case.

In computer science a few conventions have emerged on how numbers should be interpreted, depending on how they start:

  • decimal (the usual system with digits from 0 to 9): no prefix
  • binary (digits 0 and 1): prefix 0b, so 0b1001110
  • octal (digits 0 through 7): prefix 0, so 0116
  • hexadecimal (digits 0 through 9 and then A through E): prefix 0x, so 0x8E

If your zip code starts with 9, it won’t be interpreted as octal. You’re fine.

[-] xthexder@l.sw0.com 7 points 4 months ago* (last edited 4 months ago)

Well, you're right. I wasn't getting it, but I've also never seen any piece of software that would treat a single leading zero as octal. That's just a recipe for disaster, and it should use 0o116 to be unambiguous

(I am a software engineer, but was assuming you meant it was hardcoded to parse as octal, not some weird auto-detect)

[-] bleistift2@sopuli.xyz 7 points 4 months ago

I’ve also never seen any piece of software that would treat a single leading zero as octal

I thought JavaScript did that, but it turns out it doesn’t. I thought Java did that, but it turns out it doesn’t. Python did it until version 2.7: https://docs.python.org/2.7/library/functions.html#int. C still does it: https://en.cppreference.com/w/c/string/byte/strtol

[-] xthexder@l.sw0.com 6 points 4 months ago* (last edited 4 months ago)

Interesting that strtol in C does that. I've always explicitly passed in base 10 or 16, but I didn't know it would auto-detect if you passed 0. TIL.

[-] docAvid@midwest.social 2 points 4 months ago

It's been a long time, but I'm pretty sure C treats a leading zero as octal in source code. PHP and Node definitely do. Yes, it's a bad convention. It's much worse if that's being done by a runtime function that parses user input, though. I'm pretty sure I've seen that somewhere in the past, but no idea where. Doesn't seem likely to be common.

[-] bleistift2@sopuli.xyz 2 points 4 months ago

PHP and Node definitely do.

Node doesn’t.

> parseInt('077')
77
  1. If the input string, with leading whitespace and possible +/- signs removed, begins with 0x or 0X (a zero, followed by lowercase or uppercase X), radix is assumed to be 16 and the rest of the string is parsed as a hexadecimal number.
  2. If the input string begins with any other value, the radix is 10 (decimal).

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt

[-] docAvid@midwest.social 2 points 4 months ago

You seem to have missed the important phrase "in source code", as well as the entire second part of my comment discussing that runtime functions that parse user input are different.

[-] bleistift2@sopuli.xyz 1 points 4 months ago

You seem to have missed the important phrase “in source code”

I read that, but I thought it was a useless qualifier, because everything is source code. You probably meant “in a literal”.

[-] raman_klogius@ani.social 3 points 4 months ago

Who tf decided that a 0 prefix means base 8 in the first place? If a time machine was invented somehow I'm going to cap that man, after the guy that created JavaScript.

[-] JackbyDev@programming.dev 7 points 4 months ago

Should be like 0o777 to mimic hex 0xFF

[-] kamen@lemmy.world 3 points 4 months ago

Oof.

I guess this is one of the reasons that some linters now scream if you don't provide base when parsing numbers. But then again good luck finding it if it happens internally. Still, I feel like a ZIP should be treated as a string even if it looks like a number.

[-] bitfucker@programming.dev 4 points 4 months ago

Yep. Much like we don't treat phone numbers like a number. The rule of thumb is that if you don't do any arithmetic with it, it is not a "number" but numeric.

[-] sukhmel@programming.dev 1 points 4 months ago* (last edited 4 months ago)

Well, we don't, but every electonic tables software out in the wild on the other hand…

/jYes, I know that you can force it to become text by prepending ' to the phone, choose an appropriate format for the cells, etc, etc

The point is that this often requires meddling after the phone gets displayed as something like 3e10

this post was submitted on 30 Jun 2024
564 points (100.0% liked)

Programmer Humor

19572 readers
1482 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 1 year ago
MODERATORS