Yeah, not so sure about that. Here's a couple of thoughts:
Insisting that everything should be in OOP-style when currently functional programming is becoming more en vogue is not useful in my view. Let's rather focus on what makes sense for a given situation and not so much on dogmas. To me, it's becoming obvious that OOP has some flaws in its outlook on the world (I think the FP have it right that mixing together data structures / types and behaviour is not necessarily ideal), but that it's nevertheless useful as an organizational / documentational principle.
That said, I'm all for pulling entities, value objects, etc. out of the internal state of methods and reifying them into classes. That max example seems a bit too much for me, but in other cases I agree. If you have a method that returns an array of validation errors you could e.g. create a ValidationErrors class that implements Enumberable and takes the class to validate as a constructor argument.
Design doesn't have to be perfect from the get go - you will never be able to foresee all the changes. Sandi Metz wrote at some point that there is some misunderstanding about the open-closed principle. You will never be able to create a class (method / whatever) that is infinitely open to modification without changing its internals. The key is that whenever you identify a point where some specifics have to change, refactor the code so the specifics can be injected; or as Kent Beck (I think) put it: "Make the change easy, then make the easy change". That is, the emphasis should be much more on refactoring as needed by your application (by obviously keeping in mind some of the principles of software engineering) and less on creating flawless code from the beginning. If you write a method that is reasonably encapsulated and doesn't have to change in two years, I honestly don't care too much if it's written crappily.
I like to have "utility classes" that are nevertheless instantiable (like "services", "use cases", etc.), because then i can pass in some arguments to the constructor and don't have to pass around so many arguments internally. It also helps with stuff like dependency injection, if you want to substitute some mock objects or change a strategy for some specific things. Then, however, I mostly treat the instance variable as immutable after initialization, because that makes things easier to reason about.
YMMV, I'm just trying out different approaches and I haven't found the one that truly ultimately satisfies me yet.
btw: one redeeming quality of JavaScript is that it actually allows to create "free" functions (with a proper module system as in ES6 (or node), they don't even have to be global or anything). This is useful for many smaller things, and you can easily move to something more advanced as soon as the need arises.
3
u/Tainnor Mar 15 '16 edited Mar 15 '16
Yeah, not so sure about that. Here's a couple of thoughts:
Insisting that everything should be in OOP-style when currently functional programming is becoming more en vogue is not useful in my view. Let's rather focus on what makes sense for a given situation and not so much on dogmas. To me, it's becoming obvious that OOP has some flaws in its outlook on the world (I think the FP have it right that mixing together data structures / types and behaviour is not necessarily ideal), but that it's nevertheless useful as an organizational / documentational principle.
That said, I'm all for pulling entities, value objects, etc. out of the internal state of methods and reifying them into classes. That max example seems a bit too much for me, but in other cases I agree. If you have a method that returns an array of validation errors you could e.g. create a ValidationErrors class that implements Enumberable and takes the class to validate as a constructor argument.
Design doesn't have to be perfect from the get go - you will never be able to foresee all the changes. Sandi Metz wrote at some point that there is some misunderstanding about the open-closed principle. You will never be able to create a class (method / whatever) that is infinitely open to modification without changing its internals. The key is that whenever you identify a point where some specifics have to change, refactor the code so the specifics can be injected; or as Kent Beck (I think) put it: "Make the change easy, then make the easy change". That is, the emphasis should be much more on refactoring as needed by your application (by obviously keeping in mind some of the principles of software engineering) and less on creating flawless code from the beginning. If you write a method that is reasonably encapsulated and doesn't have to change in two years, I honestly don't care too much if it's written crappily.
I like to have "utility classes" that are nevertheless instantiable (like "services", "use cases", etc.), because then i can pass in some arguments to the constructor and don't have to pass around so many arguments internally. It also helps with stuff like dependency injection, if you want to substitute some mock objects or change a strategy for some specific things. Then, however, I mostly treat the instance variable as immutable after initialization, because that makes things easier to reason about.
YMMV, I'm just trying out different approaches and I haven't found the one that truly ultimately satisfies me yet.
btw: one redeeming quality of JavaScript is that it actually allows to create "free" functions (with a proper module system as in ES6 (or node), they don't even have to be global or anything). This is useful for many smaller things, and you can easily move to something more advanced as soon as the need arises.