How to name a function - Coding in Style - episode 11


Welcome to Coding in style, a series of short videos about how to improve our code.

This is Dragos and you are watching the Episode number 11 where we talk about how to name functions and function parameters.

A few days ago I met a friend. “What are you doing these days?”, he asked. I replied: “Book”. “What do you mean?” he asked. “Swift 3”. “Are you are reading a book about Swift 3?” he said. “Yes, I do,” I replied. “Why didn’t say so?” he said.

Of course, this is not a real conversation as it would have been a strange one. But sometimes this is how we name our functions.

As we’ve seen in the previous episodes, the variables are the nouns of our code.

Similarly, the functions are the verbs. They show an action.

To check if a function name is good, you can have a chat with it:

“Hey function, whatcha doing?”

And the function should be able to reply:

“I [function name]”

You should be able to include the function name after “I” and make a valid sentence.

Let’s look at some examples:

"I dropDatabase" - yes

"I sortArray" - yes

"I user" - nop

"I database" - nop

"I createDatabase" - yes

"I databaseCreate" - sort of, but only if your name Yoda is

You can pick a capitalization rule and stick to it. For Swift, it is lower camelcase.

So dropDatabase is fine, but not dropDATABASE or drop_database.

This can be a bit weird in the case of a name that includes an abbreviation. For example HTML or HTTP.

For example, do you use formatHTML or formatHtml?

createURL or createUrl.

In my opinion, whatever you choose looks ugly, so just pick one and be consistent.

Be specific. The name should make it clear what is the purpose of the function.

For example, create is not a good one. createDatabase is better

Long names are okay.

Make the names as long as needed, but no longer than that.

For example, if you have a function createLocalDatabase and there is only one database, replace it with createDatabase.

Regarding the function parameters, apply the same rules as for variables. In couple words, the name of a parameter should be a good replacement in the sentence “my name is [parameter name]”.

Regarding the number of parameters, they should be as few as possible.

Here is what Uncle Bob (aka Robert C. Martin) says in his "Clean Code" book:

"The ideal number of arguments for a function is zero (niladic). Next comes one (monadic), followed closely by two (dyadic). Three arguments (triadic) should be avoided where possible. More than three (polyadic) requires very special justification—and then shouldn’t be used anyway."

How about methods? Obviously, we use the same rules.

But, BTW, do you know what is the difference between a function and a method? A method is a function that belongs to a class. So a function is a free bird. A method is a free bird that has been captured and put in a cage, called class.

What else? What are your ideas about naming a function?

That is about it for today. We talked about how to name a function. In a previous episode, more exactly episode 7, we discussed how to name entities in general and in episode 8 we talked about how to name a variable or a constant. Feel free to watch them for more details.

That’s about it for now. See you soon!

To view or add a comment, sign in

More articles by Dragos Ionel

  • AIGeeks 2019 in review, plans for 2020

    As we’re wrapping 2019 up, we have high hopes for how AI will improve our lives in 2020. This year, we organized and…

    2 Comments
  • How can AIGeeks help you?

    As you know AIGeeks is a Non Profit trying to improve our world around us using AI. Other than organizing the meetup…

  • Be cautious with the ternary operator

    The operators in a programming language, in general, and in Swift, in particular, are of three types: Unary - that…

    2 Comments
  • Use long names when writting your code

    Unfortunately, we have been used to the fact that a name has nothing to do with the object it names. John, Mary, and…

  • Write tests

    Welcome to another post about how to improve our code. Today we are trying to prove that the quality of the code…

    4 Comments
  • Reducing the cyclomatic complexity - Coding in style - Episode 9

    Welcome to Coding in style, a series of short videos about how to improve our coding. This is Dragos and you are…

  • To comment or not to comment

    Welcome to Coding in Style, a series of short videos about how we can improve our code. This is the episode 6 where we…

    2 Comments
  • Coding in style in 60 seconds - Comments

    Welcome to Coding in Style in 60 seconds. This is Dragos and you are watching a series of short videos about how we can…

  • Get smart by being stupid

    Do you know why programming is the best job ever? From the thousands and thousands of possible jobs out there…

  • Coding in style in 60 seconds - capitalization

    Welcome to “Coding in Style in 60 seconds”, a collection of short videos about how we can improve our code in simple…

Others also viewed

Explore content categories