|
||||||
Write better code by using the right data types, naming conventions, and comments.
Like many programming languages, Java has coding conventions to encourage uniform code. They may seem arbitrary at first, but using naming conventions, proper data types, and commenting on blocks rather than statements will prevent errors and explain program steps later on. Naming ConventionsJava has several naming conventions that are strongly suggested to create orderly and readable code. Naming conventions are guidelines to write the unique identifiers of each element type:
Data TypesJava elements store a variety of data, but a new element must declare its data type before it can be used. Java has two groups of data types: primitive data types and object reference data types. Primitive data types are the most common and consist of:
Object reference data types are elements that store references to data rather than the data itself. A web page that contains a link to text rather than the text itself is a loose analogy for an object reference element. Commenting on CodeCommenting provides clarity for both the author and future readers. Comments must concisely describe a block of code rather than individual statements. Longer programs gather more comments, but the interpreter ignores comments so as not to affect file size. Insert a single line of comment by beginning the statement with two forward slashes: //This is a comment. Every line after this will not be commented. Insert a comment block with /* and */ : /* This is a block of comment. Anything inside the symbols is commented. */ Statements & BlocksStatements and blocks are common concepts in C-based languages and are the equivalents of sentences and paragraphs. A statement ends with a semicolon (;), which also begins the next statement. A block is a group of one or more statements enclosed in curly brackets {}.
Summary As with human languages, Java recommends coding conventions to prevent sloppy programming. Using comments or researching the proper data type is annoying at first, but will save time later with troubleshooting and documenting.
The copyright of the article Java Coding Conventions in Javascript/Java Programming is owned by Ana Mills. Permission to republish Java Coding Conventions in print or online must be granted by the author in writing.
|
||||||
|
|
||||||
|
|
||||||