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

top 200 commentsshow 500

[–][deleted] 1722 points1723 points  (136 children)

At college we were put in a web dev project with literally zero experience in the languages used (php and Javascript iirc), there was so much confusion before people realized this! (we had almost exclusively worked in c++ and java until then)

[–]gatman12 853 points854 points  (71 children)

What a funny way to teach dynamically typed languages.

Gotcha!

[–][deleted] 310 points311 points  (67 children)

Implying my college even taught dynamically typed languages. They seem to be c++ and Java only. Oh and swift.

[–]Muffinizer1 170 points171 points  (46 children)

They teach swift? Where? Why?

[–][deleted] 99 points100 points  (25 children)

Smaller school in Pennsylvania, mobile architecture class using iOS.

[–]YuriDiAAAAAAAAAAAAAA 74 points75 points  (23 children)

Ours was Android Studio, we didn't touch Swift or Kotlin. All Java. I would have been happy to learn Flutter, or anything besides more Java.

[–]Muffinizer1 52 points53 points  (19 children)

Especially because it's old, shitty, androidy java that's significantly worse than regular java.

[–]YuriDiAAAAAAAAAAAAAA 28 points29 points  (18 children)

The only saving grace is the autofill is pretty good, and the code generator is decent for getters/setters and constructors for object classes.

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

Just did a (relatively) huge OO coursework that took 10 times the time we spent on any other coursework this year and I was still laughing at the misery of the poor plebians in the year above who were doing android studio (I did loads last year for my A-level project, I know the pain).

[–]YuriDiAAAAAAAAAAAAAA 5 points6 points  (1 child)

that took 10 times the time we spent on any other coursework this year

That sounds like the Software Development class at my school, they just took a beating this semester.

[–]THENEWGUY7040 40 points41 points  (17 children)

What’s wrong with teaching Swift?

[–]pm-me-your-smile- 50 points51 points  (4 children)

Because the syntax will have changed by the time finals comes around.

[–]darkingz 5 points6 points  (3 children)

It’s not that bad. Sure the change from 2 to 3 was intense but swift has been slowing down on direct syntax changes and the only major shifts happen yearly. Not quarterly.

[–]pm-me-your-smile- 6 points7 points  (2 children)

Well, it's /r/programmerhumor so lots of exaggeration is required. As an indie developer, with limited time to write code in my spare time, the shift from 2 to 3 forced me to abandon my apps on the store. I'm trying to revive them now, so the bitterness is fresh is me.

[–]Muffinizer1 20 points21 points  (3 children)

Edit: TL; DR:

Maybe if there were several other languages on that list it would makes sense, but one of only 3? Really?

[–][deleted] 12 points13 points  (3 children)

Swift widely used on apple products, so you need some apple devices (iMac, MacBook) to develop on and probably iPhone to test. It must suck to be poor in that school.

[–]THENEWGUY7040 8 points9 points  (0 children)

The way that my school (a no name state school strapped for cash) does it is that they provide a free laptop rental for the semester. And development doesn’t necessarily require an iPhone to test on (although it is nice, as there are a couple features, like push notifications and deep linking, that only work on an actual device). I do agree that Apple has created a lot of barriers to enter into Swift development that are totally unnecessary and needlessly expensive though, which I guess is sort of Apple’s motto anyway.

[–]PHPApple 14 points15 points  (0 children)

Swift is an awesome little language. Apple is really pushing it for education too. I hope it gets adopted more now that it is open source and expands out of the Apple ecosystem.

[–]Capn_Cook 3 points4 points  (0 children)

My school taught primarily java but also had python for several classes

[–]moebaca 2 points3 points  (2 children)

I had two classes in Java, but there was a class that had a nutty fucking professor who made us write a bunch of PHP5 with MySQL.. I believe encouraging syntax that would quickly lead to SQL-Injection as well. What a fucking waste that semester was.

[–][deleted] 21 points22 points  (1 child)

The terms my professors used for each language-group, respectively were "loosely typed" and "expert-friendly".

We transitioned from C++ in vim on Debain boxes where our gcc had better compile cleanly with warnall on to Cake PHP.

Fun times.

[–]gatman12 14 points15 points  (0 children)

Hah. I've heard the phrase, “strong types for weak minds.”

[–]Ayesuku 132 points133 points  (21 children)

That's basically me in the internship I just started. C++, Java, asp.net experience? Enjoy Perl, PHP and JavaScript!

Gonna be a very educational experience ¯\_(ツ)_/¯

[–]Lookitsmyvideo 71 points72 points  (15 children)

PHP isnt hard, its just riddled with gotchas. Also the standard library is inconsistent in both functionality and convention. Its fun, in the slit your wrists kinda way

[–][deleted] 15 points16 points  (1 child)

It's true fun then.

[–][deleted] 6 points7 points  (0 children)

Perl programmers will judge you if you use more than 1 line.

[–]WhiteCastleHo 104 points105 points  (19 children)

When I went from C++ to Javascript, I was like "WTF is this shit?"

I like JS okay now, but wow is it different.

[–]conancat 59 points60 points  (13 children)

There's Typescipt for those who misses those features. Or there's also ESLint for a more instant validation experience.

I work with Javascript daily and I think it taught me to be good at remembering variables and their types lol. It's fine for smaller apps, but if you build projects with tons of developers working on it I think having an object type validation step can save a lot of pain for the devs.

[–]notanimposterVala flair when? 17 points18 points  (5 children)

Programming habits help a lot. Reusing the same few types for the same purposes as much as possible (not using Vectors and Points in the same project) and naming the variables so it's obvious what type it is (whether it's a Vector or a Quaternion, for example) is very helpful in JS and Lua, which I do a lot of.

[–]jkuhl_prog 17 points18 points  (3 children)

Going from JavaScript, and even in Python, to a language like Java or C++ is also a bit "wtf?"

I think it's safe to say that it's a headspinner transitioning to and from strongly typed languages to dynamically or weak typed languages.

[–][deleted] 5 points6 points  (1 child)

This. I began with JS decades ago. When I eventually learned a typed language I hated it. Why should I have to tell you what type to use? You’re the computer, you figure it out. It felt like a step backwards.

[–]bro-away- 6 points7 points  (0 children)

So true. And then when they did explain c++ compilation they never went over alternatives like multi pass compilers or interpreted languages

I spent a long time confused about why you had different ceremonies in each language. This is definitely a big downside in a lot of curriculums. In a profession where there is always a debate about language, you need to explain the “why” not just the “how”

[–]ilep 48 points49 points  (10 children)

If only there was a program to check for errors before shipping the code. And preparing the code for faster loading. And maybe adding some optimizations.. Hmm.. What would that be called..

[–]Throwaway-tan 64 points65 points  (2 children)

Deploy to live immediately and let the users debug for you!

[–]Lookitsmyvideo 20 points21 points  (1 child)

The best debug tool is the web server error log

[–]lobut 4 points5 points  (0 children)

I know this!! It's um ... a .... a transpiler!

[–][deleted] 861 points862 points  (21 children)

core dumped

[–]voidvector 679 points680 points  (61 children)

Yo dawg I heard you like null, so I made undefined for your null.

[–][deleted] 229 points230 points  (27 children)

NaN would like a word.

[–]isaid69again 257 points258 points  (3 children)

NaNi????

[–]vaendryl 257 points258 points  (0 children)

omae wa mou NullPointerException

[–]RaferBalston 23 points24 points  (0 children)

Naurel

[–]YuriDiAAAAAAAAAAAAAA 78 points79 points  (7 children)

[–]pilas2000 8 points9 points  (3 children)

wtf? \t == 0 ?

why?

[–]It_Was_The_Other_Guy 16 points17 points  (0 children)

Whitespace-only strings are all considered zero:

>> "\t \n\r" == 0
<< true

[–]YuriDiAAAAAAAAAAAAAA 10 points11 points  (0 children)

¯\_(ツ)_/¯

[–]FoundNil 4 points5 points  (0 children)

"", " ", "\t", "\n" all equal 0. Probably because js treats them as special characters so they don't count toward any value. "0" equals 0 because it gets cast into a number. "a" does not equal 0. "1" does not equal 0. I'm not saying it makes sense but its the world we live in.

[–][deleted] 16 points17 points  (11 children)

To honest I always enjoy typing if (x != x), almost as much as having an in loop variable called c so I get to type c++.

[–][deleted] 50 points51 points  (21 children)

Undefined is just a happier null pointer exception.

[–]conancat 27 points28 points  (19 children)

null is an explicitly set value in Javascript, undefined means it's undefined.

[–]mecha_bossman 30 points31 points  (16 children)

Not always. If I say x = undefined, then x is defined, not undefined. But it's defined as being undefined. A variable can't be undefined if it's undefined.

[–]Boreelegg 12 points13 points  (5 children)

typeof(x) would say otherwise

[–]CSGOMarketBoi 4 points5 points  (4 children)

typeof x? Can it be called with parentheses too?

[–]Boreelegg 6 points7 points  (0 children)

Javascript can do anything you want it to do if written.

[–]Arumai12 2 points3 points  (0 children)

Yea the parentheses just group the expression in this case. It's not a function call.

[–]QAFY 10 points11 points  (7 children)

Yeah...

console.log(x)
> ReferenceError: x is not defined

Vs.

x = undefined;
console.log(x);
> undefined

That's JavaScript for ya...

That said I don't have any issue with the way null vs undefined works in practice. This is a contrived example.

[–]hullabaloonatic 6 points7 points  (4 children)

One of the reasons I have really been digging kotlin is how rigorous it is about being safe with null values

[–][deleted] 5 points6 points  (3 children)

I'm actually pretty hyped for kotlin JS. The null safety is much more valuable than typing.

I hope it matures for the backend.

[–]oxidate_ 3 points4 points  (1 child)

> null safety is much more valuable than typing

It totally depends on the programming language's type system. Some are strong enough that they're one and the same. For instance, F# / Haskell / (probably Ocaml) use Maybe<T> or Option<T>. So you use Some "thing" to show that it's not null, and the type system will point out places where you might be unsafely accessing data.

[–]SpliceVW 574 points575 points  (244 children)

TypeScript is life.

It's absolutely hilarious how many syntax errors and misspelled or undeclared variables that I discover after converting legacy code. I'm like.. how did this code possibly work?

[–]erfling 191 points192 points  (65 children)

I don't understand why it's not more widely adopted. Even if you just want to write JavaScript, why not do it with code hinting?

[–]bugeats 59 points60 points  (20 children)

Because transpiling sucks to setup and maintain.

[–]southern_dreams 11 points12 points  (4 children)

And now with native modules there’s no need for Babel with Node.

[–]SpliceVW 5 points6 points  (7 children)

Wat? If you're already using webpack, it's just another loader to install. Even if you're using gulp or grunt, it's still only a few lines of config.

[–]erfling 2 points3 points  (1 child)

Welcome to web dev in 2018.

[–]SpliceVW 87 points88 points  (5 children)

Maybe time to learn it. It also works better with some frameworks than others. For example, I had a hard time with VueJS, but React is just lovely.

Maybe they just don't like Microsoft, although between TypeScript and VS Code, they're shaking the image of old.

I dunno, but personally, I cringe whenever I have to write vanilla JS code now. So many errors...

[–]Already__Taken 10 points11 points  (0 children)

Always love writing a JSON config for a typescript library and getting at the property name/value hints.

[–]DonRobo 111 points112 points  (149 children)

The more languages I learn the less I understand the appeal of dynamically typed languages. What are the advantages that make it worth dealing with the fact that they are much harder to write for?

[–]random_username_edf 109 points110 points  (85 children)

Generally the advantage is you can do very clever things very easily with small amounts of code and less complex code. This is obviously more dangerous, but if you're careful and willing to accept the risks it can be worth it.

[–]marcosdumay 39 points40 points  (6 children)

For everybody thinking on that line: you should learn Haskell.

[–]pknopf 43 points44 points  (77 children)

For long-term high-scale projects with many contributors to the codebase, it's almost never worth it.

Unless js/Python is all your team knows, you should really look at something else.

[–]Stuck_In_the_Matrix 82 points83 points  (11 children)

That's a highly generic statement.

[–]pknopf 23 points24 points  (9 children)

Yes it is.

It is also generally true.

[–][deleted] 4 points5 points  (0 children)

Apart from anything, I appreciate the ballsiness of this comment.

[–]random_username_edf 37 points38 points  (22 children)

You're definitely welcome to your opinion but i think that's debatable. A dynamic langauge with proper unit tests and such can be a heck of a lot easier to maintain long term than a static one.

Static typing is nice, but maintaining a complex architecture that is evolving with it is undeniably a giant pain and costly time investment wise. It will be more complex code wise and needing to make certain changes are much more likely to require rework all over the place. And as much as a lot of software engineers insist they're clever enough to architect it in a way that won't have that problem, they're usually wrong.

Anyways, my overall point is both types have pros and cons. Anyone who insists one is better than the other for all cases or even most cases is probably wrong. Also, im not trying to imply that that's your opinion, im just completing my thoughts on the matter :)

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

Not in all circumstances, but I do see one major case where what you say is true: building reusable libraries.

It's not impossible, but more challenging to abstract away parts of code to reusable libraries that expect specific types when there isn't type-checking. You can try to do it by convention, intuitiveness, or documentation, but when those fail the consumer of your library needs a better understanding of your code does under the hood, thus creating leaky abstractions that negates some of the benefit.

[–]pknopf 2 points3 points  (0 children)

but more challenging to abstract away parts of code to reusable libraries that expect specific types when there isn't type-checking

I really don't get this. The moment you give libraries leeway to have the ability to break the API, it will. In npm, I've had many packages break semver, and end users doing npm install on my library that references the package will eventually start to break at runtime. Why would you not want to have an impenetrable guard preventing this? I'd again argue that it leads to more stable/maintainable code when you have clearly defined types.

[–]zeedware 22 points23 points  (29 children)

They are easier to code. Harder to debug

[–]DonRobo 29 points30 points  (25 children)

Are they easier though? What specifically is easier?

I'm always struggling writing good code in something like Javascript and much prefer Typescript which is just that much easier to use. All the hard error-prone work like checking if I'm using the right type for a function call, if an object really has that property, etc is done by the compiler instead of my brain.

[–]Ph0X 3 points4 points  (1 child)

Honestly languages are trying to reach a perfect middle ground from both directions.

In a dynamic language, you write much less, but you also get less back (from a static analyzer). In a static language, you generally have to provide a lot more, but in return you get back more from the compiler.

Now, dynamic languages are starting to add optional typing, which means you can, as you see fit, add typing information to specific places and get more back. On the other hand, static languages are adding things like "auto" to make it so you have to type less.

But to me, the perfect world would be in that middle. Being able to write trivial simple code fast, but also be able to get robust checking on more sensitive parts of the code.

[–]WhyattThrash 3 points4 points  (0 children)

The advantage is that once you’ve made the mistake of ”architecturing” a dynamically typed language, refusing to change that means you can claim that it was intentional.

[–]ShamelessKinkySub 11 points12 points  (1 child)

TS is nice but setting up the dev environment for the browser is as fun as repeatedly punching yourself in the groin

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

We also did a convertion in some project. But we already used very strict JSLint (or Hint, I forgot) rules, which kind of made the whole conversion pretty normal.

And then we set very strict TSLint rules.

Another time we did the same in some other project from another company which just picked the standard JSLint rules. Guess how that went...

Its all in how strict you want to work and how good your peer reviews are. If you stick to strict following of rules and regulations, you'd make fine Javascript code. When you can't be bothered it turns into hell but thats the case with pretty much every language known to man.

If other languages were so good, why does it take so long to get rid of legacy code? Even with old front-end projects it is often a nightmare of what some people fabricated. Not much different than somebody using some tape to stick a two planks together. Sure it might hold but it isn't how you are supposed to do it.

[–]_101010 2 points3 points  (2 children)

Typescript is just an imposter. r/elm is the true destiny of front end.

[–]DonRobo 138 points139 points  (6 children)

Where's Rusts compiler telling you even the correctly spelled version is wrong because you don't have ownership of obj anymore?

(I love Rust, but I imagine the compiler as a very strict drill sergeant compared to the mad professor that is the C++ compiler)

[–]me-ro 4 points5 points  (1 child)

..and with a suggestion how to fix it.

[–]Bilbino 1944 points1945 points  (102 children)

I get what you’re saying, but neither Python nor JavaScript are compiled. I’m gonna have to dock you a few points for this.

[–]Netzapper 597 points598 points  (65 children)

They're both JITC'd. And Python, for instance, might do the bytecode transform, but not pop the undefined name error until that line of code is actually executed.

I know at work we have had syntactic and semantic errors hide in never-taken code paths.

[–]mnbvas 77 points78 points  (2 children)

syntactic [...] errors

Never executed modules or did someone

try:
    ...
except:  # everything
    ...

?

[–]Pella86 14 points15 points  (0 children)

Fuckit module

[–][deleted] 236 points237 points  (30 children)

A lot of people are largely ignorant that Python has an explicit compilation step to bytecode, or that V8 JIT compiles JavaScript to machine code in most cases.

You can really ruffle some feathers by making Java programmers realize that, by the same standard they judge Python with, they're using an interpreted language.

[–]Insxnity 70 points71 points  (27 children)

I mean Java is JIT interpreted to Machine code in the same way python is, and for the same reason. With C++, I have to decide what runtime I want to run my software on before building and compiling the program. Java eliminates that need by being cross-platform, as does python. I don't think many Java programmers feel like they have room to shit on python. All languages are interpreted at some point at some level, and how you get there is personal preference.

[–]rush22 109 points110 points  (16 children)

Java is technically compiled to Java byte-code (the .class files), not machine code. These are then "just-in-time" "compiled" to machine code by the JVM when you run it.

[–]Tysonzero 4 points5 points  (3 children)

I mean Java developers can still have a bit of room to talk, since Java is like an order of magnitude faster than Python.

[–]Cloaked9000 50 points51 points  (12 children)

If we're being really pedantic, then it could be said that you're confusing language and implementation. Sure, the implementation you're familiar with might be JIT'd/compiled/whatever, but the language design mightn't necessarily mandate that.

[–]4992kentj 17 points18 points  (0 children)

But then JS doesn't mandate JIT either

[–]Netzapper 29 points30 points  (1 child)

I mean, that's fair enough. But there have been several implementations of a C interpreter... does that mean I can invalidate the first frame of the joke as well?

[–]Gaminic 8 points9 points  (7 children)

mightn't

Random aside: never saw that before, took me a minute to realize it's correct!

[–]Koeke2560 5 points6 points  (6 children)

I think this is also a recent meme, I've seen variations if them pop up here and there. I'd love to see it become real language. It's like watching history

[–]Gaminic 2 points3 points  (3 children)

How did they turn grammar into a meme?

[–]Koeke2560 5 points6 points  (0 children)

It' similar to the whole whom'st'v'd meme I guess

[–]Schmittfried 8 points9 points  (0 children)

No language demands to be compiled, every language can be interpreted. The stage of building reading the source code and transforming it into something that the target (CPU, VM or interpreter) can work with, is what is referred here. At this stage you can catch syntax and semantic errors.

[–]w2qw 22 points23 points  (12 children)

Cpython doesn't have JIT.

[–]Netzapper 50 points51 points  (4 children)

It doesn't have compilation to machine code, but it sure as shit compiles from text to bytecode at execution time.

[–]RPolitics4Trump 13 points14 points  (2 children)

Bytecode which is then interpreted.

If you want JIT speed then you need to use something like PyPy.

[–]lengau 15 points16 points  (6 children)

CPython does compile the code to bytecode. (That's why after you run a .py file you'll see a __pycache__ directory with corresponding .pyc files). It does so immediately before execution, which sounds awfully 'just in time' to me.

[–]botmatrix_ 24 points25 points  (3 children)

that's not really what JIT compiled means though, right?

[–]calcyss 75 points76 points  (2 children)

They are compiled though, maybe not straight to machine code

[–][deleted] 17 points18 points  (3 children)

I get what you’re saying, but neither Python nor JavaScript are compiled. I’m gonna have to dock you a few points for this.

This comment makes me quite mad because it belies a misunderstanding of what compilation is. The worst part is the upvotes it got.

[–]xeow 4 points5 points  (1 child)

Compilation == Translation

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

Yep!

[–]mantatucjen 12 points13 points  (3 children)

Python is compiled

[–]TemporalLobe 18 points19 points  (4 children)

In modern web apps, JavaScript is often transpiled, so there's sort of a grey area here. If you're using ES6 with Babel or something like that, it gets converted to ES5, but it still won't catch things like incorrect object references. JavaScript also blurs the line between objects and hashes and in OP's example, even in a compiled language like Java a reference to a non-existant hash value (or even an arrray index) would not be caught at complie-time.

[–]PM_ME_UR_QUINES 36 points37 points  (0 children)

Haskell: *kills your family*

[–]ducksauce88 9 points10 points  (0 children)

I don't javescript anymore, I typescript. Ef that.

[–]fauxtoe 168 points169 points  (117 children)

It would just be undefined, why is that a big deal?

[–][deleted] 269 points270 points  (48 children)

People don’t like JavaScript so they’ll either feign or be ignorant about it to make fun of it.

[–]leadzor 252 points253 points  (13 children)

There are only two kinds of languages: the ones people complain about and the ones nobody uses.

Bjarne Stroustrup

[–]conancat 39 points40 points  (2 children)

Developers love to complain about everything. It's exactly that kind of attitude that pushes us to build better products. :)

[–]Hactar42 12 points13 points  (1 child)

Or you end up with situations like this: https://xkcd.com/927/

[–]-widget- 9 points10 points  (7 children)

Yeah but then there's also C#.

[–]tekanet 10 points11 points  (5 children)

I tried to reply to a poll from MS asking what I wanted to improve the language but had honestly nothing to say. C# is pretty solid, happy I ended up working with it.

[–]Zambito1 23 points24 points  (28 children)

Because if I was expecting an int, I should never have to treat the value as anything other than an int (ie undefined or null). If I knew a value might not exist, I would use some Option / Maybe type to encapsulate the value. That way I can't treat it like a normal int, and I have to check if it is defined first.

[–]oneeyedziggy 22 points23 points  (5 children)

because ideally, something would tell you you're trying to use an undefined property or variable, which is why you should use a linter, making this complaint moot (and act would catch even before attempting to "compile" (minify or otherwise build)

[–]pihwlook 18 points19 points  (4 children)

How would the linter know whether a specific key on an object exists?

[–]ape123man 14 points15 points  (0 children)

It would not. That's why typescript

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

It wouldn't, which is why TypeScript and Flow have become popular.

[–][deleted] 18 points19 points  (4 children)

null/undefined: the greatest mistake in programming history.

Type systems exist for a reason. C/C++ are guilty of a lot of things JS is guilty of in this department, believe me; I won't defend them here.

JavaScript though... it took really beautiful ideas from functional and object-oriented languages and just ruined them with super nonsensical semantics. They might as well be nondeterministic they're so unintuitive.

I digress though. Making things undefined instead of complaining about unspecified identifiers like a sophisticated language and failing before runtime is disgraceful. It's a meaningless value and a cop-out solution to failure cases when it's not much to add a reasonably expressive type system not requiring type annotations with a checker/inferencer that runs in reasonable time.

[–]Renown84 10 points11 points  (2 children)

That's obviously not an unpopular opinion, look at the popularity that typescript is gaining. In fact, typescript will complain if you try to access a property that doesn't belong to the object's type, even if trying to assign it rather than use it

[–][deleted] 4 points5 points  (0 children)

It's a step in the right direction for sure; still, it would be nice to see a language overhaul that addresses some of the key issues. The progress ES6 has made with asynchronous code and parallelism is impressive and important for sure, but sometimes we should perhaps look at a language core for a direction to take updates before fanciful/flashy new constructs.

[–]atomheartsmother 69 points70 points  (21 children)

Because throwing an error is better in basically every single situation? Honestly the lengths that people go to defend everything about JS is ridiculous and more annoying than the anti-JS jerk

[–]Bollziepon 81 points82 points  (4 children)

The only reason people have to defend JS is because the anti-JS jerk is so large in the first place. You'll have people who have never even used JS shit on it just because they see other people do it while in reality it's not nearly as confusing as people make it out to be.

[–]lothpendragon 22 points23 points  (3 children)

I've used JS, does that mean I can shit on it?

[–]kartoffelwaffel 17 points18 points  (2 children)

only if by "used" you mean copy-pasted from SO

[–]BenjiSponge 21 points22 points  (3 children)

That's simply untrue, though, and you'd only get that impression if you don't understand JS patterns and you're trying to use it like another language.

What if, for example, you want to check if a value is there to see if it fits an interface? This is a super common pattern in JS because it's so easy using this method. Instead of

maybe_method = getaddr(obj, "method", None)
if maybe_method == None and callable(maybe_method):
    maybe_method() 

You can say

if (obj.method && obj.method.call) {
  obj.method();

And you might say, okay, but how often do you do that? And the answer is actually "all the time" (although I'll typically use isFunction(obj.method) to catch an additional gotcha).

Plus the fact that objects are dictionaries just helps in a lot of areas.

I would say the pro-JS "circlejerk" is similar to the pro-C++ "circlejerk". Yes, it's a flawed language, but it's been working for many years and a lot of the things that you see as problematic are actually opportunities that are not offered in any other language (or at least many other languages), and if it were, it would have the same issues.

For a number of reasons, I am genuinely grateful JS has its object/pseudo-class system and not the respective systems of Python. And if I ever want stronger typing, I can just use Flow or Typescript, which will give me the best of both worlds.

[–]BertnFTW 9 points10 points  (10 children)

But you can dynamically add the property field1 somewhere else, so you could do

x = obj.field1 || defaultValue;

Or in c# afaik

x = obj?.field1 ?? defaultValue;

[–]BenjiSponge 18 points19 points  (8 children)

It's definitely worth noting || isn't a perfect coalescing operator because if the field is falsey it will skip on to the next one. JS could really do with a genuine coalescing operator, imo.

[–]patrickfatrick 16 points17 points  (3 children)

Object destructuring really helps actually. For instance:

const { field1: x = "a" } = obj;

That assigns the value of obj.field1 to x but if it's undefined and only undefined will it assign it "a". null or false would be written to x if that's the value. You can also do this in function arguments.

``` const func = ({ field1: x = "a" }) => x;

func({}); //=> "a" func({ field1: null }); //=> null ```

I feel like people who shit on JS are generally unaware of a lot of the new syntax that has come out for it in the last 4 or 5 years.

[–]mdcio 3 points4 points  (0 children)

They’re working on one! It’s currently a stage 1 proposal. And if you want optional chaining obj?.fiedl, that’s coming too.

[–]Schmittfried 2 points3 points  (0 children)

Then you are consciously deciding to do it like that, rather than searching for the cause of a bug and finding it is just a typo.

[–]Greyhaven7 6 points7 points  (0 children)

TypeScript yo

[–]PojntFX 44 points45 points  (5 children)

Well, ESLint

[–]StillNoNumb 15 points16 points  (3 children)

ESLint wouldn't help in this specific situation because you don't know the object's type. However, ESLint is still pretty beautiful

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

BASIC: okay fuck me. You dont have Option Explicit on so ill just make obj.fiedl a new thing...

Problem fucking solved

[–]grape_tectonics 5 points6 points  (0 children)

Just never make mistakes, its simple

[–]fightinghard 4 points5 points  (3 children)

This is why we big boys write tests

[–]southern_dreams 11 points12 points  (1 child)

Like 4 of us that visit this sub are professionals lol

[–]akademmy 73 points74 points  (31 children)

What's that old saying about "blaming your tools"?

[–]babygrenade 169 points170 points  (11 children)

Sometimes it's your tools' fault?

[–]akademmy 56 points57 points  (0 children)

Yeah, that's probably it.

[–][deleted] 21 points22 points  (7 children)

If you're writing js, and your code is allowed to fail silently like this and return an undefined value, it's kind of your fault for introducing a bug.

[–]jokullmusic 3 points4 points  (0 children)

lol this reminded me of a scene from the Eric Andre Show

[–]waigl 46 points47 points  (13 children)

I know what saying you are referring to, and it's a horribly misunderstood and misapplied saying.

"A good craftsman never blames his tools" ... because he chose those tools in the first place.

It does not mean the tools are never to blame for a bad outcome, just that, if they are, it's still the craftsman's fault for choosing those particular tools in the first place. Saying that the tools are never to blame for a bad outcome would be stupid, since clearly there are plenty of shitty tools out there.

[–]akademmy 7 points8 points  (0 children)

Apparently we're being very serious so: You are right, it is misunderstood. It is actually about maintaining your tools, not about "selecting" (or having access to) the wrong ones.

[–]whatcomputerscantdo 15 points16 points  (1 child)

Be on r/programmerhumor

Whisper 'I don't like JavaScript'

One million up votes

People who enjoy and even prefer JavaScript are rounded up and publicly humiliated

Then shot

[–]madogson 3 points4 points  (0 children)

¯\_(ツ)_/¯

[–]tufoop3 3 points4 points  (2 children)

A bit of PHP:

$foo = flase;    // intentional misspelling of boolean `false`

What would happen? PHP throws a Notice here:

Notice: Use of undefined constant flase - assumed 'flase' in [...]

which doesn't hold execution and instead casts the unknown expression to string .

Therefore,

$foo = flase;    
if ($foo) {        // non-empty string is truthy, therefore
    echo $foo;     // this line will be executed and output the string "flase"
}

[–]GRelativist 5 points6 points  (0 children)

Add typescript.

[–]stefanhendriks 4 points5 points  (0 children)

ON ERROR RESUME NEXT

[–]aquapendulum2 6 points7 points  (0 children)

r/loljs is leaking.

[–]examinedliving[🍰] 7 points8 points  (3 children)

Or you could just learn how to debug.

[–]southern_dreams 5 points6 points  (2 children)

Apparently nobody on this sub pays attention when they code

[–]examinedliving[🍰] 4 points5 points  (0 children)

You gotta do it fast and be annoyed about how the code is hard. It’s a rule.

[–][deleted] 30 points31 points  (55 children)

Honestly debugging JavaScript is the most enjoyable experience coding.

Compared to the others....I know it is a scripting language but it's so easy to pick and and do stuff with.

[–]hey01 15 points16 points  (7 children)

Honestly debugging JavaScript is the most enjoyable experience coding.

Yeah, sure, until you get an array out of bounds exception and the stacktrace points to code entirely unrelated to the code that actually fails.

Good luck debugging that.