views:

181

answers:

3

I would like to use 'default' as a variable name. Is there a code convention (like class -> clazz) that suggests how I should name the variable?

+11  A: 

I usually add a term that indicates for what it is the default. So I'd use defaultName or defaultPermission or possibly defaultValue (only if the meaning is clear for the context).

Joachim Sauer
Well actually that is exactly what I'm trying to avoid. I have several entities that have a list of other entities (for example user has several address) and one of the list elements is the 'default' entity for the other entity.I'm using dozer mapper to map the entities to DTOs (and to get the default something) and by using generics and a generic 'default' variable (instead of defaultAddress) I can do the mapping with just one custom converter (instead of writing a seperate custom converter for every entity).But I guess defaultValue should do
Sand
@Sand: why not make it `defaultEntry`, `defaultEntity` or `defaultSelection` depending on what *exactly* it is.
Joachim Sauer
`defaultValue` is a good option in cases where you're dealing with primitives, too.
Andrzej Doyle
+1  A: 

I've seen dflt used on several places.

static <T> T nonNull(T value, T dflt) {
    return value == null ? dflt : value;
}
gustafc
Oh no, please don't do that!
Péter Török
I'm not a fan of abbreviations like that either. It looks like it could mean "deflate" or who knows what else.
Jack Leow
To be honest... neither am I.
gustafc
A: 

One more thing: if it is a fixed value - a constant instead of a variable - make it final or even static final/public static final and keep it as class member instead of a local variable. You should write constants upper case.

Example

public class MyClass {

  public static final String DEFAULT_NAME = "MyApplication";

  public String name;

  public MyClass() {
      this.name = DEFAULT_NAME;
  }

}
tweber
The name should be `DEFAULT_NAME`. The Sun Coding Convention says that multiple words should be separated with an underscore in constant names.
Joachim Sauer
Thanks, i corrected the example.
tweber