Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.Start your free trial
Aaron West256 Points
When would I define a function inside of a class vs without or outside of a class?
My understanding of classes and functions are still a bit foggy. Can someone please explain to me, if I can define a function without having defined a class, then what benefit do classes bring to my code. And in what scenario would I have a function defined without a class vs having the function defined within a class?
Emil Rais26,831 Points
One of the reasons we use classes is that it allows us to give some structure to our code.
If you have a couple of unrelated functions, the benefit you will see from adding them to a class is small as cohesion of such a class will be low. Say you had a function that could send emails to your users and another function that could look up orders in your database. Exposing those two functions through a utility class would only help you in terms of discoverability, so you might as well define the two functions separately in their own right.
However if the functions share a domain there will be more benefits to it. Say you have a couple of functions that interact with your database's user table. You could have a function that lets you look up all users, let you look up users by id, add new users and update existing users. If you add all of these functions to a particular class cohesion will be high as they are very related. Opportunities for reusability will be easier to exploit. The functions will all need access to the same database and so the class can provide a common mechanism for that.
The method signatures can also be made simpler this way. When the functions exist in their own right, they need to specify parameters for all the objects that they will use. A
lookUpUserById function would need a database accessor and an id for instance. When a function is defined by a class the class can now be used to provide access for some of these parameters, which is very useful when they're not subject to change.
lookUpUserById will always need the database accessor but the id could vary from invocation to invocation. Here it would be typical to instead define the database accessor as a field on the class and instantiate it in the class' constructor. Then the method would only need to accept the single id parameter making it simpler and more intuitive to use.
I would recommend that you consider using classes regularly. Not because it will necessarily provide a benefit to your existing code but because it will help you learn about classes and eventually the many benefits will become apparent to you.