this post was submitted on 05 May 2025
459 points (98.9% liked)

Programmer Humor

23030 readers
1629 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 2 years ago
MODERATORS
 
top 50 comments
sorted by: hot top controversial new old
[–] [email protected] 2 points 3 hours ago* (last edited 3 hours ago)

If(x-parseInt(x)<0){ y=0;} Else{ y=parseInt(x)!}

[–] [email protected] 20 points 18 hours ago

I've seen code in my workplace using parseInt to round JS Number. Made me cringe coming from system programing but I didn't see the danger.

It's sad the only way to prevent such a bad code in production is to use transpilers.

[–] [email protected] 31 points 18 hours ago

Good old JS, because exceptions are a sin.

[–] [email protected] 11 points 18 hours ago

looks functional to me. Its a pure function, right?

[–] [email protected] 21 points 22 hours ago (2 children)

What language is that so I can avoid it.

[–] [email protected] 8 points 18 hours ago

We all know what it is.

[–] [email protected] 42 points 21 hours ago

lol it’s js of course

[–] [email protected] 46 points 1 day ago (5 children)

I know this is for fun, but as general advice to the homies, if a language or system is doing something you didn't expect, make sure to look at the documentation

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

This will save a lot of time and headaches

[–] [email protected] 4 points 11 hours ago

oh god the reason is even stupider then I expected

Because large numbers use the e character in their string representation (e.g., 6.022e23 for 6.022 × 1023), using parseInt to truncate numbers will produce unexpected results when used on very large or very small numbers. parseInt should not be used as a substitute for Math.trunc().

[–] [email protected] 15 points 17 hours ago* (last edited 17 hours ago) (1 children)

Holy fuck that is long. When the documentation for the integer parsing function is 10 pages long, there's something seriously wrong with the language

[–] [email protected] 6 points 17 hours ago (1 children)

Is it? I've seen longer articles for C# and not as many complaints about it.

[–] [email protected] 5 points 15 hours ago

Probably not an article about integer parsing, though. If the docs are that long, then because Microsoft does have a tendency to be overly verbose for things they think you need, just to have no docs for the stuff you actually need.

For reference here's the relevant rust docs.

[–] [email protected] 14 points 18 hours ago* (last edited 18 hours ago)

https://en.wikipedia.org/wiki/Principle_of_least_astonishment

...and of course JS made it into the examples, how could it not:

A programming language's standard library usually provides a function similar to the pseudocode ParseInteger(string, radix), which creates a machine-readable integer from a string of human-readable digits. The radix conventionally defaults to 10, meaning the string is interpreted as decimal (base 10). This function usually supports other bases, like binary (base 2) and octal (base 8), but only when they are specified explicitly. In a departure from this convention, JavaScript originally defaulted to base 8 for strings beginning with "0", causing developer confusion and software bugs. This was discouraged in ECMAScript 3 and dropped in ECMAScript 5.

[–] [email protected] 4 points 18 hours ago* (last edited 18 hours ago)

Okay but this documentation is obviously wrong from the first sentence

The parseInt() function parses a string argument and returns an integer of the specified radix

Integers don't have radices. It should read:

The parseInt() function parses a string argument representing an integer of the specified radix and returns that integer.

~~Either way, I still don't understand the behaviour in the image.~~ nvm, thanks [email protected]

[–] [email protected] 9 points 23 hours ago (2 children)

I'd advise to always look into the corresponding documentation before using something from any library.

[–] [email protected] 4 points 17 hours ago

But I'm too busy being confused by the behaviors of libraries I previously didn't read the documentation for, to read the documentation for every new library I adopt.

(This is sarcasm...mostly.)

[–] [email protected] 13 points 21 hours ago

I'll go with 5 hours of debugging, thank you very much!

load more comments
view more: next ›