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

iOS Swift Functions and Optionals Parameters and Tuples Named Parameters

What is the point of having named parameters? How do they help?

Just wondering how named parameters could be useful in Swift. It just seems like more work to me.

3 Answers

Jeremy Hayden
Jeremy Hayden
1,740 Points

When your only dealing with 10 lines if code, then you are correct. They are kinda pointless.

But when your on a project with 5000 lines of code over a period of a year, it makes your code way easier to read. The easier it is to read, the easier and faster it is to debug.

You can quickly look at your functions and know exactly what each element is used for.

I have gone over code that I had written 2 years ago and thought, what was I thinking!? What the heck is this funtion for?

On the flip side, I now need less lines of comments describing my code becuse I chose descriptive variable and function names as well as named tuples.

Julian Coy
Julian Coy
6,774 Points

The only use that I can seem to find for eternal (named) parameters, is when you are actually WRITING the code. It allows you to forget about parameter ordering due to autocomplete functionality of Xcode. In reality, they are simply a convenience tool, they don't change anything except for they FORCE any invocations of a method that utilizes external parameters to cater to that form.

Honestly, I dislike the compiler-level enforcement policy in this regard. I think you should be able to still call the function without having to specifically name each individual parameter, but that's just my 2 cents.

If you call your function in only one place, then go ahead and hard code your variables (in which case you probably don't need to make it a function). If you call it in more than one place, such as an area function, then you can easily just change the parameters of the width and height when you call the function rather than changing the variables in the function which would only make it work in one place. Parameters are very useful.

Julian Coy
Julian Coy
6,774 Points

I don't think you understood the question. The OP wasn't asking for the use cases of parameters, but was asking about external (named) parameters in particular.

Your answer isn't incorrect, it's just for the wrong question.