Java puts some rules and conventions for naming every element/identifier in a Java program, just like any other programming language does.
The conventions come into play when we're talking about codes readability and maintainability. Sun says that only 20% of the lifetime cost of a software goes to code creation and testing, while the other 80% goes to maintenance….it's very important for every software engineer to be aware about this fact. That said, adhering to Code Convention is as mandatory as adhering to naming rules.
Sun, as the Java creator, has made a "Java Code Convention" that every Java programmer on the planet should follow. It's a short, concise document. If you never see it, then go to http://java.sun.com/docs/codeconv/
right away, download it ASAP and read it before you make any important code….and any fatal mistakes.
This article, however, only talks about the Java Code Convention applied to naming procedure.
So, what are the naming rules and conventions that will make Sun proud of you if you could implement it successfully..? well at least the guy who is designated to maintain your code won't hate you and terrorize you after a minute of reading your code….
Naming Rules
- First of all, you're not allowed to use java keywords and reserved words. For the complete list of all those words, see the list in the bottom of this article
- The identifier can be composed of:
- Unicode letter character, e.g: a..z
- Unicode digits character, e.g: 0..9
- Dollar sign: $
- Underscore: _
with a restriction that (read no. 3)
- Unicode letter character, e.g: a..z
- The identifier can start with any of the item listed in no.2 except with a digit. That said, it must start either with a letter, or a $, or a _. After the first character, the identifier can be composed of any combination of them.
- Identifiers are case sensitive. Cow and cow are two different identifiers.
- The identifiers can have unlimited number of characters.
That's it! Here are examples of legal identifiers:
aName123__$$;
_$123;
$$$$$$$$;
a_very_very_long_name_here;
and examples of illegal identifiers:
1aName;
&aName;
:)smileyName;
-creativeName;
Naming Conventions
- In general, use a complete whole word, not a cryptic abbreviation. That would make the element we name become more intuitive and clearer.
example: use manager instead of mngr - Classes and Interfaces
The identifier for classes/interfaces should be a Noun, start with an uppercase letter, and if there are more than a word (internal words), then the first letter of subsequent words should be capitalized too (a convention called as camelCase rules).
example:class Car
class SuperCar
interface Resizable
interface FlexiblyResizable - Methods
The identifier for methods should be a Verb. Start it with a lowercase letter then apply camelCase rules.
example:
hold()
holdTight()
measureLength()
searchWords()
- Variables
The variables identifier should be a Noun and, like a method, start it with a lowercase letter and apply camelCase rules.
example:
account;
accountNumber;
space;
spaceArea;
- Constants.
Constants are static final variables. Every letter of their identifier should be capitalized. Use underscore character to separate internal words
example:
MAX_INTEGER;
PI;
CLICK_COUNTS;
Again, this article is all about naming rules and conventions. To find out more about another java programming conventions, read the great "Java Code Conventions" document
Java Keywords
abstract | assert | boolean | break | byte |
case | catch | char | class | continue |
default | do | double | else | enum |
extends | final | finally | float | for |
if | implements | import | instanceof | int |
interface | long | native | new | package |
private | protected | public | return | short |
static | strictfp | super | switch | synchronized |
this | throw | throws | transient | try |
void | volatile | while |
|
|
Unused keywords:
const goto
Reserved words: true false null
No comments:
Post a Comment