• Post Reply Bookmark Topic Watch Topic
  • New Topic

Name convention on GUI elements  RSS feed

 
Stijn Rensen
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First some minimal background on my programming:
Currently I'm playing around with Java, being a complete newby on Java. In the past I have hobbied with VBA (to support some tasks at work) and Pascal (for fun as a kid). But in no means am I a skilled programmer. I mainly do this for the fun of it.

Though I have a longterm goal lingering in the back of my mind: Currently I'm a mathteacher at a secondary school. In the future I might want to extend this with programming once programming becomes a class that is part of a profile a student can do exams in. In which case I will start a study to get certified for this.

But since this lingers in my mind I do want to make sure that I don't teach myself bad habbits while playing with Java.

Ok so I'm confused. Back in the days when I did some VBA to ease my work in MS Exce,l it was very custom to use Hungarian notation for all the elements I put on a window (labels, textfields, buttons, etc). And I really liked this form. Especially when you had a close relation between a Label and a TextField voor instance. In which case names like lblFavoriteFood and txtFavoriteFood would make sence to me.

But now I'm working on some basic programming using swing and the book doesn't mention hungarion notation anymore, nor does the book use txt, lbl, btn, etc, when naming the elements. And googling on the internet I find mixed opinions on this. Where some people favor hungarion notation for GUI elements and other are opose to it.

So how bad would my habbit be if I use hungarion notation?

 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It makes code difficult to read if you use Hungarian notation. Joel Spolsy has some suggestions when Hungarian Notation may be correct, and he says that the original meaning of Hungarian Notation was different from what people mean nowadays, but apart from that avoid Hungarian notation.

Even worse is what you get from IDEs which write code automatically
JTextBox jTextBox1 = new JTextBox();
JTextBox jTextBox2 = new JTextBox();
JTextBox jTextBox3 = new JTextBox();
 
Stijn Rensen
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cheers for the responce. and the link to the article (good way to reuse code.... uhhh.. text) ;)

 
Tim Holloway
Saloon Keeper
Posts: 18789
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use of Hungarian Notation to indicate data types is a Bad Thing. I learned that when I tried to apply it to C++, where an item could swing between being numeric, character, or even a class instance as I continuosly refactored my designs.

On the other hand, I have found Hungarian-style notation very useful in the semantic sense, where I can have a model (xxx), a control (ctlXxx), and a label (lblXXX) for example and the names thus indicate what's related to what.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That latter is similar to what Spolsky says, where he tags variable names with s or us meaning safe and unsafe.
 
Stijn Rensen
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so I used hungarion notation for the incorrect reasons in VBA, got it. And I understand the strength of Apps Hungarian.

So how would one go and name things in swing that are closely linked: like an JTextField where you can enter a zipcode and a JLabel which holds the text "Zipcode".

And in this case I want a variable for the JLabel so I can manipulate the color of the text when needed. So I wont take the following as an alternative:



Thanks!
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That will work nicely, but
  • 1: In Swing you should add details about the Layout to the add() instruction.
  • 2: Because you have an anonymous object, it is awkward to retrieve a reference to that label in order to call its methods.
  •  
    Stijn Rensen
    Ranch Hand
    Posts: 48
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for the remarks on the coding.

    However my focus is on the namingconvention of the objects. Looking to your code you name it zipCodeLabel. Isn't that last part not a bit like hungarionnotation in disguise....? and if so, is that ok?

    Cheers
     
    Campbell Ritchie
    Marshal
    Posts: 56518
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'll let somebody else answer about calling it Label.
     
    Carey Brown
    Saloon Keeper
    Posts: 3309
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Stijn Rensen wrote:Thanks for the remarks on the coding.

    However my focus is on the namingconvention of the objects. Looking to your code you name it zipCodeLabel. Isn't that last part not a bit like hungarionnotation in disguise....? and if so, is that ok?

    Cheers

    Yes, it is a little like hungarion notation but without the sometimes cryptic abbreviations. You need variable names that relate to what they do more than how they do it, but the line between the two is blurry. I prefer names that read well in English (or your native language). GUI programming generates more need for variables than other types of programming and the relationship between a couple of variables is important and using Label, Button, etc., seems like a good way to go about it.
     
    Stijn Rensen
    Ranch Hand
    Posts: 48
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Top replies all!

    If I may take my own conclusion on this:
    - The original idea for hungarian notation (App Hungarian) was to make coding less errorprone. Used correctly, shaddy or wrong code would pop out because of inconsistency in a line of code
    - However the paper on this got misinterpetated on the word type. Mixing types of variables over sort of variables.
    - That gave the hungarian notation a bad name and as such also the principle of app hungarian
    - However good coding also implies sensible names for all your variables. This may include a description on what the variable is used for (ie zipCodeLabel is used to control the JLabel object on zipCodes).

    So to conclusion: don't use hungarion notation for the sake of "typing" your variables. But do give your variables descriptive names. And if that means you include the variabletype in the name that in itself is not a bad thing. Especially while programming GUI's since that is prone to having more variables.

    Stijn
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 66304
    152
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hungarian notation in a strongly-typed language is worse than useless.

    I used it when I was writing in assembly language, but not since.
     
    Thomas Gard
    Ranch Hand
    Posts: 51
    2
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Bear Bibeault wrote:Hungarian notation in a strongly-typed language is worse than useless.


    Not precisely. Having source code get past the compiler with type safety is one thing.

    Having the exhausted engineer at 3am hopped up on caffeine read it is another.

    Code should always be written with an unknown reader in mind. If adding small clues to the end of variable names can help indicate what things are, then all the better. Note, this is nothing compared to the gobleddygook hungarian crap used in the Windows world.

    I have no issue with:

    startButton
    endButton
    pauseButton
    (^^^all 3 together are cleanly readable when they appear deeply inside a GridBagLayout.

    startB
    endB
    pauseB
    (^^^These are better than: )
    start
    end
    pause
    ....when all 3 are Button instances.
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 66304
    152
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thomas Gard wrote:Code should always be written with an unknown reader in mind.

    Precisely. That's why good naming is important, and cryptic Hungarian notation is a no-go.

    Your example has nothing to do with Hungarian notation, but with common-sense naming.

    But yes, in my opinion startButton is the only acceptable naming. Neither startB or start would pass my code read and get merged.

     
    Stijn Rensen
    Ranch Hand
    Posts: 48
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thanks Bear and Thomas,

    Seems like we are on the same page. good Naming is important. And adding the type of the element to a variablename isn't a bad thing. cryptic abbreviations is the thing you want to avoid. Unless they are widly accepted. Like i for a counter, or g for the graphics.

     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!