3 ms·
Good advice. Don't use classes. At least resist using them as much as possible. Consider this simple aspect: one of the key differences between a class method
by keymone 7y ago
Good advice. Don't use classes. At least resist using them as much as possible.
Consider this simple aspect: one of the key differences between a class method and a separate funcion is implicit (and sometimes explicit, though that's not much better) self/this parameter that allows access to all object fields. Essentially you're opting in to grow the input of every method call by all the data held by an object. That's a mistake on many levels:
- it is one of the most important "best practice" advice to only pass the data that function needs to do the job, with classes you're breaking that advice immediately
- ease of access to additional data doesn't leave room to think if it's actually good idea to do so, factor in laziness and inexperienced contributors and you get methods that grow in complexity much more than they should
- testing anything becomes torture, as unlike with simple function class methods now depend not only on incoming arguments but also on state of the object itself, i've seen tests with dozens of lines of setup for single line of test
Prefer simple pure functions, prefer builtin data types, prefer immutability, only pass arguments that function needs to do the job.
- Udik 7y ago> only pass arguments that function needs to do the job. Why would users of that function bother to know what data it needs to do its job? It seems you'll end up polluting your whole code with the implementation details of one of its components. What happens of you want to change those implementation details?
- keymone 7y agoBecause the two alternatives we’re talking about here are ‘foo(bar, baz)’ where foo, bar and baz convey meaning and ‘object.doWork()’ which means nothing and explains nothing. Yes, it’s a spectrum and there’s a lot of space in between those alternatives, I simply believe being closer to the first option is better than to the second. Pure functions are conductive to transparency, objects, methods and class hierarchies are conductive to obscurity.