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 trialAngelus Miculek
6,534 Pointsnot too impressed with this function
- Why do we need this function? Could you not just add $2 (the service charge amount) to the original amount_due equation?
- Why do we need a parameter for the function, and why do we have to change num_tickets to number_of_tickets?
2 Answers
Travis Alstrand
Treehouse Project ReviewerHey Angelus Miculek 👋
I think the creation of the function was done to keep logic separate in it's own place for readability and in case that same calculation needs to be used again in any other part of the code. He mentions in the video it's not necessary in this case, since it's not being used elsewhere, but it's generally good practice to separate your logic out like that to make code reusable and not repeat ourselves down the line.
The change in naming convention in that function was his preference in the moment. When naming a parameter on a function, you can technically name it anything you'd like, but it's always good to keep it relevant and understandable. Since there is already a variable below named num_tickets
, he probably named it something else, but similar to that, to not cause extra confusion (mixing up variable names). The variable below was still kept num_tickets
, just the variable local to that specific function is named number_of_tickets
.
You'll find your own preferences over time of how you like to factor / refactor your code and what naming conventions you like, but there are general 'good practices' that are always good to keep in mind when doing so. We want it to be as clear and understandable as possible as if an outsider who didn't write the code was looking at it and able to understand it with no problem 👍
Angelus Miculek
6,534 PointsThanks, Travis!