Friday, November 27, 2015

Whacha doin' or how to name a function?

While the variables are the nouns of the coding (as we've seen), the functions are the verbs.

To be sure they have a correct name, ask them:

"Whatcha doin'?"

"I [function name]" should be the answer.

"I createDatabase" - is good.
"I sortArray" - is great.

"I user" - not that much.
"I database" - nop.



Also the order of the words is important. First one should be a verb:

"I createDatabase" is good.
"I databaseCreate" is okay, but only if your name is Yoda.

The name should be explicit:

"create" is a good name, but not clear. Create what? A variable, a database, a singleton.

The same as for variables, make the name of the function as long as you need, but not longer than that.

For example createLocalDatabase might be replaced with createDatabase if there is only one database.

Use camel case, unless there are other conventions in the language you are using.

Why is createDatabase better than create_database? Because it is shorter.

Tomorrow we'll talk about how to name the parameters of a function.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.