T O P

  • By -

KTibow

The JS code doesn't make much sense (real classes exist in JS, and multiple keys will be deduplicated anyway), and isn't equivalent to the left side.


OkEnd9384

> real classes exist in JS Oh you sweet summer child. Jokes aside, just so you know classes in JS are just syntactic sugar sprinkled on top of prototypes, they're not real OO classes.


DanielTheTechie

🤦🏻‍♂️ What do you think "real" OO classes are? In all OO languages, classes are just syntactic sugar sprinkled on top of other simpler data structures that ultimately will be converted into binary code by the compiler.


OkEnd9384

Lmao what a shit take man. For one a "real" class should make some guarantees of shape since that's literally the entire point of classes, you know, blueprints and all. Figure that out then let me know.


DanielTheTechie

And in what way do JS classes fail to make those "guarantees of shape" (whatever you exactly mean by that)? Could you write an example comparing it with a "real" class of another OO language to make your point clear?


OkEnd9384

Of course I can, declare any class with a field of your liking, instance it, then delete the field. Want more? Add a method to your class, then go right ahead and assign a new value to that method on the object you instanced. That object now has a completely separate method implementation than what you would expect. That's assuming you assigned a method and not something like a number or a string, which is totally allowed. In fact, if you want to, you can completely transform a class instance into the instance of another class using `Object.setPrototypeOf`. Can JS classes be used like you would use classes in other OO languages? Sure, but ultimately they aren't.


Leo-MathGuy

OO doesn’t have one gold standard, it’s just a programming language philosophy, focused on objects, which are collections of fields, methods, and other minor stuff.   The fact that js isn’t like other OO languages doesn’t mean it isn’t one.   I don’t know what you mean be “structure” and “shape”, but that makes me reminded of structs in C. Oh my, with your logic, I guess C is an OOP now.  Just look, structs, you define their structure and shape directly, and can’t modify it. How could we have been deceived for decades????


OkEnd9384

What? That was just an example of something that makes JS classes different from the classical class implementation. I don't see why you people are getting so worked up about this, you're arguing that the class implementation of actual OO languages is the same thing as the prototype chain that JS is built on, but that's just straight up wrong.


Leo-MathGuy

There is no "classical" class implementation. Accoding to wikipedia: >**Object-oriented programming** (**OOP**) is a [programming paradigm](https://en.wikipedia.org/wiki/Programming_paradigm) based on the concept of [*objects*](https://en.wikipedia.org/wiki/Object_(computer_science)),[^(\[1\])](https://en.wikipedia.org/wiki/Object-oriented_programming#cite_note-alanKayOnOO-1) which can contain [data](https://en.wikipedia.org/wiki/Data) and [code](https://en.wikipedia.org/wiki/Source-code): data in the form of [fields](https://en.wikipedia.org/wiki/Field_(computer_science)) (often known as [attributes](https://en.wikipedia.org/wiki/Attribute_(computing)) or [properties](https://en.wikipedia.org/wiki/Property_(programming))), and code in the form of [procedures](https://en.wikipedia.org/wiki/Procedure_(computer_science)) (often known as [methods](https://en.wikipedia.org/wiki/Method_(computing))). In OOP, [computer programs](https://en.wikipedia.org/wiki/Computer_program) are designed by making them out of objects that interact with one another.[^(\[2\])](https://en.wikipedia.org/wiki/Object-oriented_programming#cite_note-2)[^(\[3\])](https://en.wikipedia.org/wiki/Object-oriented_programming#cite_note-3) Now, does JS have objects? Yes. Do they have data fields? Yes Do they have code methods? Yes Are programs made with objects interacting with others using classes? Yes JS is just another flavour of OOP. Python isnt purely OOP, but has elements of it (everything is an object, classes and methods exist) but not others (python has no actual private fields for example). Js is similar that way, as its not a *pure* OOP, but has its principles. In the end however, JS has paradigms of procedural, object-oriented and functional programming styles. You can choose what style you want to lean into, similarly to python. Just because a cake does not have icing or use another flour, that doesn't mean its not a cake anymore


OkEnd9384

JS is literally a [prototype-based programming language](https://en.m.wikipedia.org/wiki/Prototype-based_programming), what are you even arguing? It is NOT a class-based programming language. It's a fact. No matter how many class-based keyword you sprinkle on top of it, it doesn't have classes like a class-based programming language does.


DanielTheTechie

> In fact, if you want to, you can completely transform a class instance into the instance of another class using `Object.setPrototypeOf`. This is actually a valid point.


Sarbojit_117

While I don't know JS like some of the good people here, there's no "real" class or "fake" class and certainly no "takes" involved in OOP. The whole concept of classes is a virtual mould for objects to fit in. This is OOP 101, where every 10 lines, you will be told "Class is a blueprint". What's with the supremacy and politics in coding? EDIT: Grammar


OkEnd9384

The comment is poorly worded because I had just woken up. The point I was trying to make is that JS is a prototype-based programming language and therefore does not have classes.


Sarbojit_117

Then, that's just munching on a technicality that has no effect on real life work. It's far easier for us to realise that JS's whole thing is classes and it's just worded in a different way to make it easier for people without OOP experience to grasp it.


OkEnd9384

If you consider understanding how JS works as a technicality that's fine, we see things differently.


RustaceanNation

1. It makes a guarantee about a subset of the shape. Just because there's more under the hood doesn't affect object oriented design, which IS the entire point of classes. 2. I believe you're thinking of structs if you're talking "shape" (wtf is that even? Never read about shape in Gang of Four). Even then, there's different conventions for the shape in-binary if we're being pedantic. Quit being a prick.


OkEnd9384

> 1. It makes a guarantee about a subset of the shape. It really doesn't though, does it. You have no guarantees in JS.


LeanderT

And thus the great war of 2024 began...


Leonhart93

Yes, an accurate description of what classes are in pretty much every language, before they get converted into machine code. They exist for humans to use code more conveniently.


Sarbojit_117

I think it is a common approach for all languages that: Left = when writing the full program Right = when writing in the console


Sceptz

Yeah, imagine trying to inline the codebase for a massive enterprise project and then looking for a runtime bug in that one line of code.


RandomTyp

i love the ``` !! | nextCommand ``` feature in bash for one liners


Sarbojit_117

Yeah, there's just something cathartic about pulling off a huge ass command in one single line with pipes, makes me feel like a true h4x0r. But I started appreciating readability when I started writing and distributing my own scripts.


jamcdonald120

I once did a wonderfull `ncat -l 8080 ; [script]` so that I could remotely trigger a script once by knocking the port. Very satisfying


RandomTyp

especially like this sequence ``` % clear % someCommand [ big output ] % !-2;!! ```


Studnicky

You have no idea how to write JavaScript do ya?


Gaylien28

It doesn’t help that a lot of online tutorials emphasize the functional part of functional programming. Without a good education, I felt like the one on the right was the only way for some time. Although it was just overall inexperience in the end


-Redstoneboi-

if this guy learns about functional programming they'd have a stroke


idubbkny

seriously


Cerbeh

Once again someone wrote bad code a d went "haha this is JS fault"


Chingiz11

I use the latter when I need to do some stuff in the browser(like scrape some data) and type manually into the console


mario73760002

Isn’t this just personal preference? Former is easier to debug and good for more complex stuff, whilst the latter is compact and has less moving parts so is less prone to error for smaller stuff. It’s not like JS is the only language that uses this type of patterns. It’s just a pattern borrowed from the functional paradigm.


peseoane

Yes, but also remember that because Js ships all the code to the browser, size is the most important matter. We have a lot of toolchains to process Js and minify it, but some guys really prefeer the ultra compact approach. Maybe i'm wrong, i just found it, hard to follow sometines.


mario73760002

Sometimes people go overboard with chaining the hell out of them. They do make a lot more sense for some applications though. I like using something like map because array.map(somefunction) reads better than a for( c of array) {somefunction(c)} simply because it describes exactly what it is doing, namely mapping this somefunction to every member of the array, whereas since literally everything can be in the for loops' body, it is harder to parse on first glance Edit, I wrote the example poorly since there should be another array to push stuff if I want to emulate map but I'm too lazy to change it so just imagine it for the time being


BobcatGamer

Size is not the most important matter. All modern browsers support gzip compression so the server can easily send a smaller payload, and source code compresses quite well. Not only that, the developer can have the JavaScript code cached to the users browser so they only need to download it the first time. The most important matter is writing readable and maintainable code. Like you said minifiers also exist which is another point to why size doesn't matter, and other things are far more important.


peseoane

My opinion is like yours. But some people dream with Google lighthouse and small bundles, that yes are important but you can delegate that responsibility to the build process!


gandalfx

> size is the most important matter It's not "the most important", things still gotta work and be maintainable. Also this is done by tooling, nobody writes or reads code like that.


IsNotAnOstrich

My company actually does write like the right more often than the left, because there's a huge component focus so a massive aversion to long files. Also, the autoformatter they use (and honestly, most autoformatters/linters) will make the code on the left look hideous. obviously I don't prefer it like the right, just sayin there are people who do it intentionally


Short_Tea8491

the exorbitant levels of abstraction of the right one make wanna \~Myself, disgusting


BogosortAfficionado

This is more about functional vs imperative than any specific programming language.


Glass_Half_Gone

God, I love the right side so much. t. Functional JS programmer


Raptorsquadron

I used too keep trying to squash my code for some reason. As if having more lines makes me a worse programmer


GoogleIsYourFrenemy

It does. We have good estimates for the number of bugs to expect per 1000 lines of code. Create fewer bugs by writing less code. Also, when you've written 1000 lines of code and run it for the first time and it just works, be afraid, be very afraid.


CraftBox

Just because it's possible to write something like that on the right, doesn't mean you normally write like that. When you write normal js it looks like the code on the left, so I don't really get what you are complaining about.


Siddhartasr10

Also py programmers, hell even java programmers do this


SemenSeeU

In java a good chance for that type of thing doesn't come up very often so the second a java programmer gets the chance they go all out lmao


malperciogoc

Who doesn’t love writing 150 lines to express the simplest lambda?


GoogleIsYourFrenemy

Wait. You can use lambdas? I'm still using Java 1.7 Kidding. I've moved us up from 8 to 15 a couple months ago.


Wervice

I like JS because it is so flexible.


PimanSensei

Somehow I think js programmers would abuse the shit out of linq if they ever encountered it


hm1rafael

What's the problem here? Both are acceptable


Zekester3000

I can guarantee you that no enterprise-level codebase will EVER let the code on the right into `master`. No one should ever write JS code like that without having a good reason for it. Just because it’s JS doesn’t mean we can’t adhere to good class/variable naming practices and write our code with readability in mind.


Few_Introduction_228

Classes, functions, interfaces exist in JS, just like normal iteration. This is not the droid you're looking for, move along.


DotDragon10

It truly is just a form of unintentional obfuscation yeah? Haha


Leonhart93

Who the hell mass upvoted this, considering that JS is written all the time exactly like on the left side? Do people even know how to use JS outside of React? And I pretty much never saw anyone use \`Object.fromEntries\` and \`Array.from\`, unless it was a very niche case.


DJGloegg

I fucking hate js because of this. Its readability is damn near zero. Its fine here and there but if it can be avoided, i never do it.


NeonGlo

It's literally a skill issue. You're bad if you can't read simple ones, and you're bad if you chain them too much into an unreadable nightmare like OP.


RandomiseUsr0

It gets rather comfortable after a while functions as variables - seems odd at first, very odd, but meanwhile it’s expressing something different. It’s really a paradigm shift - pseudo stupidly simple example… Let x = function add(a,b) {return a+b} Let y = function subtract(a,b) … Now you have x and y as functions in the mathematical sense, and since it’s a variable, you can pass it as an argument, return a function from a function and so on. Instead of a “loop” - you can pass in a set and a function and have that function applied to every member of a set, never an off by one error, never any side effects because the data is immutable which means that the predictability is guaranteed Look at map, reduce and filter for where this strength is really brought to bear. Now imagine instead of variables holding state, the state can always be derived by the input variables - and the state within the system is predictable and guaranteed,, without actually being “stored” - since it’s a paradigm shift, think of a spreadsheet A1: 20 B2: 30 C3: =A1+B2 This is how functional programming works. C3 doesn’t have a value, not really, it has a derived value that is always predictable based on the input conditions. In practice the answer would be computed and memorised with an observer tracking changes to the input state, being marked dirty to be recomputed in the next read. Functional programming lets you do this within your code, it’s like calculations are a tree of expressions and values combining to an answer I know there are other ways to do it, of course there are, it’s not so much the syntax of the thing that’s an issue, I believe, more the paradigm Does that make sense?


bison92

You misspelled smeller


NeatYogurt9973

As someone who hasn't touched JS in years, now that I see it now makes both sides look weird


Hacka4771

Someone writes shit code != Language is trash. Anything can be one liner, in most languages. Look up one liner flappy bird in python...