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?
Even worse is what you get from IDEs which write code automatically
JTextBox jTextBox1 = new JTextBox();
JTextBox jTextBox2 = new JTextBox();
JTextBox jTextBox3 = new JTextBox();
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.
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:
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?
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.
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.
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:
(^^^all 3 together are cleanly readable when they appear deeply inside a GridBagLayout.
(^^^These are better than: )
....when all 3 are Button instances.
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.
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.