I am creating functions for small and independent tasks, which do not need a context (eg removing characters from string), and methods (and their classes) when it's related to a more complex object (eg an array of Block representing a map in a game) and it needs to use a specific context.
To me, methods are better than functions when you need to give more than 4 or 5 arguments. In this situation, it means you need to create a class to handle those arguments (often, there isn't only one function doing stuff with those 4-5 (or more) arguments).
Thank you for stopping by! I am a full-stack developer that combines the power of entrepreneurship and programming to make the lives of programmers easier.
I would create an abstract class using those arguments as attributes, if they are used in more than 1 function, to have all the data processing in the same place.
Usually, I choose a name related with how I play with those data and not regarding the most important argument.
Thank you for stopping by! I am a full-stack developer that combines the power of entrepreneurship and programming to make the lives of programmers easier.
It could, but if you can "group" methods playing with the sames arguments (but doing different things with those) it can be pretty efficient in reducing SLOC (physical lines of code) and code complexity (IMO, in some cases creating a function with more than 4-5 arguments is better than creating a class).
To me, the most interesting thing with a class is that it handle its resources on its own, all of them, in the same place.
Thank you for stopping by! I am a full-stack developer that combines the power of entrepreneurship and programming to make the lives of programmers easier.
Yup, that's why they were designed. Data with functionality. Just be careful not to group data with functionalities that don't actually deal with the data as whole but just parts of it. Or even worse, consider functionalities as objects and creating classes based on those. It can lead to so many headaches.
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.
Hi!
I am creating functions for small and independent tasks, which do not need a context (eg removing characters from string), and methods (and their classes) when it's related to a more complex object (eg an array of Block representing a map in a game) and it needs to use a specific context.
To me, methods are better than functions when you need to give more than 4 or 5 arguments. In this situation, it means you need to create a class to handle those arguments (often, there isn't only one function doing stuff with those 4-5 (or more) arguments).
Great answer.
I have a question. If the method has 4-5 arguments and all can be modified then in which class do you put it? The most important argument?
I would create an abstract class using those arguments as attributes, if they are used in more than 1 function, to have all the data processing in the same place.
Usually, I choose a name related with how I play with those data and not regarding the most important argument.
This would work most of the time but wouldn't that create too many classes if there are many such methods?
It could, but if you can "group" methods playing with the sames arguments (but doing different things with those) it can be pretty efficient in reducing SLOC (physical lines of code) and code complexity (IMO, in some cases creating a function with more than 4-5 arguments is better than creating a class).
To me, the most interesting thing with a class is that it handle its resources on its own, all of them, in the same place.
Yup, that's why they were designed. Data with functionality. Just be careful not to group data with functionalities that don't actually deal with the data as whole but just parts of it. Or even worse, consider functionalities as objects and creating classes based on those. It can lead to so many headaches.