📜 ⬆️ ⬇️

You broke my javascript

A long time ago

Previously, everyone wrote a lot of disgusting JS right on the pages, without thinking it over, and it was very bad. I suspect it is still being written this way, but it is done by the same people who don’t read blogs or habr, so let's not think about them (then, of course, we’ll have to be with them in the same project and - God, no, please, just not this )

The coolest, cool and magical thing that happened in JS is that no one in large organizations wanted to deal with him, staying in his calm world of perfectly organized layers of abstractions from factories and magical XML injection frameworks.

And it was great for those who, like me, wanted to receive a salary of large organizations, without getting into the layers of disgusting "best practices" and performance problems arising from an uncontrollable fear of giving access to the database at least to anyone other than DBA .
')
Moreover, when all these performance problems arose, we could save everyone by writing a frontend to JS, which pretended that no problems existed and it all worked perfectly, despite the melancholy quality of the backend.

We reached js heyday

With the advent of jQuery, our life has become even better, and the world around has begun to gather from small reusable jQuery plugins. Finally, we reached our prime when NPM arrived, and we began to use a more or less efficient system of modules for working with autonomous widgets.

I imagined a future where I could work with great teams on great code from small modules and autonomous functions and widgets. I even thought that we would be able to win control of the code from corporate maniacs with their libraries, ORMs, patterns and practices, and the factories factories based on them.

We followed the sanity process and did excellent things from a great code, freeing ourselves from the shackles of nasty corporate frameworks.

You broke everything

Now, I still sometimes write on JS, but more often I work with Erlang, creating services for streaming / transcoding video and all that (that's why I rarely write to the blog), but in general, my backend is very cool, and my frontend is also quite currently cool (i use React from facebook and npm for everything else). But then I stumbled upon an entry on Stack Overflow, which reminded me of the times of corporate development, and to my horror I discovered that this venereal disease, spread by Java, is already appearing on the face of my Internet.

Angular.js: service or provider or factory?

Well, this question in itself is not terrible yet, let's look at the most popular answer (not the most popular one anymore - it was mislead after this post) and we will absorb all of its wisdom, which, judging by the reviews, undoubtedly is there:

Wow! Thanks for the detailed explanation. Now everything is clear and easy. Very cool!!

If I were a kind person, I would say that this comment is an irony and a very good example of the law of Po, but, having read everything, I feel that the matter, alas, is not that, and there is nothing to rejoice here.

So, the first thing we see is a quote directly from Angular's fucking documentation, which looks something like this:

In Angular, “service” is a singleton created by a “factory of objects”. These object factories are functions that, in turn, are created by the “service provider”. Service providers are "constructor functions." When creation occurs, they must have the $ get property, in which the object factory function will be located.

And what the hell is this crap? For me, it sounds like "in order to write Hello world, you first need to create the Hello world service, which will create the Hello world factory to create the Hello world service to write Hello world on the screen."

Whatooooa What am I, someone's dissertation opened? This is completely incomprehensible.

No, you are not reading someone else's dissertation, you are really reading the angular documentation.

If this were a dissertation, she would probably try to describe a solution to a problem, instead of describing a re-elaborated solution to a fictional problem (this is not really 100% true, scientists also live in their own world, but almost true)

Below is a real example made up for this question.

I suspect that most of the scenarios for which Angular was invented are invented, because only this can explain the appearance of all these factories, proxies and services on the frond. The code and explanations that I will show you now are written by non-conscious lunatics, and it's hard to believe that this is all serious.

Service, Factory and Provider may be the same.

What? No, of course, they can, they are just functions that return value, but okay, let's climb deeper into this rabbit hole and see where it takes us.

We are given an example of "instantiating a machine", starting with the fact that

With the help of the service (singleton), you can not do it, because the service can not be instantiated.

So they explain to us why we need providers, because

To instantiate, you will need a Factory or Provider.

Not. Lord WHAT. WHAT THE HELL.

var car = new Car({ cylinders: 4 }) 


Damn new. We argued about it in the world of corporate backends so many times, and again seeing this bullshit in JS is just physically painful. All the same crap in a language that recently was my salvation from this disgusting shit.

The provider can be configured for your application.

Well, of course, we can configure providers if we need to configure our applications. How else to configure our applications and to achieve their configurability for our corporate configurable applications?

The code that is given later, I just adore. He is practically already a parody of himself, I don’t even need to comment on anything, it's just a grimace.

 app.service('CarService', function () { this.dealer = "Bad"; this.numCylinder = 4; }); app.factory('CarFactory', function () { return function (numCylinder) { this.dealer = "Bad"; this.numCylinder = numCylinder }; }); app.provider('CarProvider', function () { this.dealerName = 'Bad'; this.$get = function () { return function (numCylinder) { this.numCylinder = numCylinder; this.dealer = this.dealerName; } }; this.setDealerName = function (str) { this.dealerName = str; } }); 


To configure the dealer, you just need to

 app.config(function (CarProviderProvider) { CarProviderProvider.setDealerName('Good'); }); 


Hey, this is just a configuration, do not do anything with this code !!!

I thought of writing the equivalent in simple JS, but then I began to understand abstractions, and I no longer want to program, but I want to get under the table and beat my head on the floor until the brains begin to flow. Anything else, if only he did not have to deal with this madness anymore.

But why CarProviderProvider, and not CarProvider?

This is my advice. If you ask such a question. If you ask a question that needs such an answer, to which this question fits, YOU EVERYTHING DID IT WRONG.

There is nothing shameful about the fact that you did everything wrong, this is normal - we all make mistakes, but judging by the development of events, no one understands what is actually happening. And soon we will hire Angular Consultants and send our students to expensive Angular Training Courts for many, many years. Congratulations - got caught.

People, what's wrong with you?

We had a cool thing, and you broke it. We had where to run from the disgusting corporate mutual support, but instead of learning how to program a pancake, you simply moved your providers of factories to a place where you could do something relatively quickly quite recently.

Go to hell, I'm leaving

Okay, everything is OK, I no longer do corporate software. I showed this question with the answer to my colleagues, we all laughed at you perfectly, because such a level of stupidity is indescribably hilarious. But you know what? When you finish digging this hole for yourself, and understand what a bottomless hole it is, I will stand above you and laugh, because ultimately you are to blame for everything.

Begin to think for yourself, throw this crap before it's too late and learn how to write at least some normal code. All that you need is already described, you will succeed. And if you need to be held by the hand with all these factories and providers and service provider factories, then understand right away that the world does not need your shit code. Go and find a job that you really succeed in. Stop breaking our javascript.

Source: https://habr.com/ru/post/220631/


All Articles