views:

258

answers:

10

Background:

There are a few threads on SO about how to choose names for variables and functions. It dawned up on me that there might be a use for a site where you could go and ask random people what name a particular function should have. You describe the behavior, and they come up with a name. The benefit of this, you get multiple perspectives and potentially code that is more readable to more people.

The other thing that dawned upon me is that SO may not be a good site for that kind of thing. Who wants to create a new SO question every time they scratch their neck trying to think of a function or variable name?

Question:

I have a function that does something very simple. It takes a source string and converts it into a target output. The target output is intended to be usable as a variable name or a database column name.

Example:

'Customer Name' --(becomes)--> 'customer_name'

What should I name my function? Where can I go to ask this question for the 356 other functions I want to have perfect names for?

See also:

http://stackoverflow.com/questions/1505880/thoughts-on-variable-function-naming-conventions ;; http://stackoverflow.com/questions/380458/marking-up-argument-names-in-comments-of-functions

+3  A: 

The output for your function looks like a slug, so I've seen functions that do that called slugify.

In general name your functions according to what they do, so your code reads as naturally as possible. If it's really hard to name your function, it may be because your function is doing too many things, or does not have a clearly defined purpose, in which case, refactor your code.

Dominic Rodger
+6  A: 

Good function names are definitely important, I won't argue with that. However, you are thinking too hard. You will never get any work done if you agonize this much about naming your 356 other functions.

See this article from Joel for some new thinking you should consider.

Kyle Heironimus
Agreed, it's better to refactor the names later and go with whatever comes to mind and actually get something done.
Jonas
Trying to improve one of the most important software craftsmanship skills is thinking too hard? I've gotta say minus one.
Jeff Sternal
+2  A: 

Come up with the simplest name that still conveys what the function is doing. You are converting a regular string to a slug, so slugify() or convertToSlug() would both be fine names.

If you can look at the name of a function, and know what it does by that, then it's a fine name.

There are also varying conventions for different languages and frameworks. Here is an interesting reference to check out on the subject: Naming Conventions

GSto
+1  A: 

When it comes to naming schemes for data elements there is a "gold standard" that I like to refer to: The US Coast Guard Data Element Naming Standards. If you're in the business of data element name management then I'd regard it as a must-read. Maybe not as a must-obey, but certainly it is food for thought.

http://www.uscg.mil/directives/ci/5000-5999/CI%5F5230%5F42A.pdf

David Aldridge
If I want expertise on air/sea rescue, I might well go to the Coast Guard. But for data naming? A quick skim of that document singularly failed to impress.
anon
Then you skimmed over the extensive parts that laid out the general principles: prime words, class words, modifiers etc., and the suggested methods for identifying them, constructing data elements from them, and so on. Naturally the examples are drawn from the coast guard domain but the principles extend to every environment (finance, logistics, customer subscriptions, network monitoring etc) that I've worked with.
David Aldridge
+1  A: 

When naming functions, you can never go wrong with VerbNoun format. In your specific case, I would use something like NormalizeName. The more you do this, the better you will become at it. As to whom to ask about questions like this, my most valuable resource tends to be co-workers. As another person said, however, I wouldn't agonize too long about the function names you use; just make sure that they're clear and concise. If you can't find the "perfect" name, be sure to explain the function well in its comment header (actually, be sure to do that anyway).

Emerick Rogul
A: 

personally I go with asking someone in the same office as me, usually a non-programmer so I can get a plain english answer.

However in this case you probably want to use the obscure technical term slug, so ToSlug()

jk
+1  A: 

My answer to your first question is 'columnizeSourceString'. For your second question, go to careers.stackoverflow.com and take ANY candidate that you find. They will all be able to come up with decent function names without thinking too much into it or attempting to "crowdsource" it.

msergeant
Yes, but I chose a very simple example to try to get at the core of a bigger issue. The example is not a candidate for "crowdsourcing", but one can easily show examples that *are* candidates for mutual input.
dreftymac
+2  A: 

The answer depends on the code you are developing, and if you are writing code for a public project that already have coding standards.
Generally speaking, and without to know the language used for the code, I think that names like <verb><object> are usually better; using just the verb could take to a function name that can conflict with another name, and which would not be clear enough for who uses the function.

I also think that a generic answer is not possible, as a function name depends also from the built in functions available from the language; to make an example, if I would make a function to handle arrays in php, I would probably avoid to use a name starting with array_ to avoid conflicts with functions made available from PHP.
The name to choose for a function depends also from the support for namespaces present in a langugage; in a language with support for namespaces, or modules, the function name is simplified, but in a language without such support the function names should be chosen to avoid conflicts.

kiamlaluno
That is a nice answer from the perspective of daily practicalities. Thanks.
dreftymac
+3  A: 

Here's an approach, rather than an answer. Name it foo(). That lets you put it into context, apply it, stretch it out a bit without worrying about the name. And then, somewhere that you're using it, you'll have a wee epiphany (or just get annoyed at seeing "foo" everywhere), and then you'll know what the name should be. Then rename it. Since today's tools make renaming so easy, reliable, and safe, you can take this kind of approach, not worrying immediately about the name, knowing you can (and will) fix it in the future.

In my experience, the foo's don't last more than a couple of minutes; if you find them sticking around for days, this approach probably isn't for you.

Carl Manaster
This sounds good. The only modification I could think would be to use foo001 foo002 ... foo00N so that the foos are distinguishable placeholders that can be globally searched and replaced.
dreftymac
You shouldn't be using search and replace. If you're using an editor that doesn't support the automated Rename refactoring, I would avoid this technique.
Carl Manaster
A: 

You are the one who knows best about what the method actually needs to do. Discuss with your colleagues what the best name will then be. The hard part is distilling the "what does this do" so you do not have to look at the actual method to get it.

The best way to do this is repeatedly (so you learn) and iterative (so you improve). Use refactoring for the iterative part.

Thorbjørn Ravn Andersen