this post was submitted on 31 Jan 2024
122 points (87.7% liked)

Today I Learned

17733 readers
336 users here now

What did you learn today? Share it with us!

We learn something new every day. This is a community dedicated to informing each other and helping to spread knowledge.

The rules for posting and commenting, besides the rules defined here for lemmy.world, are as follows:

Rules (interactive)


Rule 1- All posts must begin with TIL. Linking to a source of info is optional, but highly recommended as it helps to spark discussion.

** Posts must be about an actual fact that you have learned, but it doesn't matter if you learned it today. See Rule 6 for all exceptions.**



Rule 2- Your post subject cannot be illegal or NSFW material.

Your post subject cannot be illegal or NSFW material. You will be warned first, banned second.



Rule 3- Do not seek mental, medical and professional help here.

Do not seek mental, medical and professional help here. Breaking this rule will not get you or your post removed, but it will put you at risk, and possibly in danger.



Rule 4- No self promotion or upvote-farming of any kind.

That's it.



Rule 5- No baiting or sealioning or promoting an agenda.

Posts and comments which, instead of being of an innocuous nature, are specifically intended (based on reports and in the opinion of our crack moderation team) to bait users into ideological wars on charged political topics will be removed and the authors warned - or banned - depending on severity.



Rule 6- Regarding non-TIL posts.

Provided it is about the community itself, you may post non-TIL posts using the [META] tag on your post title.



Rule 7- You can't harass or disturb other members.

If you vocally harass or discriminate against any individual member, you will be removed.

Likewise, if you are a member, sympathiser or a resemblant of a movement that is known to largely hate, mock, discriminate against, and/or want to take lives of a group of people, and you were provably vocal about your hate, then you will be banned on sight.

For further explanation, clarification and feedback about this rule, you may follow this link.



Rule 8- All comments should try to stay relevant to their parent content.



Rule 9- Reposts from other platforms are not allowed.

Let everyone have their own content.



Rule 10- Majority of bots aren't allowed to participate here.

Unless included in our Whitelist for Bots, your bot will not be allowed to participate in this community. To have your bot whitelisted, please contact the moderators for a short review.



Partnered Communities

You can view our partnered communities list by following this link. To partner with our community and be included, you are free to message the moderators or comment on a pinned post.

Community Moderation

For inquiry on becoming a moderator of this community, you may comment on the pinned post of the time, or simply shoot a message to the current moderators.

founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] Dirk@lemmy.ml 25 points 9 months ago (1 children)

11 lines of code shouldn't be a package.

[–] xor@infosec.pub 7 points 9 months ago (4 children)

you should see the "is_odd" package...

it's like, return (num%2)? true:false

[–] Dirk@lemmy.ml 11 points 9 months ago (2 children)

People using this deserve that their code breaks. Absolutely ridiculous.

Neither this, nor the leftpad thing, nor this is-even “package” are things I would even think about for a second before just writing it on my own. I wouldn’t even consider those features (let alone packages to depend my code on!) but basic programming.

[–] Ephera@lemmy.ml 8 points 9 months ago (1 children)

Problem is when you accidentally pull it in as a transitive dependency...

[–] Dirk@lemmy.ml 7 points 9 months ago

Yeah :( This also is why such nonsense “breaks the Internet” …

[–] xor@infosec.pub 4 points 9 months ago (1 children)

i just don't see how npm is letting this happen...
im going to write an npm module called "true" that just returns true...

[–] Dirk@lemmy.ml 6 points 9 months ago

… and that has 4 dependencies on it’s own!

[–] vrighter@discuss.tchncs.de 3 points 9 months ago (1 children)

and that's still too verbose. it should be (num % 2) != 0

[–] xor@infosec.pub 1 points 9 months ago (1 children)
[–] 2deck@lemmy.world 1 points 9 months ago

Name checks out

[–] 50gp@kbin.social 1 points 9 months ago* (last edited 9 months ago) (4 children)

at which point do you blame the language for not implementing it natively?

[–] rikudou@lemmings.world 9 points 9 months ago

I mean, does any language implement is_odd() natively? Doesn't everyone implement modulus and pretty much assumes that you remember modulus from elementary and can infer that even numbers are those where x % 2 == 0.

[–] Dirk@lemmy.ml 6 points 9 months ago (1 children)

at which point do you blame the language for not implementing it natively?

Erm … What more native than number % 2 do you want to have it?

[–] Ephera@lemmy.ml -3 points 9 months ago* (last edited 9 months ago) (1 children)

2.is_even()

(I don't know, if this is possible in JS.)

[–] Dirk@lemmy.ml 4 points 9 months ago (1 children)

Let’s call the number variable just x, you then have literal math (Euclidean division) if you ignore === instead of = for equals.

x % 2 === 0

This can’t get better or more native than “just math”. This is the whole code you need to detect if a number is even. I wouldn’t even call it “code”.

If you remove whitespaces and ignore the type you end up with x%2==0 which is 6 characters long and a fully valid if clause. No magic involved, no abstraction, no weird function calls on integers …

I see that in modern JS this type of coding is a trend, but you can’t tell me you want to replace 6 characters with an own module or a package. :)

[–] Ephera@lemmy.ml -1 points 9 months ago

No, I want that in the std lib. Yes, it would just call x % 2 == 0 underneath. But the advantage is readability. I'm in principle aware that x % 2 == 0 is true when the number is even, but I need it seldomly enough that I do still need to think about it for a second before I know for sure. I don't need to think about x.is_even(). And the readability is what I want natively, i.e. in the std lib.

It being in the std lib would also sidestep your concerns about security or the function call having unknown side effects.

[–] Aatube@kbin.social 6 points 9 months ago* (last edited 9 months ago)

Isn’t %2 already native?

(BTW this thing failed JavaScript so hard ECMA immediately included it in that year’s standard)

[–] xor@infosec.pub 1 points 9 months ago (1 children)

at 200k weekly downloads, i blame npm for allowing it...
https://www.npmjs.com/package/is-even

[–] yetAnotherUser@feddit.de 2 points 9 months ago (1 children)

https://www.npmjs.com/package/undefined

What do you think about this package? 14,000 weekly downloads...

[–] xor@infosec.pub 3 points 9 months ago

now i understand why people would call code "beautiful" and "elegant"
...
this here is a true work of art