Ban instanceof
and avoid inheritance, specifically in TypePattern
. #33
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
In
TypePatterns.js
, there is a base classTypePattern
and a couple of classes likeMatchTypePattern
that extend this base class. In general, I try to prevent using class inheritance when it's not really needed. Would it be possible in this case to remove the inheritance, and instead use TypeScript here and defineTypePattern
as an interface instead?Well, I agree that at the moment inheritance is only used for the equality test. Is that not enough reason for using a class hierarchy? (I'll be frank that personally I would use a class hierarchy merely for the sake of having an
instanceof
test for all of the derived classes, if it were convenient, so it's hard for me to judge where the line is for when inheritance is "needed".)If your judgment is that we should not use class inheritance here, then I will just make all the TypePatterns be entities that happen to have the same interface. In that case, is it OK if I just make each one its own non-derived class, or do you prefer I not use classes at all, but rather plain objects? Thanks for letting me know as soon as you have a chance so I can make the changes needed before going on to benchmarking.
I like the famous "prefer composition over inheritance"😄. I'm totally fine with using classes by themselves, but in my experience it helps to keep complexity managable by using inheritance as little as possible.
In general, I also try to separate data and logic as much as possible, and often the data is then just JSON which is easy to inspect, alter, serialize, etc. And the logic can often be plain JavaScript functions. That way there is not that much need for classes.
One remark regarding
instanceof
: in mathjs we do not rely oninstanceof
because that turned out to give issues in the past, see https://github.com/josdejong/mathjs/issues/345.Wow, OK, I didn't realize that
instanceof`` was considered broken in JavaScript (I guess because if I create an object in one frame/window and it is somehow passed to another frame/window it is not an
instanceof`` the versions of the classes in that other window? Is that the concerning issue?)I will change TypePatterns to a collection of classes with the same interface and remove instanceof.
Changeto BanTypePattern
into an interface?instanceof
and avoid inheritance, specifically inTypePattern
?Banto Baninstanceof
and avoid inheritance, specifically inTypePattern
?instanceof
and avoid inheritance, specifically inTypePattern
.Yes indeed,
instanceof
doesn't reliably work between iframes and the main window.