When you are defining a function/variable/etc and are not sure what to name it, what do you name it? How do you come up with a name?

If you use a temporary name as a place-card until you give it it's real name, what temporary name do you use?

update

I have been using things like WILL_NAME_LATER, NEEDS_NAME, or TO_BE_NAMED. I was hoping there was an adopted convention, I was actually hoping that if I used this adopted convention my IDE would highlight the name until I changed it.

I guess the convention for something like this would be to add a TODO in the code, with the explanation of why you need to update it. Many tools can parse those TODOs and show a list of all things left to be done.
–
Eduardo ScozOct 1 '10 at 15:00

18 Answers
18

It's nearly impossible to not be able to think of a name for an artifact you want to design. You may not like what name you come up with because it isn't concise or sexy, but if you think too hard, you'll end up with a poorly named artifact.

Let's say you have something that helps you construct objects, but you don't know this is typically called a factory. Just call it ObjectCreator. It sounds obtuse, but at least it's clear.

Let's say you have a dictionary that converts hostnames to IP addresses. Just go ahead and call it HostnamesToIpAddresses. Sure it's long, but it says exactly what it does.

The inability to come up with a name for something means you don't know what it is doing, which also means you have a greater problem before you.

I always want to slap people who complain about identifiers being too long (when they're only three or four words). We should not be limited by our typing speed, and if we are, we should go look at ABCD and learn to type!
–
dash-tom-bangSep 30 '10 at 21:20

If you can't think of a good name on a variable or function, you either don't understand what you're doing or you got a poor design. Either way, slapping some arbitrary name like "x" (unless you're dealing with coordinates) won't solve your dilemma; it will only make it worse and the pain to maintain greater.

I disagree. Developers won't always have a full understanding of the business's terminology. Heck, even the client may not be able to convey the terminology during requirements gathering-- let alone at times even understand their own business! But this doesn't stop software from being produced. It doesn't stop prototypes from being created. Or even code being out right thrown out because of a lack of understanding. But code in these situations still need to be produced and delivered.
–
spongSep 30 '10 at 20:58

3

@sunpech Excuse me, but this is a little scary. Are you trying to tell us that it's common to write random code not knowing what it actually does? If you don't know something, put some effort to find out and you'll avoid problems in the future.
–
Adam ByrtekSep 30 '10 at 22:28

1

@sunpech: The only place I've actually seen foo and bar be used in code is in small snippets of code used to show a concept. Sure, I stumble too when think up names, but I have never ended up in such bad position where foo has been the only reasonable option. If that were to happen, then I really don't know what the hell I'm doing and need to go back to the drawing board and pseudo-code instead till I know what to do.
–
gablinOct 2 '10 at 18:41

I name it what I think the function should do -- something that more or less conveys the intent. Once the body of the function is written, I find it obvious what to call it and go back and rename it if needed.

This doesn't always work, especially when the developer may not be familiar with the terminology or even intent of the business/industry. Something more generic and obvious should be used to convey that it needs to be defined and changed later on. The OP seems to be asking when the intent is unknown, and what should initially be used. If the naming is something that more or less conveys intent, then a good name is already not far off.
–
spongSep 30 '10 at 20:30

1

@sunpech Seriously, even when first starting out in a job with a fairly specific domain, I haven't had trouble coming up with a function name. Maybe I just need to write more functions. :)
–
Anna Lear♦Sep 30 '10 at 20:50

1

@sunpech: How can you write a function without knowing what it does?
–
configuratorSep 30 '10 at 22:51

3

@sunpech: I didn't say that. I just said you can't possibly write a function without knowing what it's doing. It's not possible. I've never seen it happen, and I don't see how it could possibly happen. Maybe I'm being daft, but when you name a function DoFoo(), what in the hell do you put inside it??
–
configuratorOct 1 '10 at 1:19

2

@sunpech: I never said names have to be 'good' from the start. I just fail to see how you would come into a situation where you want to create a function but you don't know what it's going to do.
–
configuratorOct 2 '10 at 0:06

This question and especially its answers scare me senseless. Somebody's going to have to maintain that code in which you just named a variable "Cup", you know. If you're not lucky, that somebody will be you!

You've got a variable. It's a thing. It represents a thing, anyway. And things have names. That's how you know they're things! Are you really telling me you have to name a thing after another thing because you can't come up with the name of the actual thing?

Iterators should be called i. Nested iterators are likely a mistake, but if you need them, then make your way through the rest of the vowels, in order (a, e, o, u, and god help me, yes, sometimes y).

The terms foobar, foo, bar, and baz are sometimes used as placeholder names (also referred to as metasyntactic variables) in computer programming or computer-related documentation. They have been used to name entities such as variables, functions, and commands whose purpose is unimportant and serve only to demonstrate a concept. The words themselves have no meaning in this usage. Foobar is sometimes used alone; foo, bar, and baz are sometimes used in that order, when multiple entities are needed.

If I can't come up with a great name right away, I use an "okay" name temporarily, then keep coding. At least it'll be something that adequately describes the item, even it's not perfect. Almost always, by the time I'm done writing the first draft of that particular chunk of code, a more perfect name will have occurred to me. Through the process of coding, my intentions with that particular variable become more clear. (On the other hand, sometimes it occurs to me that the variable to was ill-conceived to begin with and I delete it in favor of something else.)

Prefix your function with something and give it a best-shot name for now. For example, a function that saves-all-products-for-the-selected-user-to-the-database could be RENAME_SaveAllProductsForTheSelectedUserToTheDatabase()

If I absolutely have no idea what to name the variable, which hasn't happened in over twenty years...names of old girlfriends, or women I wish had been girlfriends. The last code with those identifiers was removed from production quite a few years ago.

I almost never have a problem finding good, descriptive names.. but sometimes the naming becomes quite redundant, in that the class and variable names ar very similar.
WebClient webclient = new Webclient (uri );
...and the like.