guidelines for naming widgets


what’s the “best practice” - if any - to name your widgets in a window?
For example, if I want to display a textbox asking for a username input, is it better to call it “username”, “usernameWidget”, “usernameTextBox”, or “txtUsername”?

I’d like to use the last one (Hungarian notation), but that’s not really Java-like, although it makes sense (to me) to clearly distinguish between a username text box and, for example, a username label…

Thanks for your suggestions,


Having read enough cryptic names such as “lpstrNameId” in a past life, I would suggest leaving the type or context out of the name, while keeping it in mind, as you would do in natural language. That is the normal Java convention, I believe.

So, “username” is OK when you are talking about a bean property, as well when when you are talking about an user interface field bound to such a property. The context of the name, whether it is a bean or a Vaadin composite, should be enough to make it understandable.

Sometimes you may need to add some type information to a name to make it unique in the context. For example:

ObjectProperty nameProperty = new ObjectProperty("Hello", String.class);
TextField nameField = new TextField("Name", nameProperty);

But, if those variables are in a different context, such as member variables of different objects, they could be both “name”.

Sometimes, only the type could be relevant in a context. For example, if you put fields to a form, you could just as well call the form “form”. But if there can be many forms, you should start giving them names.

In general, names should be abstract enough. It should not matter whether a variable is a TextField or TextArea or ComboBox or whatever else.

But, in the end, it is entirely your choise, what you find best and intuitive and informative enough.

(lpstr? there
only long pointers in C nowadays…)