Q. Function name verb convention
A. Every programmer knows naming is very important.
I want to tell you about a convention I use when naming functions.
When the first word is a verb it tells you something about what
the function does.
Too many functions in the world are called getSomething().
Is it a trivial function or one that might do something over the internet?
My naming convention helps to make it clear.
If a function is doing a small amount of string manipulation then
I call it composeSomething()
For example composeTemporaryFolder()
This name tells me that it might use $TEMP and something else to
make the name for a temporary folder. But it does not create it.
"compose" refers to composing a letter or email
rather than function composition g(f(x))
When a function's work might already be done but we just want
to be certain that it is, its called ensureSomething().
For example ensureFolderExists(folderName)
In this case, we know the function will not return an error
if the folder already exists. It will just exit early.
When we actually going to be doing some significant work
its time for makeSomething().
For example makeFolder(folderName)
This function is actually going to create the folder on disk.
Does the Linux funtion mktemp(3) make the name of a folder and create
it on disk? It composes the name only.
However mkdir(3) does create on disk.
set and get
"get" is overused. It can be a "getter" in a class which
just returns the value of member or it might do a huge amount of work.
I only use setSomething() and getSomething() when its a small
amount of work.
fetch and put
I use fetchSomething() to get something where there is some work
involved - eg from a database or website.
And putSomething() to set something where there is work
to be done.
Asking a question
I used to use isSomething() for all boolean functions.
But now I'm not so strict, I use the best verb for the question
But isSomething() is the default.
What about when a large amount of work needs to be done to get
a boolean result? In that case I would use fetchSomething
Putting it all together
url = thing.getUrl()
content = fetchPage(url)
folderName = composeTemporaryFolder()
putFile(folderName + '/stuff.txt', content)