Re:The use of `class` for things that should be simple free ...
by disgruntledphd2 on Thursday May 28, 2020 @04:29AM
(#23334877)
Traditionally, I don't really like classes, as I started with a mostly functional language (R).
However, when writing Python (for a poker simulator), I came to the conclusion that objects can actually be really useful for the following reasons: 1) managing state: this is the big one, if you need data with your functions, a class is an obvious way of doing it.
2) documentation: a class with methods is a higher-level structure than a bunch of functions and you are more likely not to forget the existence of a method on a class relative to a function somewhere in your code base (this second idea shamelessly stolen from Martin Fowler).
Re:The use of `class` for things that should be simple free ...
by Ayesh on Thursday May 28, 2020 @04:37AM
(#23334916)
For PHP, one reason why I decide to use a class because there is no autoloading support for functions.
This allows me to logically group functions _and_ throw in autoloading support.
For example recently, as an exercise, I made a little scraper in Python to extract movies data from a website [1]. This could have been done in procedural programming directly (actually the first iteration was like that), but I rewrote the whole thing as a class and I still don't know why. I think it's completely overkill.
If I was doing a scraper for multiple websites related to movies, that would make sense to use classes as they share the same goal and I could make good use of heritage/interfaces/abstract classes. But here, it's not. I read again and again about when to use OOP or not but I still struggle to find most of the times the good decision. It's like I doing it just because it looks "better".
[1]: https://github.com/kinoute/scraper-allocine