In the case of variable names with a single word, it is normal to write them in the following way:
int entero;
double doble;
String cadena;
But for the case of variables that use two words, how should they be written? I have only used two forms:
Option 1:
int numero_entero;
double numero_doble;
Option 2:
int numeroEntero;
double numeroDoble;
I have also noticed that some variables that the Java libraries have are written in uppercase and separated by _ :
Foo.ANCHURA_MAXIMA;
Foo.ANCHURA_MINIMA;
Naming conventions make programs more understandable by making them easier to read. They can also provide information about the function of the identifier, such as whether it is a constant, a package, or a class, which can be helpful in understanding your code.
The Java documentation outlines its naming convention:
Paquetes
The prefix of a unique package name is always written in lowercase ASCII letters and should be one of the top-level domain names, currently com, edu, gov, mil, net, org, or one of the two-letter codes in English identifying countries as specified in ISO 3166, 1981 .
Subsequent components of the package name vary according to an organization's internal naming conventions. Such conventions might specify that certain directory name components are division, department, project, machine, or login names.
Clases
Class names must be nouns . If there is more than one word, the first letter of each word is capitalized. Try to keep your class names simple and descriptive. Use complete words: avoid acronyms and abbreviations (unless the abbreviation is used much more than the long form, such as URL or HTML).
Interfaces
Interface names must be written with each first letter capitalized, like class names.
Métodos
Method names must be verbs. The first letter is always lowercase and if there are several words, the first letters of each internal word in uppercase.
Variables
Except for variables, all instances, classes, and class constants are case sensitive with a lowercase first letter. Internal words start with capital letters. Variable names must not start with underscores
_
or dollar signs$
, although both are allowed.Variable names should be short but meaningful. The choice of a variable name should be mnemonic, that is, designed to indicate to the casual observer the intended use. One-character variable names should be avoided, except for "throwaway" temporary variables. Common names for temporary variables are:
i
,j
,k
,m
, andn
for integers;c
,d
, ande
for characters.Constantes
The names of variables declared as class constants and of ANSI constants must be all uppercase with words separated by underscores ("
_
") . (ANSI constants should be avoided, for ease of debugging.)However you can use another convention. It is recommended that it be one of the standardized ones in the programming community and that it be uniform , that is, that you use the same naming convention in all your code.
As a general rule:
For classes and variables:
$
,_
, numbers.$
and_
Lessons:
Use of convention UperCamelCase (first letter capitalized, the rest of the words begin in capital letters.)
Variables:
Use of convention lowerCamelCase(first letter lowercase, the rest of the words begin in uppercase.) p/e: fullname