Name convention on Java Swing components(prefix)

后端 未结 4 1107
忘掉有多难
忘掉有多难 2021-02-06 00:46

An question that was brought to my mind when programming with Swing in Java was, is it a recommendation or an \"official\"/most used naming convention(prefix) on Swing component

4条回答
  •  情书的邮戳
    2021-02-06 01:03

    Why not just call JTextField textField, JButton button, JLabel label, and JPanel panel. Is it so bad to spend a few extra characters to make the variable read like an English word?

    Furthermore, when I do put the type in the variable name, I put it on the end. So a JLabel that displays a name is nameLabel (which IMO is more readable than lblName).

    And even furthermore, following duffymo's advice, it's bad practice to put the type in the variable name. A better approach is to describe what the variable is. In the case of a name label, it's a UI component that displays name. So a better name might be nameComponent. The fact that nameComponent is a JLabel or some other type is secondary and shouldn't clutter the variable name.

提交回复
热议问题