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 trial
Joe Dayvie
11,956 PointsMultiple aspects for the same result with coding?
Hello,
I have read that there are usually multiple ways to reach the same result when it comes to coding but is that actually true, or is there usually a reason to prefer one over the other?
I have asked myself this question with a variety of topics; however, the most recent is under the iOS Swift Track.
When explaining Computed Properties, Amit mentioned that we could either create a method to return the surface area or use a computed property.
In this example, does it matter which one is used? I would think they result in the same way but alas, I am newer to coding so I wanted to ask others who actually know :P
PS: Side note question - When you were starting out, did you have difficulty putting a certain syntax, method, functions, etc. into a real world solution? I overall seem to have difficulty knowing how I would actually use but maybe that will become clearer as I complete this track?
Thank you!
Joe
3 Answers

krilnon
1,458 PointsI have read that there are usually multiple ways to reach the same result when it comes to coding but is that actually true, or is there usually a reason to prefer one over the other?
Yes, that has been true in my experience. There are always arguments someone can make about preferring one approach over another. Often those arguments are subjective and depend a lot on context.
When explaining Computed Properties, Amit mentioned that we could either create a method to return the surface area or use a computed property.
In this example, does it matter which one is used? I would think they result in the same way but alas, I am newer to coding so I wanted to ask others who actually know :P
The example you mentioned is so simple that it doesn't matter. There are lots of small differences and concerns that come up in real programs. Methods can accept arguments, so if area
depended on my favorite color and that wasn't already information stored within my shape, an argument would make sense: square.area(favoriteColor: Color.Red)
. That wouldn't be possible with a computed property.
As it stands, though, area
doesn't depend on any arguments, so you're left with the superficial calling syntax difference between square.area
and square.area()
.
Side note question - When you were starting out, did you have difficulty putting a certain syntax, method, functions, etc. into a real world solution? I overall seem to have difficulty knowing how I would actually use but maybe that will become clearer as I complete this track?
Yeah, things' utilities will become clearer as you have more opportunities to use them. I sometimes have the problem that I know how to do something without using new knowledge, so it's unclear if the new stuff is helpful at all. It's kind of like getting a rice cooker or electric kettle. Maybe I only drink tea, but then it also comes in handy when coffee drinkers visit. Or apparently you can use rice cookers to make huge, tall pancakes.

Jayden Spring
8,625 PointsHi Joe,
Thought I would drop my two cents again. I agree with krilnon as for the computed properties to an extent, however as far as performance goes it is more efficient to go with a computed property than using a separate method - but it really DOES NOT increase the overhead of the application in this case as it is such a small operation.
There are always going to be multiple ways to do something, when you are learning I think it is better to create something step by step so you know what you are doing. Then once you have reached your desired outcome, you can refactor it so that you are using fewer calls to methods or fewer if statements etc. As an example, im working on a separate C++ based cross platform application at the moment and I have been for the past few days creating a class which quite simply will layout some image elements on the screen in the same format with native controls depending on the device. So as you can imagine at the start there were a lot of if's for's and while's - however once I had it all working properly with the desired outcome I read through it and made it as efficient as I possibly could.
I speak about efficiency, but I don't think this is something to worry about until you are doing something UI or data intensive. A good example of efficiency is when you get to the part of the course that introduces you to GCD. If you were to do all the data processing tasks on the foreground thread it would make your UI sluggish (if heavy) - whereas placing them on the background increases efficiency. This kind of ties back to your question in that there are two ways to do a data processing task, in the foreground and in background thread, they both achieve the same outcome and do the same thing - just one is more efficient than the other.
Hope this helps, excuse the rambling! Jayden
Joe Dayvie
11,956 PointsJayden,
Thank you again for responding - Its greatly appreciated! =)
As mentioned before, its good to know that there are multiple solutions. I have been so wrapped up in trying to understand everything that I forgot simply what you mention. Focus step by step and understand what I am doing and then return to make the code efficient. Obviously as I become more experienced, I will write the initial code more efficient.
No worries about rambling - I enjoy to read what others say! I try to get as much knowledge from as many sources and I don't know anyone personally who programs so its places like this which helps =)
Joe

krilnon
1,458 Pointshowever as far as performance goes it is more efficient to go with a computed property than using a separate method
What makes computed properties more efficient?

Jayden Spring
8,625 PointsPaging out to multiple functions as the application increases will have a impact on efficiency albeit for a structure as an example it would be minimal.
Joe Dayvie
11,956 PointsJoe Dayvie
11,956 PointsKrilnon,
I appreciate you taking the time to respond! Its good to know that there are many, smaller differences and as I continue further, things will become clearer (hopefully).