4 ms·
That OO refactor isn’t actual OO. The tell tale sign is that it is named by what it does rather than what it is (verb vs noun) and the -or ending in the name [0
by vips7L 2y ago
That OO refactor isn’t actual OO. The tell tale sign is that it is named by what it does rather than what it is (verb vs noun) and the -or ending in the name [0]. It’s just a function masquerading as a class.
The better refactor to introduce OO concepts would have been to introduce an isAdult function on the user class and maybe a formatted function. This + the functional refactor probably would have made for the best code.
return users.filter(u => u.isAdult())
.map(u => format(u)); // maybe u.formatted()
[0] https://www.yegor256.com/2015/03/09/objects-end-with-er.html https://www.yegor256.com/2015/03/09/objects-end-with-er.html
- dominicrose 2y agoWhat about a pure function that can take anything that has an age as input? Well obviously that wouldn't work for a cat but it's just an example. It requires typescript and I'm not sure how to name the file it would go in, but I think it's interesting to consider this duck-typing style. function isAdult({age}: {age: int}) { return age >= 18 } ps: I replaced const by function because I don't like the IDE saying I can't use something before it is defined. It's not a bug it's an early feature of javascript to be able to use a function before it is defined. Code is just easier to read when putting the caller above the callee.
- vips7L 2y agoThat wouldn’t be object oriented. In OO you tend to want to ask an object about itself, Yegor talks a bit about this in his book Elegant Objects. What you are proposing is just functions or data-oriented programming; which is fine if that’s your thing, but I’d be weary because of the reasons you outline above. Can a book be an adult? What about a tv show? Or recipe from the 9th century? isAdult really only applies to users and really belongs on that object.
- Frieren 2y ago> u.isAdult() Being adult is not a property of the user but of the jurisdiction that the user is in. In some places or some purposes it is 18 but it could be, e.g., 21 for other purposes. If you software is not going to just run on the USA it is not a good idea to implement isAdult in the user but in a separated entity that contains data about purpose and location.
- vips7L 2y agoWith proper OO you could still implement that on the user object. boolean isAdult() { return this.age >= this.location.ageOfAdulthood(); // or this.location.isAdult(this.age); pick your poison! } …anyway it’s just an example of how to introduce OO concepts. As everything in programming it depends
- Terr_ 2y agoJust having some fun with bikeshedding here: Yeah, that could work but IMO in a big/international system the responsibility should ideally live elsewhere, since: * You may need to determine adulthood for a different jurisdiction than where the person currently resides. Their citizenship may be elsewhere, or you may be running a report that expects "adulthood" to be by some other region's standards, etc. * Sometimes the underlying kind of adult-need wanted is slightly different, like for consuming alcohol or voting. * There may be a weird country or province has laws that need additional factors, like some odd place where it's a different age-cutoff for men and women.
- vips7L 2y agoYes this is just extreme bike-shedding at this point. But none of this is impossible with more OO principles, like interfaces: class User { // Convenience function to check if the user is an adult in their current location boolean isAdult() { return this.location.isAdult(this); } boolean isOfDrinkingAge() { return this.location.isOfDrinkingAge(this); } } interface Location { boolean isAdult(User u); boolean isOfDrinkingAge(User u); } class WeirdLawsLocation implements Location { boolean isAdult(User u) { return switch (u.gender()) { case MALE -> u.age() >= 16; case FEMALE -> u.age() >= 18; } } boolean isOfDrinkingAge(User u) { return u.age() >= 21 } } In the hypothetical that you want to check somewhere the user is not currently: class SwedenLocation implements Location { boolean isAdult(User u) { return u.age() >= 18; } boolean isOfDrinkinAge(User u) { return u.age() >= 18; } } var sweden = new SwedenLocation(); sweden.isOfDrinkingAge(user);