Hi. I got a question to authors of "SCJD Exam with J2SE 5". There is a note at the bottom of page 21 which says: "The Sun Coding Conventions specify that you should apply the same naming convention to all instance, class, and local variables. Be aware that you may see code written by other coders where instance or class variables are signified by an underscore or some other special mark. Another way to achieve the same effect is by using the convention variable when referring to a local variable, this.variable when reffering to an instance variable, and Class.variable when referring to a class variable. Doing so makes it explicit which type of variable you are referring to." Could you please provide a sample code clarifying the part in bold. And one other question about variable naming conventions: can I use some kind of prefixes for naming variables say 'a', 'an' for arguments 'the' for local variables and something like 'i' for fields? Thank you.
Using prefixes, postfixes, and such is extremely poor coding style. Definitely NOT do that, if I were an assessor (I'm not) I'd detract serious points for just that. I've written styleguides in the past, and they've always said to not do it. So does the Sun styleguide which is what you have to follow. So do most others, except the ones promoted by Microsoft for Visual Studio (which still use Hungarian notation which your idea is a weaker version of).
I now have to use it at work, but not for much longer (as I'm switching jobs) and it's a disaster. Completely unnecessary, complicates typing and makes changing the type of something next to impossible as it would mean massive code changes just to get the variable names to match their types again.
I was referring to the last sentence by the OP, not the quote from your book (though maybe the OP misunderstood that note to mean using prefixes).
posted 13 years ago
Thank you all. I actually didn't know that using prefixes/postfixes was such a bad thing. Saying the truth we used prefixes at work and I found it usefull . But now I know this is a bad practice and I will certainly NOT use it in my SCJD project!