3 ms·
You can write these same methods in an OOP language like Java. You dont have to use classes for everything. But alot of times, yes it makes sense to group a se
by andoando 1y ago
You can write these same methods in an OOP language like Java. You dont have to use classes for everything.
But alot of times, yes it makes sense to group a set of related methods and states.
You say this is not a natural way of thinking but I strongly disagree, it lines up perfectly with how I think. You are you, the car dealership is a dealership. You buy a car from the car dealership, the dealership gets money, you lose money, the dealership loses a car and you gain a car. I want these states reflected in the objects they belong and not passed around globally and tracked
Or if I am writing an API library, yes I very much want to 1. group all my calls together in a class, and 2. keep track of some state, like auth tokens, expirations, configuration for the http client, etc. So you can just do api.login, api.likeX, etc
Moreover, most methods youd write in a large project are so limited in scope to the type and purpose, this idea of some great modularity is nonsense. Its not as if you can have a single delete function that works on deleting users, images from your s3, etc. Youd end up writing bunch of functions like deleteUser(user), createUser(user), deleteImage(image), and wow wouldn't it be great if we could just group these functions together and just do user.delete, user.create? we could even define an interface like Crudable and implement it differently based on what we're deleting. wows
- ninetyninenine 1y agoOk simple question. Have a method called increment(n) on an object called tracker. It’s used to increment tracking id values. I want to reuse the logic of increment(n) inside another object called childHeight to increment height values. Can I import the method and reuse it inside another object? No. I can’t. But if increment was a pure function thats like this increment(c, n) then I can move it anywhere. That is what I mean by modularity. For oop lack of modularity is fundamental to its design. The grouping of methods around mutating state breaks modularity. You’re talking about a semantic issue. Grouping methods by meaning. I’m talking about a logistical issue where the grouping by semantics cannot be broken even though the logic is the same. Incrementing height and incrementing ids are identical in logic even though the semantics are divergent. That is the problem with OOP.
- andoando 1y agoThere are multiple ways to handle that. Class Number { static Int inc(Int n) { return n+1 } } import static Number.inc Class Tracker { Integer height = 0; incHeight() { this.height = inc(this.height) } } class Tracker extends Numbers { Integer height = 0; incHeight() { this.height = this.inc(this.height) } } Or interface Number { default Int inc(Int n) { return n+1 } } class Tracker implements Numbers { Integer height = 0; incHeight() { this.height = this.inc(this.height) } } Or my pick class Number { //or extend the Integer class and add your methods int num = 0; inc() { this.num +=1 } } class Tracker { Number trackingId; } class Building { Number height; } t = Tracker() t.height.inc() b = Building() b.height.inc() All OOP does is really give you a way to group state and methods, how you use it is up to you. There is a reason almost all big software is written this way and not in Lisp.
- ninetyninenine 1y agoDo you see the problem? What you wrote is the problem with oop. Gotta tear your whole program apart and rewrite it. You had to create new things and rewrite your methods. With functional you don’t rewrite. You recompose what you already have. It means oop is not modular. You didn’t create modules that can be reused. Nothing could be reused so you had to reconfigure everything.
- andoando 1y agoTake a look at the Interable interfae in Java if you want some good examples of reusable functions. It enables map, filter, stream, reduce, groupby, etc on lists, sets, etc or collections class which gives sort, min, max, replaceAll, etc