3 ms·
Typescript works well without classes because you have great type system, you can implement an interface on the fly by just passing an object with the correct p
by fetbaffe 7y ago
Typescript works well without classes because you have great type system, you can implement an interface on the fly by just passing an object with the correct properties to fulfill it.
But the class based architecture doesn't necessary have to be as bad as described in the article.
Inheritance is somewhat of the old school of OOP, modern OOP rarely use inheritance, it uses composition, just like a functional code base. In my projects my classes are always marked as final. Can't even remember the last time I inherited a another class, maybe if I interacted with some legacy code.
It is important to separate your classes, what classes can have state & what classes can not have state. Classes with state should be small & classes without can be large, because stateless classes can easily be reused.
Classes have the main advantage that you easily inject dependencies in the constructor (composition), e.g. a repository depending on the database driver.
You can do that with functions as well, e.g currying, but I find it a bit more messy if your language is not specialized against that use case & with a class you "curry" all class methods at once. If you don't inject your depencies in your functions you will have a much harder time to reuse that function & test it.
I agree with that class based GUI code rarely works, this is because GUI (if it is HTML or OpenGL) does not translate well to a class with hierarchies. GUI code have tendency to more be procedural.