Javascript is hell

I just started my first major javscript project. I've done very minimal javascript development in the past, but I do have a firm understanding on how browsers work and how the DOM is manipulated. However I never realized the extent to which this language is absolute shit.

This whole "asynchronous event based callback" garbage sounds nice in theory, but honestly would work better if it just allowed users to sleep manually and then periodically check to see if their event has fired. THERE IS NO SLEEP FUNCTION. Tradititional loops are essentially useless for most DOM based operations because of this, instead everything has to be formulated in a ridiculous recursive callback based structure, making it impossible to trace the logic of the script without going in depth into the workings of the code. It's completely unintuitive to write a non-terminating script, especially due to the fucking non-standardized recursion limit. If you want things to happen in order, then you have to go through tremendous pain to make the asynchrony synchronous.

The language itself is designed in such a way that its easier to incomprehensible shit, than a clean understandible script.

The project i'm working on is a bot in phantomjs, and I'm thinking of just scrapping it and re-implementing it in C, even though I will need to do some reverse engineering of websocket protocols and POST requests.

Other urls found in this thread:

en.wikipedia.org/wiki/SXML
developer.mozilla.org/en/docs/Web/JavaScript/EventLoop
blog.carbonfive.com/2013/10/27/the-javascript-event-loop-explained/
developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers
en.wikipedia.org/wiki/AmigaGuide
twitter.com/SFWRedditImages

JS was never intended for the things that are being done with it today.

Why would you write a bot in C? Just use Perl.

I don't know perl very well, although I will look into writing a bot with it. C is just the language I am most comfortable with.

C is a good language but make sure you use the right tool for the job as cliché as that might sound.
Perl is far better suited for making anything botlike. Of course you could do it in JS or Python but those languages suck as you already know.

Re-write it in PHP OP

What are you exactly trying to accomplish, man? I often use for and while loops to generate websites dynamically (nah, not for production, don't worry) and they work well. If you are talking about periodically updating the page, then you are right. It's a bit weird to program the execution of a function in X time, and sometimes I just wish JS had proper threading or actors to avoid locking down the whole website as the script finishes processing.

Sounds like you are using JS libraries. Vanilla JS does have some callback functions in its standard library, but you don't have to use them constantly unless you are doing something very specific.

Nigger, that's not how you do it. While it does look like it's "recursive", it will never reach stack overflow since, if you are using setInterval() like you should, you would be actually calling the function over and over, like in a while loop, instead of doing it recursively.


Because C is the best language ever made for everything, you fucking ignoramus.

Has anyone had to pick apart a javascript program using the "everything is a fake event" pattern?

Heres a simplified algorithm of my bot:
1. Create a mutation observer on the element I need to interact with
2. Interact with the element using phantomjs
3. The mutation observer callback checks if everything worked and triggers the next interaction
4. After everything is done, do it all over again

A recursive callback pattern is necissary for this, as far as I can tell, While loops just won't work since all of it is asynchronous.

the Phantomjs API is all asynchronous and opening pages / evaluating code within the page is all done through callbacks. But mutation observers are vanilla and have the same problems.

Please don't joke about this. The average Holla Forumsie acutally believes this.

Take a look at setInterval(). It's pretty much like telling the browser's main loop "wait for X seconds, then execute this". If used as a tail call, you let the function return (because setInterval is a s y n c h r o n o u s), but it will still get called at the programmed time. It's the closer you can get to a while loop with sleep, but much less verbose.

Thanks. I never looked into that function, but I will try it out.

The average Holla Forumsie is a senior low level programmer with years of experience in C (in fact, he possibly hasn't even touched other languages ever because he already knows they are shit), embedded devices and kernel code. He got employed for the first time, without even having finished his degree, 300k starting to write low level code (involving lots of C and assembly) for some undisclosed company which only produces very complicated and very reliable enterprise (but never Pajeet tier, of course!) software. You would know this if you were a true Holla Forumsie.

10/10

this made me chuckle. But honestly C is a good tool for a lot of things, not becuase of the language features or it's low level nature, but because there's a library for anything.

...

Except C isn't medicore on its own without libs, its just not fantastic.

Yes, it's hell. You live under a rock or something?

And now you get the pleasure of using (relatively) titanic packages which attempt to fix it with a huge toolbox of functionality like Node.JS.

What you might not've yet realized is that the internet is hell. The technologies it's built on are garbage, through and through, mismanaged by a long line of companies; they embody design by committee. The GPU world has their own similar example; the Khronos Group is the same shit.

Have you ever noticed that it's only when individual companies do their own thing and make something that's so good it cannot be denied as the global standard and then price it so attractively that it can that we actually get good software? Assuming their ideas aren't stolen or fail to launch altogether because they're too small.

HTML was alright.

Yeah, I can't deny that. It's also one of the most "compatible" languages out there, which is probably the reason so many libraries have been made with it. That said for every C library, you are almost guaranteed to find bindings for other languages.

No, it's not. Even though compression helps, it's still one of the most verbose languages you could use for networking, and that makes absolutely no sense. HTML also includes in its spec a lot of "if the developer fucked up writing this, just ignore it and try to solve his error!", which is stupid. Most serious languages will simply refuse to compile if something is wrong with its syntax, but somehow you can keep fucking up with HTML and still get a working page. Stuff like "this tag needs to be closed, this one doesn't" just bloat the fuck out of the spec and makes writing parsers much more complicated. S-expressions are much superior, except for attributes clarity, but you can do just as well without that.

I would say the only half decent thing is HTTP, and it still requires a couple changes to make it acceptable. I would like to say HTTP2 fixes that, but it fucks with other things as well so you just have a differently fucked protocol.

The fact that HTTP2 can't easily be generated by a human using netcat is problematic.

That's more of a network effect than a property of the language. Note that your OS and parts of your hardware are heavily designed around C.

Why distinguish the network effect from the language itself? Circumstance surrounding a language is as important as the the properties of the language itself. All that matters is what can be done now, not what could have been done.

I don't know that you could get much less verbose without making it less accessible to non-programmers.

I agree the lack of strict interpretation is a big problem, though. They dropped the whole 'doctypes' silliness with the last spec but I'm not even sure what the point was in the first place since it's almost impossible to actually choke a "compiler" (browser).

Then use xhtml5. The spec allows much of the junk like document.write to be bypassed, and it throws an error if not well formed.

The browsers which don't support xhtml are dead as shit now, so you're free to use the stricter variant.

Because what's possible now is subject to change and people tend to take the C Machine and all its outgrowths for an invariant of computing. Why would you encourage these delusions?

I was replying to an user saying HTML was good. Furthermore, properly written HTML is undistinguishable from XHTML so the point is kinda moot. I know I can write proper HTML (most people can, anyway), but there are some people who can't and rely on HTML, a "tolerant" language, being the predominant technology. Browsers are spending time imaking complicated parsers for the sake of these fucktards who can't even read for ten minutes to fully grasp the basic syntax of HTML, and that's the real problem.

Furthermore, XHTML is still verbose.


S-expressions are fairly easy to understand.
en.wikipedia.org/wiki/SXML
If you don't think this could be the tersest shit ever if implemented properly then get the fuck out of my face.

What exactly do you mean by saying browsers need to cater to retards who can't read?
Like what kind of mistakes are we talking about here?

The example looks breddy nice, although it also kind of looks like Scheme had a stroke.

Who cares about verbosity.
After compression, XML and SXML representations will be almost identical.

Browsers will accept almost anything as HTML and display the result, no matter how badly mangled. Modern browsers use resource-expensive parsing algorithms that will attempt to close tags, fix nesting issues and even add missing elements for you.

As a language (of sorts), HTML is almost alone in that the "compiler" won't break and tell you to fuck off for writing erroneous code. This is because if browsers were strict and threw breaks or error messages, users wouldn't tolerate it.

Your sperging out over nothing.


Serve a document as application/xhtml+xml really slowly. Byte-wise if you like. The browser inserts "missing" elements speculatively, despite not knowing if the final document will be well formed xml or valid html.

Is this process resource-expensive? No / I definitely don't care.

Have you looked into async / await in ES7? While internally it doesn't change what actually going on, I've heard it makes it easier to write code in the manner you are describing.

phantom is still on ES5, slowly adding ES6 features.

slimer has most of ES6 due to being firefox based, but still no ES7

If this is for some automated bot thing, don't know if it helps, but I've been using Capybara and PhantomJS (Poltergeist Gem) for testing stuff (1.9.something because of broken jqxhr stuff in 2.1.1). I think Capybara features something where it will automatically wait for something to appear on the dom, so you don't even have to write any sort of callback stuff. I think it just blocks until it's there; if that's what you're looking for.

How do I force a browser to treat my file as XHTML5 rather than HTML?


You can blame Microsoft and Netscape for that. They had a race to the bottom on whose browser could be more compatible and started accepting all sorts of shit. Even if the browser can't make sense of some piece of code it would rather display a mess than admit that the code is shit.


There are long-term consequences to this. It has reached the point where the internet has become so complicated that it takes and entire enterprise to write a browser. That's why we are stuck with the current mess of browsers where you can't just go in and fix a thing. No matter how many forks of Firefox or Chrome there are, all of them die in a short time because the code is such an unmaintainable mess.

mv ass.html ass.xhtml

Are there any alternative markup languages? It may take 30 years but if there is a consorted effort we may be able to rid ourselves of HTML

var a;var b;a = 2;b = a;b = 3;console.log(a);

Do you know what a is? I'll give you a hint. It's not 2.

You can still author PDFs in a text editor if you're sperg enough

it is on my interpreter

Same here. is a faggot, and my mom says faggots go to hell.

Fine I guess it doesn't happen with integers.
var a; var b; a = { key: 2 };b = a; b.key = 3;console.log(a);

Do you even know how objects and pointers work?

its due to objects being passed by reference while other thinhgs aren't.

No shit, that isn't the point.

You're right though it is a moronic language feature.

Then explain to us our grand epiphany, oh lord of the references.

Congratulations, you discovered pass by reference! The same thing happens in Python and a thousand other languages, and that's a good thing because it means that when you have a dictionary or array with a hundred thousand members the whole thing doesn't have to be duplicated just because it's referenced elsewhere.

This is a very good and common language property.

h-hey..!

First, understand this:

developer.mozilla.org/en/docs/Web/JavaScript/EventLoop
blog.carbonfive.com/2013/10/27/the-javascript-event-loop-explained/

You don't need a sleep function; set an handler on your event. If you need to poll some value at a regular interval, write a function that calls setTimeout with the function itself passed as a parameter. (Alternatively, use setInterval).


Chain promises together.

kek

This is what happens when you brainwash kids with the OOP meme.

I blame CS programs teaching students Java as their very first programming language. Thankfully there are EE programs that still start you off right.

With assembly.

Huh? The Event loop vs Threading paradigm dichotomy is totally orthogonal to Object-Oriented v Procedural paradigm dichotomy. (Besides, Java allows you to suspend threads with a sleep function.)

I don't get it. It says JavaScript is single-threaded but at the same time it's non-blocking? Here is how I imagine the event-loop to work: there is one worker who is sitting at his desk and doing nothing while nothing is happening. Then someone comes by and gives him a note (a message) to go bash his head against the wall for five minutes, so he gets up and starts bashing his head against the wall. In the meanwhile another note arrives telling him to run three times around the block. How can he be running around the block when he's still busy bashing his head against the wall?

In the second link the thread is told to make a request to a webserver, and immediately afterwards he is told to print out something. How can he print the message while still going around to fetch the request from Google? Or does message passing simply mean that there is more then one step? Like first open all the messages, read them, build a to-do list and along the way do all the things that don't belong on the list (like printing out to the console). And only after having build the to-do list does he start checking things off it.

He can't. What you do is "fake it", by handing him two notes at the very beginning. The first note says "bash your head against the wall indefinitely" and the second note says "after five minutes have passed run three times around the block".


Welcome to hell.

But that still means that the head-bashing will prevent the running for five minutes. If someone came by with a note saying "stamp this letter" they would still have to wait for five minutes.

...

You can tell he doesn't know anything about Python cause he didn't specify 2 or 3 :^)

That's what callbacks are for. You put a listener on some event, and that listener will work asynchronously. Whenever that listener is triggered, it will add the callback function to the message queue awaiting for execution. It will be executed as soon as the previous called functions finish. If those called functions never finish (because you put an endless while loop in them), the callback function will never be executed.

Basically, if he is told to bash his head indefinitely (while true loop), he will never get to run around the building, even though the to-do tasks will still pile up on his desk, because he is not the only person to put tasks on his desk, even though he is the only capable of completing them.

Mozilla's setTimeout() example in that page makes this very obvious. Basically, the guy is told to open a folder of tasks (function). He opens that folder on top of his to-do list (queue) and the first task tells him to make some coffee ("start" console.log() call). He makes some coffee, and continues reading his folder for new tasks. Second task says "add to your to-do list now: pee on the coffee" (callback 1); since he is still reading the folder, he puts this memo under the folder, and keeps reading. Now he is told to drink the unpeed coffee (middle console.log()), and he does so. Now, the next note says "add to your to-do list in 0 seconds: drink the coffee" (second callback). He still hasn't finished reading the folder, so he puts it under the folder and the pee command, for whenever he has finished with that stuff. Third command basically tells him to bash his head once against the table (third console.log()), and he does so. Wew, that was hard, but he managed to avoid drinking his own pee. The guy closes the folder, since it's done, and looks into the next task. "Pee in the coffee", it says. He does so (first callback execution). He removes that list from his pile, and reads the next one: "drink the coffee". JavaScript salaryman wished he was truly asynchronous

Node I/O is internally multithreaded. The application code (bashHead(), peeInCoffee()) is single-threaded. If you want your javascript application code to use multiple threads, use Workers.

developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers

Thank you, that explanation made sense.


So operations are only asynchronous and non-blocking if they have been written to be like that. Doesn't that put us right back on square 1 again?

C is ok with just the standard library, if you just want to write simple software. But if you want to make a roguelike or similar game, it's nice to have curses at least, although there are ways around that, like using ANSI escape codes for cursor positioning.

There are lots of such languages, many of which even predate the web. Here's one that was shipped with all Amiga computers from 1990 and onward:
en.wikipedia.org/wiki/AmigaGuide
I guess Apple had standardized something like that even earlier, but the idea is much older.

...

Learn you write async code you shitters. Even in languages were you can do threads + non-blocking it's considered backwards and retarded to do it like that today.

Learn grammar you preschool

lol

Or for php fags header( 'content-type: application/xhtml+xml' );

This causes the parser to skip the logic for , document.write, and possibility other legacy quirks.