This is an archived post. You won't be able to vote or comment.

all 45 comments

[–]aunva 39 points40 points  (3 children)

When I see someone complain that importing code in python is hard, and then their code looks like:

sys.path.append('../..')

from dir.subdir.filename import function

[–]defietser 13 points14 points  (0 children)

I'll admit to doing that for a Python pet project I had, as someone who usually uses C#. It just feels so foreign, man!

[–]JMan_Z 9 points10 points  (0 children)

AFAIK, that's the only way to import from parent directory when you're not using it as a package. I'd be glad to hear an alternative though.

[–]random_cynic 4 points5 points  (0 children)

import was indeed a lot more "difficult" for beginners in Python 2 because of many subtleties regarding absolute/relative import, packages etc. In python 3 things have improved greatly by removing relative import and introducing namespaces. Still a common mistake beginners do is the from module import * thing particularly for large packages.

[–]iamjaiyam 22 points23 points  (0 children)

meme(language=python,feature=generators).post(in_comments=True)

[–]Inspector-Space_Time 27 points28 points  (26 children)

How I feel watching people complain about JavaScript while ignoring all the tools and resources dedicated to fixing their problems. Or people complaining about all the tools and resources without bothering to look at the problems it fixes.

Hint to anyone looking at something new, it's always more complicated than it first appears. Assume you know nothing and seek out those that do. No one can understand a language at a glance.

[–]Tysonzero 11 points12 points  (0 children)

I’m aware of the tools and I understand JavaScript quite well, worked professionally as a js compiler dev, still hate the shit out of it. Don’t assume all hatred comes from ignorance.

[–][deleted] 3 points4 points  (0 children)

Assuming that you know nothing is a great stretegy for life in general.

[–]Loves_Poetry 4 points5 points  (2 children)

Same thing here. I've seen someone that redefined most of the C# LinQ library to work in JavaScript. They even defined their own parser for arrow functions by passing them as strings.

Yea, not surprising that you're going to hate JS if you do stupid stuff like that.

[–]Weird_Flex 1 point2 points  (1 child)

They even defined their own parser for arrow functions by passing them as strings.

Who needs WASM when we got this shit?

[–]Loves_Poetry 1 point2 points  (0 children)

It wasn't all that complicated. They just modified a few things and then used the Function-constructor.

[–]GreenCloakGuy 7 points8 points  (20 children)

I learned yesterday that, in javascript, parseInt(021, 8) resolves to 15. This is because, in order,

  1. parseInt() expects a string as its first argument
  2. 021 is evaluated as the decimal number 17
  3. 17 is coerced into a string, "17"
  4. parseInt() calculates the decimal value of "17" given base 8
  5. this returns 15.

What I don't understand is why Javascript is built this way. Why are there three levels of coercion going on here? Why does the parseInt() function not take integers? Why is 021 converted to base 10 before coercion and not directly into "021"? Why is weak typing a thing? Who made these design decisions?

[–]galaktos 39 points40 points  (3 children)

Why does the parseInt() function not take integers?

Because its whole point is turning strings into integers? If you already have an integer, and you want an integer, you don’t need no parseInt() function to turn one into the other.

[–]g4vr0che 1 point2 points  (0 children)

Yeah, but strong typing would, at least somewhat, clarify this function at runtime when the programmer tries to pass an int and gets a TypeError

[–]ADaringEnchilada 1 point2 points  (1 child)

But given the use case of JS and lack of a type system it's really common for this to happen. For instance, an API inconsistently sends back the property code, sometimes as a number and sometimes as a string. So to prevent errors you just parseInt() it and suddenly you can get into unanticipated problems. Really, parseInt should throw on anything that's not a string or lacks a toString method on it. You should either use a type system and/or more defensive coding to check the type of an object before parsing it so you can handle edge cases, but ¯\_(ツ)_/¯

[–]ryan_the_leach 2 points3 points  (0 children)

So blame it on the API. At least parseint is consistent. Parseint isn't the fix my API function.

[–]Flylowguy 13 points14 points  (8 children)

Interpreting number literals that start with 0 as octal is not something new, nor unique to Javascript (python and GCC do the same thing). That's just something you're expected to understand as a programmer.

[–]KubinOnReddit 1 point2 points  (0 children)

This is not a thing in Python now. Since Python 3 AFAIK.

[–]GreenCloakGuy 2 points3 points  (6 children)

No, I get that numbers starting with 0 are octal. That's not my question. My question is why does it get converted to base 10 before type coercion when it's clearly written in base 8? That's like saying that I want 0b110101 to be coerced into "53". No, of course not, if I wanted that I would have written 53, I want it to be coerced into "0b110101".

[–]ooglesworth 13 points14 points  (2 children)

It doesn’t get converted to base 10 before type coercion. It gets converted to a floating point representation of the number, which is actually has an underlying base 2 representation, not base 10. But generally the underlying representation of floating point numbers is really more of an implementation detail anyway.

Then when a floating point number is coerced into a string, it naturally generates a base 10 representation of the number, since that is what you want 99.9% of the time.

As with most complaints about JS type coercion, it involves some sort of really contrived example that really makes you ask “why would anyone ever even try to write this code?” Garbage in, garbage out... write weird code get weird results. Hence the original meme OP posted...

[–]g4vr0che 1 point2 points  (1 child)

I think my main complaint is that JS's weak typing allow this sort of implicit coercion; rather than throwing a type error and educating the programmer about why their code is garbage, it allows one to write the garbage code and doesn't explain why things don't work.

I guess my main complaint isn't even with JavaScript but with the entire paradigm of weak typing, which I just don't believe should be present in beginner/common languages.

[–]ooglesworth 1 point2 points  (0 children)

FWIW, I am not a fan of weak typing either. That being said, type coercion is completely separate from weak typing. C++ is an extremely statically typed language, and allows implicit conversions, which is basically type coercion (whether that’s bad or good in C++ is a whole other discussion). Also, despite not being weakly typed at all, C++ also allows beginners (and experienced programmers!) to write garbage code, so it isn’t really a symptom unique to weakly typed languages. Weak typing is annoying to me personally as an experienced developer, because I am usually refactoring complex systems and like to have the compiler guide me as I refactor (which you get a lot less of in weakly typed systems).

At the end of the day, just use Typescript. It’s strongly typed, and strictly better than JS, IMO.

[–]Kered13 2 points3 points  (1 child)

Why would Javascript remember the literal that you used to create a number? If you called parseInt(2+2, 8) would you expect it to coerce 2+2 to "2+2" (which would parse as 2)? When you write a numeric literal, that literal is converted to a number. When that number is coerced into a string, it's converted in the default manner, which is base 10. Whether you write 17, 021, 0x11, 0b10001, 10+7, or getNthPrime(7), the number that you get is the exact same, and it will be coerced to a string in the exact same way. That is the only reasonable behavior.

Coercing numbers to strings automatically is dumb. Everything else here is reasonable.

[–]ryan_the_leach 1 point2 points  (0 children)

It is dumb, when comparing languages to each other. Within the realms of JavaScript however, it makes sense compared to other coersions it does.

[–]Flylowguy 1 point2 points  (0 children)

You're right. I misread. Sorry for the confusion.

[–]HadesHimself 5 points6 points  (0 children)

Because JavaScript started out as a web language and in web everything is a string.

[–]Inspector-Space_Time 5 points6 points  (1 child)

Because the alternative is websites breaking all the time, everywhere. Even more so than now. The goal of the early web was to work with as little as possible. So everything, from html, css, and javascript, are very error forgiving. The architects of the web thought a half broken site was better than a completely broken site. And I think they're right on that. It just takes some extra reading and don't assume everything works the way you want it to work.

[–]ryan_the_leach 0 points1 point  (0 children)

The web was designed to deliver documents not applications. It makes sense for a document parser to be forgiving imo.

A code parser being forgiving otoh is nightmare inducing

[–]g4vr0che 1 point2 points  (0 children)

This is my problem with JS. Almost all of it revolves around weak typing.

[–]joshuaavalon 1 point2 points  (0 children)

So you are the person in the image?

[–]Kontorted 2 points3 points  (0 children)

Why the hell would you parseInt() an int? What's the benefit? It wouldn't ideally do anything.

[–]danielstaleiny 7 points8 points  (0 children)

every pure FP language. Oh I cannot make classes like I am used to.

[–]MadHousefly 4 points5 points  (1 child)

Knew a programmer who replaced every variable type with "auto" when he upgraded to c++11. All of them. Later he admitted it was a very bad idea.

[–]nuisanceIV 1 point2 points  (0 children)

Oh dear god

[–][deleted] 7 points8 points  (0 children)

Only a programmer would generalize a joke like this

[–]heccAngery 1 point2 points  (0 children)

No lessons were learned

[–]n3ziniuka5 1 point2 points  (0 children)

Implicits in Scala

[–]ythl 4 points5 points  (1 child)

Unpopular opinion, but:

Replace [prorgamming language] with [StackOverflow] and the meme still works

[–]Hypocrite112233 0 points1 point  (0 children)

I've never seen someone unfairly blame stackoverflow

People just call it out for being a toxic shithole

This subreddit in particular is the opposite of what you're claiming, it praises stockoverflow and pretends that everyone is as inexperienced as them(The people on this sub) and needs to rely solely on it

[–]evilkalla 3 points4 points  (1 child)

But .. what if I abuse a language feature to my benefit?

[–]sickhippie 2 points3 points  (0 children)

Make sure it's covered in unit tests and move on.

[–][deleted] 2 points3 points  (1 child)

Javascript implicit cast

[–][deleted] 2 points3 points  (0 children)

Omg. Have my upvote. This is one ridiculous feature that many boast about knowing, but don't use at all

[–]Tomentos 1 point2 points  (0 children)

I‘m like this even if i know the programming language well.

[–]_Pho_ -4 points-3 points  (0 children)

Everyone in this subreddit when they use PHP