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
2010-06-23 10:28:57
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
2010-06-23 10:32:21
@Sand: why not make it `defaultEntry`, `defaultEntity` or `defaultSelection` depending on what *exactly* it is.
Joachim Sauer
2010-06-23 10:58:20
`defaultValue` is a good option in cases where you're dealing with primitives, too.
Andrzej Doyle
2010-06-23 11:45:06
+1
A:
I've seen dflt
used on several places.
static <T> T nonNull(T value, T dflt) {
return value == null ? dflt : value;
}
gustafc
2010-06-23 10:31:30
I'm not a fan of abbreviations like that either. It looks like it could mean "deflate" or who knows what else.
Jack Leow
2010-06-23 10:41:33
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
2010-06-23 11:42:54
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
2010-06-23 18:56:27