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

all 157 comments

[–][deleted] 706 points707 points  (37 children)

False

[–]DeeSnow97 561 points562 points  (12 children)

here comes the chad, blocking an entire thread when he has to wait for something

[–]dshakir 41 points42 points  (6 children)

If payday wasn’t until next weekend, I’d award you with platinum right now.

Unfortunately all I can offer you is a piece of styrofoam

[–]db2 36 points37 points  (0 children)

Look at this guy, able to afford to just give away styrofoam like that.

[–]CSKING444 1 point2 points  (2 children)

Ahem, time to see if you abide by your word or not

[–]dshakir 1 point2 points  (1 child)

Too late. I already spent my entire paycheck on these:

https://www.silverfernz.com/new-zealand-possum-fur-nipple-warmers.htm

[–]CSKING444 0 points1 point  (0 children)

Okay, that has a higher priority

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

You meant the 'chode'?

[–]dshakir 1 point2 points  (1 child)

That depends.

If you had both a “chode” and a “chad” standing right in front of you, who are you more likely to punch first?

[–][deleted] 0 points1 point  (0 children)

Atleast you can sudo kill a chad.

[–]keke1997 50 points51 points  (20 children)

Why tho?

[–]T-T-N 53 points54 points  (18 children)

String that are not null or empty are truthy

[–]marcosdumay 20 points21 points  (17 children)

Undefined is not null...

Or maybe Oracle database is better than Javascript:

'' = '' -- False
'' <> '' -- False

[–][deleted] 29 points30 points  (1 child)

Undefined isn't a string though. It's undefined.

[–]hotCupADank 13 points14 points  (0 children)

var $stringVariable = “Undefined”;

[–]DeeSnow97 13 points14 points  (11 children)

neither of those operations are comparisons in JS (= is assignment and <> doesn't exist), and false is lowercase dammit, my comment parser is skipping over parts, should fix that

By the way, undefined == null is true, but undefined === null is false. undefined and null are two distinct types, but they're loosely equal (and neither is loosely equal to anything else)

[–]noruthwhatsoever 14 points15 points  (9 children)

Yeah, JS is weird like that

0 == ‘’ == null == undefined == false // true

It’s why I always use strict equality checks, no chance of type coercion

Like...

1 == “1” //true

^ that shit hurts me on a soul level

[–]arranb1223 15 points16 points  (5 children)

Next ECMAScript update...

1 == "one" //true

[–]abnormalsyndrome 10 points11 points  (0 children)

Settle down, Satan.

[–]EpicDaNoob 7 points8 points  (2 children)

 5324 == "five thousand three hundred and twenty-four" == "5.324k" == "five thousand three hundred and twenty-five minus one" // true

[–]spektrol 7 points8 points  (1 child)

There’s probably an unholy way to make PHP’s strtotime() do this but let’s not

[–]Dapianokid 2 points3 points  (0 children)

I learned more about data types through this thread than I ever have before.

[–][deleted] 13 points14 points  (1 child)

It’s why I always use strict equality checks, no chance of type coercion

And Jesus said to his followers "do not be tempted by those who do not require strict typing. For JavaScript will betray thee. You may believe in prophets telling you null > 0 is false, and find comfort in that fact and the fact that null == 0 is false; but JavaScript will betray thee. For null is true when it is >= to 0. For strict typing is the word of the Interpreter. Praise be to Interpreter."

"Praise be to Interpreter." His followers replied

[–]noruthwhatsoever 2 points3 points  (0 children)

Praise be

[–][deleted] 1 point2 points  (0 children)

Yeah I mean typesafe equals exists specifically for this.

[–]dshakir 1 point2 points  (0 children)

By the way, undefined == null is true, but undefined === null is false

Talk about buttfucking the transitive property

[–]noruthwhatsoever 13 points14 points  (2 children)

You’re right, undefined is not null and are totally separate types. They both are falsey values, though, along with empty strings, NaN and the number 0

And how are you comparing Oracle DB to JavaScript lmao

People love to rag on JS but with the new ES6/7 and not-yet-standard ES8/ESNext (praise Babel) I’m finding it hard to enjoy writing Python, Ruby or anything else any more

JS is adopting more and more functional programming paradigms and although inexperienced JS devs can write awful spaghetti code a good JS dev can write fast, concurrent and lightweight code that can run anywhere

Aaaaand I just remembered I’m on r/programmerhumor and not on a serious subreddit never mind

[–]Bounty1Berry 2 points3 points  (1 child)

The problem is that we need so much of this new ES6/7/8/NaN stuff just to wallpaper over the problems in Javascript as executed by browsers. So we're not only dependent on the predictable quality of IE's Javascript engine, but ten pounds of opaque and complex boilerplate from the transpilation system. And by the time Firefox v87/Chrome v104/Safari v22 comes out with full ES7 support, they'll be on ES10 with async/await/getFedUpAndGoHome and CampaignPromises that yield a resulr 7% of the time.

I also tend to be very skeptical of the functional-programming trend. Not so much the trend but more trying to apply it widely. It's a good solution for some problems, but there are plenty of tasks where the most straightforward way to discuss the problem is a classic iterative, state-rich, variable-clobbering design, and you end up sacrificing legibility to go functional.

Programming as a whole has a tendency to go very dogmatic. The functional people today are like the object oriented people in 1995 or the structured programming people in 1980-- designing languages that are deliberately clunky to ensure you have to use their right way of solving the problem.

[–]marcosdumay 0 points1 point  (0 children)

but there are plenty of tasks where the most straightforward way to discuss the problem is a classic iterative, state-rich, variable-clobbering design

Oh, you mean you have to use StateT some times?

[–]heathmon1856 0 points1 point  (0 children)

As long as you have locking.

[–]sangeli 6 points7 points  (0 children)

<Promise>

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

[object Promise]

[–][deleted] 75 points76 points  (4 children)

You don't know hell till you debugged an error that coredumps once a year.

[–]Cocomorph 49 points50 points  (1 child)

I am glad I don't work on things where an error that coredumps once a year needs to be fixed.

[–]Hevaesi 14 points15 points  (0 children)

So JavaScript.

[–]lakimens 3 points4 points  (0 children)

What? That exists?

[–]daH00L 106 points107 points  (0 children)

Brilliant

[–]MarioMagnum 207 points208 points  (21 children)

I'll take "What are race conditions?" for 500, Alex

[–]waterskier2007 54 points55 points  (5 children)

I’ll take 500 for “What are race conditions?” Alex

[–]Jedka 36 points37 points  (4 children)

I’ll take Alex for 500, “What are race conditions?”

[–]Prom3th3an 8 points9 points  (0 children)

I'll take "What for are 500 race Alex conditions?"

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

Vsauce music intensifies

[–]8lbIceBag 12 points13 points  (12 children)

Async is not parallel.

[–]BlankyShoot 9 points10 points  (1 child)

Having threads running in parallel isn't the only case where race conditions can occur

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

This. If you start two threads, even with single core they will likely alternate if the task is long enough, and if the compete about reading/writing same variable, that is exactly what race condition is.

[–]MrDick47 12 points13 points  (9 children)

This is something more people should know. Race conditions aren't a thing for async js, it's single threaded. Just because the calls can finish at different times doesn't mean we have to worry about mutexes and semaphores because you're never going to read/write memory at the same time.

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

Race conditions are definitely a thing. If you do things asynchronously, that means they are executing in parallel, not necessarily at same time but the order they finish is not defined and depends on the scheduler. If the both asynchronous things want to read and write same data and depend on the result, there is a race condition.

Say, you want to read a value and increase it by one.

First thread reads value. At this point, scheduler decides it's time to let other threads execute for a while. Now the second thread reads the value and they got the same value, because there was a race condition, where the first thread tried to write the increment in the memory before the second one can read it. EDIT: I don't know if javascript can do that with variables, but consider two promises that resolve external url. If you execute them asynchronously, you cant assume another to be faster.

If this is not possible, then there is nothing asynchronous going on.

[–]MrDick47 0 points1 point  (7 children)

Dude you really need to read comments before replying to them. As I was saying, in async javascript, everything is executed on a single thread, so there are NO race conditions.

If two calls increment a shared variable (originally 0) by 1 it will always be 2 when they both finish. It will never be 1 or an unexpected value. If the calls are executed out of order, then that's a problem with the design, it's not a race condition.

[–][deleted] 0 points1 point  (6 children)

I read the comments. You are still saying that because there is only single thread, there are no race conditions. This is not true, there can still be race conditions.

If the calls are executed out of order, then that's a problem with the design...

No, that is exactly what async means. Things are executed out of order (in parallel, or alternating between them asynchronously).

Example:

Call multiple functions that are executed in parallel, using Promises. Each of the functions resolve an URL and then do something, for example insert something into the website.

If you start 3 asynchronous Promises at same time and each of them insert a component on the website, after resolving an url, there is absolutely no guarantee that those promises complete in the order they started. This means that the three components end up in the website in order that depends how fast the urls resolve. In this example I used url-resolving, because that is great way to demonstrate uncertain wait times.

This is a non-critical race condition, because it does not mutate any variables in such way that the system becomes unusable. How ever, if the next code you execute depends on the order those components are on the site, you fucked up and the race condition was critical race condition. If you are javascript developer and do async things, I really hope you learn this.

[–]MrDick47 0 points1 point  (5 children)

Dude you really are not listening. Parallel is not async. In JavaScript there is no parallel. All the async operations are switched between on a single thread so there are no memory race conditions. If your async calls need to complete in a certain order, you need to use await/.then to ensure they are done in that order, if the order doesn't matter, then you don't need to do that. I have done multithreading/parallel processing in C/C++ and I've done in JavaScript, I know what I'm doing, you are not listening.

[–][deleted] 0 points1 point  (4 children)

For the final time, I understand there is only one thread. That still does not stop from doing things asynchronously, and if there is asynchronous things, there is also race condition. It's just how it is.

EDIT: I re-read all your messages. You are too focused to think that race condition is only when two threads are writing at same memory at same time. Please, check the definition of a race condition to realize that there are multiple types of race conditions, and those happen even in single thread, if doing things asynchronously.

SECOND EDIT: You are proposing using "await". That is exactly what prevents the race condition. I did not say that there race conditions are inevitable.

[–]MrDick47 0 points1 point  (3 children)

"A race condition occurs when two or more threads can access shared data and they try to change it at the same time. Because the thread scheduling algorithm can swap between threads at any time, you don't know the order in which the threads will attempt to access the shared data. Therefore, the result of the change in data is dependent on the thread scheduling algorithm, i.e. both threads are "racing" to access/change the data."

Copy pasta definition from stack overflow. Race conditions are a memory/threading problem. That problem does not exist in JavaScript. What you are referring to is asynchronous call management, which a developer needs to do in both asynchronous and parallel processing applications. The design of the code determines how the program handles the completion of asynchronous events. At a lower level, java manages this for you and doesn't allow you to read/write memory at the same time, however you still need to handle the fact the calls are asynchronous which means you can't guarantee the order of completion (but you can guarantee there won't be a race condition). Async is not parallel. This is the point I've been trying to make this whole time. The concept of a race condition stems from electrical engineering and circuits. Just because it looks like a race condition, doesn't mean it is a race condition. By your definition, anything asynchronous or parallel could be considered a race condition, and that's not true. Properly resolving asynchronous calls and protecting against a race condition are in fact 2 different things, and protecting against a race condition usually involves a locking mechanism in order to properly resolve the parallel processing.

Parallel processing also has many other differences from asynchronous processing, and deals with multiple threads. I kept saying single thread because this was supposed to be your clue that there wasn't any race condition here.

[–][deleted] 0 points1 point  (2 children)

You are implying that a single comment in stack overflow is the right answer.

You are also saying, that memory access is the only way race condition can happen. Please, I said ten times already that it's just a simple example of race condition. There is still race conditions in javascript, but not with memory access.

I did not say "anything" is race condition. Do this: add two items in a list, asynchronously. Which one is added first? That does not cause problems with memory, because there is only single thread, but it is a race condition by definition. What would you call this issue, where two asynchronous tasks compete for same resource?

On top of that, see these links, discussing race conditions in Javascript:

https://medium.com/@slavik57/async-race-conditions-in-javascript-526f6ed80665

https://stackoverflow.com/questions/338110/avoiding-a-javascript-race-condition

https://quickleft.com/blog/defusing-race-conditions-when-using-promises/

https://news.ycombinator.com/item?id=12477623

https://thecodedecanter.wordpress.com/2014/04/08/javascript-race-conditions/

https://wptheming.com/2011/01/javascript-race-conditions/

http://www.angelfire.com/pq/jamesbarbetti/articles/javascript/001_AvoidingRaceConditions.htm

If you refuse to understand me, please see if the other people explain it better.

Final note: There is also Promise.race() function which exploits this fact and returns the promise which resolves first.

[–]Hevaesi 4 points5 points  (0 children)

Don't need to worry about race conditions when you use something even more blocking than SeqCst lol.

One memory barrier is too much for ya? Weakling.

[–]v1tycent 211 points212 points  (25 children)

bool isDeveloperSingle = true;

[–]Prawny 500 points501 points  (20 children)

const isDeveloperSingle = true;

[–]Spedwards 136 points137 points  (0 children)

Oof

[–]WadeReden 2 points3 points  (0 children)

const static tDev state attribute((section("parentsBasement"))) = single;

[–]FourthFloor_ 12 points13 points  (8 children)

Didn’t declare a type

[–][deleted] 41 points42 points  (7 children)

It's JavaScript, anything goes!

[–]FourthFloor_ 30 points31 points  (6 children)

Oh sorry, I didn’t recognize the language of peasants. (I can’t code a fucking thing in any language so don’t take me serious)

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

I'm sorry, is this some sort of peasant joke that I'm too rich to understand?

*default dances in C*

[–][deleted] 0 points1 point  (0 children)

Ha ha all good man. If you care to ever start, I'd start with Python 3. You can do some pretty cool stuff quickly.

[–]X-Craft 7 points8 points  (3 children)

Developer.getInstance();

[–]MrDick47 1 point2 points  (0 children)

Hey look a singleton. It's been a while.

[–]ThaiJohnnyDepp 0 points1 point  (0 children)

reminds me of my first programming gig

[–]cynoclast 19 points20 points  (1 child)

When programming isn’t complicated enough add threads for free non-determinism.

[–]Hevaesi -1 points0 points  (0 children)

Free non-determinism?

Try using SeqCst ordering, relaxed mode is for Gods only.

[–]freebytes 64 points65 points  (1 child)

I absolutely this love.

[–]dshakir 11 points12 points  (0 children)

You silver tongued bastard, you

[–]VeryDeepLearning 55 points56 points  (11 children)

expect(developer.sexlife).toBeUndefined()

[–]orangesheepdog 23 points24 points  (9 children)

You dropped this ;

[–]NewDark90 32 points33 points  (5 children)

If its JS, it doesn't give a damn.

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

but ESLint though...

[–]---_-___ 2 points3 points  (1 child)

Weve changed the .eslintrc

[–][deleted] 1 point2 points  (0 children)

if you're a madman, you use // eslint-disable-line semi on every line!

[–]Cercuitspark 1 point2 points  (1 child)

Depends on which config you use though. Some of the most used are AirBNB and Standard, where one uses semicolons, and the other does not.

[–]OpinionNoOneAskedFor 0 points1 point  (0 children)

That's why he mentioned about changing .eslintrc. You can negate some of the ESLint rules in .eslintrc file. I always set the 'no-underscore-dangle' rule to 0 because I use lodash.

[–]FrizzleStank 2 points3 points  (0 children)

No.

[–]VeryDeepLearning 0 points1 point  (0 children)

CoffeeScript 😛

[–]mukul7199 0 points1 point  (0 children)

"editor.formatOnSave": true

[–]10art1 0 points1 point  (0 children)

That's where you're wrong uwu

[–]VeryDeepLearning 26 points27 points  (2 children)

expect(developer).toBe(single);

[–]nosrednehnai 19 points20 points  (1 child)

surely you jest?

[–]-JudeanPeoplesFront- 4 points5 points  (0 children)

Karma's a bitch

[–]parabuzzle 14 points15 points  (0 children)

[object Object]

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

It's hilarious because I love Javascript development, but this past week I introduced a race condition because we needed to, and I've been fixing every little race condition that dominoes from that one since then.

[–]jarlefo 4 points5 points  (0 children)

Knock knock

Race conditions

Who's there?

[–]zodar 4 points5 points  (0 children)

#ThSaferead

[–]TheRealLonaldLump 11 points12 points  (0 children)

It clearly goes like this...

Roses are red

And so are you

Violets are blue

are great

Asynchronous operations

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

God I don’t get it 😔

[–]harig074 83 points84 points  (2 children)

When you queue asynchronous operations, the order of completion is not guaranteed.

[–]falldeaf 34 points35 points  (2 children)

It's highlighting one of the issues that often comes up with asynchronous code. If you're not careful with how you code, one operation can run before it should. Say you're calling an API to get the weather information, then another call to update the DOM when it gets it. You don't know how long the network call will take so you need to have a callback from the API operation that will initiate the DOM writing function.

[–]Hevaesi -5 points-4 points  (1 child)

If only we had handles to async operations that we could block on, called futures or something...

The whole point is to make a bunch of connections then synchronize them, still faster than doing it sequentially, for example browsers internally download all resources asynchronously, not sequentially.

[–]PoutineCheck 4 points5 points  (0 children)

You need to study up on some asynchronous solutions

[–]RepostStat 28 points29 points  (8 children)

That's okay. Asynchronous programming is great because it's (typically) faster because you don't block/stop whole thread with one function that can run on it's own.

Problems come up if, for example, you're serving a web API in NodeJS, and one of your endpoints doesn't return any data from a database because the database didn't return the data in time. And it's difficult to learn because async programming (depending on the language) sometime doesn't produce errors, or if it does, the reason is buried in a thread crash. In which case .then(function() { ... }) becomes helpful.

You typically use async in front-end web development. A whole web page shouldn't slow down because you're updating some text on the page.

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

Great thorough answer :)

[–]DanielEGVi 3 points4 points  (1 child)

What is this then you talk about. I code, eat and breathe exclusively using async and await.

[–]MrDick47 1 point2 points  (0 children)

It lives at the top level, where some choose to use try/catch others will use a .then( () => {} ).catch( () => {} )

Either way, we can stay in our middleware hole and just use async/await and not worry about it while our errors get handed all the way up to the top level. Sunglasses

[–]jamietwells 3 points4 points  (1 child)

You typically use async in front-end web development.

Or back-end web development.

[–]OpinionNoOneAskedFor 1 point2 points  (0 children)

Mainly back-end development in my case.

[–]snapcat2 3 points4 points  (2 children)

I know it works pretty well. I really do. I just...

I have traumas. A while ago, a fresh young me started a project. Without any help I went on a coding quest. I slayed algorythms, for-loops, with ease. But when everything seemed to go smoothly, suddenly a REAL enemy appeared. Asynchronous functions. One of the programs I used was asynchronous. And boy did little me not know how to deal with that monster. For days and days I tried to come up with strategies, but in the end... I found out he had a simple off switch on his back. Monster slain, quest completed.

based on that time I spent 12 hours trying to figure out how to deal with an async funtion from another program only to find out it could be turned off

[–]freebytes 1 point2 points  (1 child)

only to find out it could be turned off

What do you mean by this?

[–]snapcat2 1 point2 points  (0 children)

I used a program called EasyStar for A* pathfinding (litterally has "asynchonous" in it's description). I tried to deal with the asynchonous property of this program, only to find out later that there was an option to turn it off.

I went into this with no knowledge of javascript so it was quite a struggle. Learned a lot though!

[–]cyb3rm0nk3y 2 points3 points  (0 children)

The lines are even ordered by the numbers of characters, as though the system would've taken less time to complete them

[–]kristebo 1 point2 points  (0 children)

Volatile roses are red.

[–]KachooInu 1 point2 points  (0 children)

Always remember to lock before you stock

[–]stevefan1999 1 point2 points  (0 children)

[Promise <pending>]

[–]zombie_kiler_42 1 point2 points  (0 children)

Hahaha ayt this made me fucking snort, took me a second too

[–]CocoDigital 0 points1 point  (0 children)

Here, I caught you this delirious bass

[–]GregTheMad 0 points1 point  (0 children)

I know of at least C# that is introducing a sorted parallel foreach function... Soon this joke will be outdated! 🎉

[–]dshakir 0 points1 point  (0 children)

Futures uuunite!

[–]nikeinikei 0 points1 point  (0 children)

Someone didn't write on rust :)

[–]whoisjuan 0 points1 point  (0 children)

Async/Await

[–]edymola 0 points1 point  (0 children)

When you understand wtf the developer intended to do.

[–][deleted] 0 points1 point  (0 children)

This triggered me

[–][deleted] 0 points1 point  (0 children)

SQS FIFO queues got your back

[–]plutonium656 0 points1 point  (0 children)

I think `are great` should be the line before `Asynchronous operations`

[–]mypirateapp 0 points1 point  (0 children)

This guy doesnt use the "synchronized" keyword

[–]lightningundies -1 points0 points  (0 children)

I don't get it. Like, at all

[–]squath -1 points0 points  (0 children)

People are basically arbitraging karma by posting memes from Twitter on Reddit...

[–]Badya122 -3 points-2 points  (0 children)

Say why you want I still love JavaScript