I always try to use the simplest abstraction that solves the problem. Oftentimes that's just a function.
Functions are also way more reusable than classes. Classes by design tie state to methods, oftentimes grouped with state I don't care about or have. If instead they used functions I could just reuse the functions I care about with the state I have.
It's not true to say the functions are more reusable than classes. These are different entities serving different purposes. A function is a set of instructions, a class is a type of data.
Similarly, we don't say that an integer is more reusable than a square root function.
Classes represent types, they could be simple types, like a complex number or vector, which represent data, or they can be a service type that encapsulate global state, bind to the OS. In my language I was separating these two concepts to be more pure.
Hello! My name is Thomas and I'm a nerd. I like tech and gadgets and speculative fiction, and playing around with programming. It's not my day job, but I'm working on making it a side gig :)
If I am allowed to be a bit pedantic, a class is not just a type. A class is a bundling of code/instructions and data. If all you have is some data and no code, then it should be something like a struct or tuple or maybe even a container type such as list or hash depending on your language and use-case.
Any type is a reference to a value and operations on that value. A class is one way of expressing a type. Types are defined by the operations that can be performed on them, not just their raw value -- if one can even define a value absent of any operations on it.
The part I dislike is that classes can represent a bunch of things that don't work like value types as well. I wish these were distinct entities. I called them "service" in the language I was working on.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I always try to use the simplest abstraction that solves the problem. Oftentimes that's just a function.
Functions are also way more reusable than classes. Classes by design tie state to methods, oftentimes grouped with state I don't care about or have. If instead they used functions I could just reuse the functions I care about with the state I have.
It's not true to say the functions are more reusable than classes. These are different entities serving different purposes. A function is a set of instructions, a class is a type of data.
Similarly, we don't say that an integer is more reusable than a square root function.
Classes represent types, they could be simple types, like a complex number or vector, which represent data, or they can be a service type that encapsulate global state, bind to the OS. In my language I was separating these two concepts to be more pure.
If I am allowed to be a bit pedantic, a class is not just a type. A class is a bundling of code/instructions and data. If all you have is some data and no code, then it should be something like a struct or tuple or maybe even a container type such as list or hash depending on your language and use-case.
I can also be pedantic and say it is a type. :)
Any type is a reference to a value and operations on that value. A class is one way of expressing a type. Types are defined by the operations that can be performed on them, not just their raw value -- if one can even define a value absent of any operations on it.
The part I dislike is that classes can represent a bunch of things that don't work like value types as well. I wish these were distinct entities. I called them "service" in the language I was working on.