11

Velocity Template Language's set directive requires a variable on the left hand side. This doesn't work.

#set ( $entries.add("d") )

Even though I have no use for the return value of add("d"), I have to assign it to a variable.

#set ( $x = $entries.add("d") ) 

I want to communicate that the variable is necessary, but I have no intention of using it later. Is there a convention for naming variables that only exist to appease the compiler?

  • 2
    how about just using a comment – Gratzy Dec 1 '11 at 15:15
  • foo? [15 characters] – Konrad Morawski Dec 1 '11 at 15:15
  • 6
    I would call it code smell. – Joel Etherton Dec 1 '11 at 17:53
  • Hi Patrick, variable naming questions are off-topic here: for more information, check out Are “name that thing” questions on-topic? – user8 Dec 1 '11 at 18:48
  • 1
    @Mark Okay, thanks. FWIW, I think this question leans more toward the "proper term for a concept" than "naming suggestions for a class or a method." I'm looking for an idiomatic name that would be used in various languages and codebases, not a specific name for a single variable. – Patrick McElhaney Dec 1 '11 at 19:03
26

I tend to use dummy for this kind of situation (a variable that I must have though I don't need to use).

16

You should check and see if your language has a name specifically for this use - Python has _ that you should use for variables you intend to throw away.

  • 10
    Note that _ is the traditional name for this kind of variable in languages that provide pattern matching (ML, Haskell, scala, etc.) – barjak Dec 1 '11 at 15:28
  • @barjak Good to know - I've never used any of those languages, I thought it was a Python thing – Izkata Dec 1 '11 at 20:38
12

dummy, temp, trash, black_hole, garbage ... any one of these tells me I won't be using them for anything.

If I do use if for something, I always rename the variable though.

  • 1
    +1 for renaming it if you do use it later. – Spencer Rathbun Dec 1 '11 at 18:36
  • Thanks, but this doesn't answer the question. I can think up plenty of names that say "this is not important." I'm looking for a single, standard name. – Patrick McElhaney Dec 1 '11 at 21:09
  • Except for a few newer languages, most of them don't have such. Such standard does not exist, if you don't consider the ones that gotten the most votes in here as "standard". – Rook Dec 1 '11 at 22:25
3

You have plenty of good answers already, however I thought I'd toss one that's more directly related to velocity.

What is the add method returning? If it's null you can simply tell velocity to quietly reference it:

$!entries.add("d")

However if you are replacing a value, and it returns the old value (like Map.put does), then you would need to set it to a dummy variable of some sort, if it's a void method, then you shouldn't need the variable at all.

If you're adamant about not using a new variable you could do the following:

#if($entries.add("d")) #end

If you're using it often enough I'd create a macro for it, then use it throughout your templates. I don't see how useful that might be though, but at least you'd only need to comment it once...

  • Brilliant! The method returns true in this case ($entries is an ArrayList), but man, I wish that worked. – Patrick McElhaney Dec 1 '11 at 17:08
  • I was afraid that you were in fact referencing a list. I've edited the answer to denote a way that you could go about not using a dummy variable, but I'm not so sure if I'd use it... – Scott Dec 1 '11 at 17:55
2

I would give it a reasonable name and add a comment explaining that you are only assigning it to appease the compiler. I have seen cases where someone has used a variable name like unused and another developer has subsequently used that variable by that name and made an ugly mess of the code.

  • I like unused too. The only problem I have with it is the fact that I'm using it the moment I'm writing it :/ – Rook Dec 1 '11 at 16:07

Not the answer you're looking for?Browse other questions tagged or ask your own question.