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! While you're at it, check out some resources Treehouse students have shared here.
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 trialRyan Giglio
3,996 PointsWhy make randomColor() an instance method instead of a static method?
It seems like it would be easier to just call ColorWheel.randomColor() rather than having to instantiate a colorWheel object first and then calling the .randomColor() method. I assume it's some kind of best practice thing but I'm not quite sure why an instance is better.
2 Answers
Pasan Premaratne
Treehouse TeacherRyan,
Static method is certainly a better solution here. Our courses assume zero programming experience however so we introduce things slowly and iteratively over multiple courses.
Just a matter of easing our student base in.
Ryan Giglio
3,996 PointsThat makes perfect sense. The "start from zero" approach has been very helpful even with my programming background, since it's been a few years since I fully flexed those muscles.
Often in programming there are things that seem more convenient at first, but that convenience comes with a hidden cost that you don't discover until later. Global variables come to mind. I wasn't sure if this was that sort of thing - to me a static method was the obvious solution, but I don't know enough about iOS development to know if there's a good reason not to do it that way, or if this approach would be more beneficial in future examples that are much more complex.
Actually, as I am responding I thought of maybe an example of that - say you're fetching your array of random colors from a server, rather than hard-coding it in the class. In that case, I assume you would fetch the data from the server in the init() method, which wouldn't be run unless you create an instance of the model first. Is that correct?
Pasan Premaratne
Treehouse TeacherIn that scenario, you could pass in the data from the server to a custom initializer, init(colorData: [Colors])
, for example. In Swift, initialization is a tricky topic and there are some very strict rules (as compared to ObjC). An instance cannot be created without all stored properties being assigned initial values in the init method. The only exception to this is unless the properties are marked as optionals, making them default to nil.
Pasan Premaratne
Treehouse Teacherlol at "wheres our answers". It's not even been a day yet give me some time to answer :p
Answered over at the other post: https://teamtreehouse.com/forum/why-store-the-random-color-in-a-variable
Justin Black
24,793 PointsJustin Black
24,793 PointsI went through this today, I didn't think of this one.. Though I did have this question:
https://teamtreehouse.com/forum/why-store-the-random-color-in-a-variable
Wheres our answers Pasan Premaratne