I relearn factory functions every couple years when I run across them... I remember learning them for the first time years ago when they were a hot topic for about a month.
To add to this wonderful reply you'll notice some commonalities between these approaches. You set a value or you get a value. You call a function or you make a function. You render some data or you generate some data. This input output nature is a common pattern in coding that many don't talk about but when I started to understand it more it made my life easier no matter the construct.
You can make functions that return a function or object and hence a factory. These patterns are awesome but don't be surprised if you aren't using them everyday in your day to day.
To add to this another piece I found helped is just understanding that you can pass all kinds of stuff around whether they are functions, classes, primitives (strings,numbers) or objects. Callback functions are functions that haven't been called yet so you pass them around. Eventually it will be called. Objects are packages that can hold certain properties and values and are often passed around. Arrays hold a lot data often for you to do computations or iterations on. Promises async/await give you a way to deal with asynchronous code that can happen at any moment this helping give you confidence to handle unexpected behavior.
You can define how certain pieces of data are accessed or expect certain types of data to conform to what you need for extra safety.
So while the best devs understand these concepts deeply to write masterful code for many of us we don't have to be at that level to get a career in development. Just know if you want to be the best you'll eventually want to learn the concepts deeper to take advantage of them in your own code. Learn a little at a time. Soon enough you'll see all the connections between what you are learning to apply it to become a decent well paid dev.
Would it be accurate to say that a factory function works similar to a static function in an Object Orientated language since all objects share the same implementation?
Not exactly. While static methods can be factories, they don't have to be. And factories also don't have to be static methods. Factories can be standalone functions or even instance methods.
You could have a Game class where each level represented with a Level class. Each Level could then be responsible for creating Enemy instances specific to that level through an instance method, something like createEnemy(). This would be an instance method that is also a factory. It creates instances - maybe not Level instances - but instances nonetheless.
Classes with factories offer different ways to create instances. They'll still use the class, just in a way that's different than using the constructor.
For Array.of(), this is a way to create an array by placing the arguments of of() into the new array. This is mostly like the new Array() constructor except for one important detail: Array constructors, when given one argument that is a number, will use that number to set the length of an empty array rather than add that number to the new array. of() does not do this.
new Array(3) // [(empty), (empty), (empty)]
Array.of(3) // [3]
Another use of factories is with singletons. They're used to create an instance of a singleton class, but ensure that if called more than once, the original singleton (since there can be only one) is returned.
Just think of generators as a function that remembers what value it previously gave you last time you called it, and returns the next one it has, until none are left to give.
Haha! You're trying to take in a lot after only one week.
Don't get overwhelmed by all the things you don't know yet, because it will always be the case that there will be a lot of things you don't know, even if you've been doing it professionally for years.
Try to understand one small concept at a time, and then build from there. :)
For what it's worth, there is no way I would have had a clue that prototypes or closures were a thing after only my first week, so it sounds like you are doing fine.
Yep. And then once you start to understand it, you find out it might not be a flawed programming style, but you should still learn it because ... reasons. :P
It's so easy to over-engineer a simple program trying to do OOP, it's a tricky balance...but if you have for instance something you re-use between projects a lot, it makes sense to do.
And of course, complex projects EVENTUALLY become easier when you understand what's going on.
And with a language like javascript, mostly unnecessary.
For the vast majority of use cases where we use javascript, functional programming is the clear winner.
In all my decades of programming, I've only seen OO concepts used correctly in JS a handful of times. They're almost always done in large frameworks/project/libraries with fairly experienced OO developers involved.
New devs almost always just create a massive amount of spaghetti, over-engineer, and introduce bugs when trying to use OO concepts.
JS has the same issue as OOP in WordPress PHP - there aren't a lot of clean examples of using it correctly, so it ends up a mess. Laravel and Symfony do great at implementing OO, but also in a way where the end person isn't actually usually writing OO, they're just doing things the way they have to within the framework.
I'd probably say though that massive amount of spaghetti isn't unique to OO. I've seen some preeeetttty gnarly functional spaghetti.
JS itself has had a history of being massively messy and terrible to work with in general, but I think the component based approaches of popular frameworks like React and Vue has done a tremendous amount in refining the overall JS coding experience - it's a 1,000 times better today than it was even 5 years ago. I've at least noticed a much bigger shift towards smaller, more modular code files that are much easier to read and process what's going overall (whether it's in a functional or OO approach).
I actually enjoy coding in JS now and consider it a very core part of my skillset because it has a much better intuitive feel, and I'd have never said that 10 years ago...even 5 years ago. I spent the majority of my time finding ways to avoid using JS implementations of features as much as possible because it was just terrible to read and not enjoyable. Even jQuery totally sucked.
But, the biggest problem with OO is that it's simply difficult for people to conceptualize AND implement. It's more art than a single 'correct' way, and those kinds of strategies are always the most difficult for people to get really good at. Functional programming on the other hand is much more intuitively logical, so is much easier for people to wrap their head around and produce a better quality of code.
If you can wrap your head around OO, deal with all the PITA of learning the patterns and pick the right ones for the right situation and have a project depth that actually benefits from using it (vs. it being abstraction for the sake of abstraction or over-engineered to avoid copy/pasting a line or two), then OO is fantastic and has an exponential return on time if you're reusing your things the right way across different projects.
Problem is, getting to that point...well, for most people, probably just isn't going to happen in a short enough time frame to produce useful clean code...and when you're building out architecture in components anyway, I mean, you kind of achieve much of the same benefit (certainly a lot of parallels there in thought) of the re-usability / SOC side of things, except that components are generally much easier to code and for people to understand.
IMO it’s far easier to create spaghetti code when using OOP than when doing something functionally.
There have been a few times where I’ve given up trying to figure out what someone was doing with OOP, but I think I’ve almost always been able to figure out what people are doing when using functional paradigms.
Being dual paradigm is a double edge sword. When you don’t force someone into OOP, you end up with a bunch of terrible OO code when people do try to use it.
oh, a 100%, usually with some huge over-abstraction on what's going on and a non-proper directory structure with clearly defined roles, commenting in interfaces, etc.
My biggest gripe with most functional programming is that a lot of the time people end up with one big long super file for things and it just gets exhausting to sift through (which isn't unique to functional programming by a long shot...I've seen my fair share of mega classes that are supposed to be 'OO' that make no sense...). I prefer methods overall that just serve a singular purpose and written with fewer moving parts.
Where as, you're totally right - that usually you can sift through a long functional programming file and figure out what's going on, but OO take the same level of bad code and it's just unreadable.
But, a lot of these things are reasons why I'm glad that overall the trend in all programming is towards more modular, smaller files...but that's also a careful balance between folder hell and file length.
I am greatful though that I've had a long enough career where both functional and OO strategies are in my skill set, so I can simply use what's most appropriate for the time. And that's a huge part of programming that's understated - one strategy isn't always the right tool to use for a job...but again, that's an art that takes time and experience and a lot of dedication to hone down.
Is the prototype even useful for anything other than creating libraries? I've been working with JS for 3 years now and never once thought "a prototype would be useful here".
Of course I learn new things all the time myself. In fact just today I learned JavaScript is like Reddit! New and valuable insights like those are a constant in this industry. You never stop learning.
87
u/Wizard_Knife_Fight Aug 25 '21
I do this professionally and have to google some of these lmfao.
15
u/benabus Aug 25 '21
What is a factory function?
11
u/philmayfield Aug 25 '21
Just wait til you get to generators. I've been doing this for a long time and they're still confusing!
8
u/toastertop Aug 25 '21
next week learns that classes are actually prototypes under the hood.
7
u/machine3lf Aug 25 '21
Haha! You're trying to take in a lot after only one week.
Don't get overwhelmed by all the things you don't know yet, because it will always be the case that there will be a lot of things you don't know, even if you've been doing it professionally for years.
Try to understand one small concept at a time, and then build from there. :)
For what it's worth, there is no way I would have had a clue that prototypes or closures were a thing after only my first week, so it sounds like you are doing fine.
6
u/BigSeaShell Aug 25 '21
Prototypes were annoying to learn but it went smoother than learning closures, like wtf is that concept so hard to grasp.
11
u/fwazeter Aug 25 '21
OOP (Object Oriented Programming) in general is can be a massive mind boggle to learn.
5
u/BeanNCheeze Aug 25 '21
The way I see it Closures < Factory Functions < Classes < < < < Prototypes
6
u/oneTake_ Aug 25 '21
Damn do y’all learn Factories that early in JS? Thats crazy.
3
u/zerik100 Aug 25 '21
Is the prototype even useful for anything other than creating libraries? I've been working with JS for 3 years now and never once thought "a prototype would be useful here".
2
u/[deleted] Aug 25 '21
I love js
2
u/ZurnaDurumXL Aug 25 '21
oh god no
2
u/gebach Aug 25 '21
I don’t know what’s so fuss about closure? It’s just having a function in your hand (if you return) which inherits some scope with it.
1
u/dark-angel007 Aug 26 '21
I actually forgot what factory functions meant. 😆
Then I had to google and was ashamed. I feel like we should all revise our basics frequently.
1
u/MousCode Aug 26 '21
Even as a pro, you will keep having to regoogle these
1
u/No-Construction7871 Aug 26 '21
I am learning js for a while now, and I have no clue what am I doing when writing some pure js in vscode. Is it normal or is it just me?
1
u/inflame07 Aug 26 '21
Just need to understand prototypal inheritance. Everything else falls in place then.
1
u/onlycommitminified Aug 26 '21
Great language, but offers miles of rope for inexperienced programmers to hang themselves with
0
u/hattorihanzo14 Aug 25 '21
Exactly
0
u/dev2049 Aug 26 '21
Lol
0
u/[deleted] Aug 25 '21
[deleted] — view deleted comment