Java Language Fundamentals (Part 1)
Comments and Code Conventions
There are 3 types of comments allowed in the Java language:
// comment on one line
/* comment on one or more lines */
/** comment which can be used in the javadoc tool to produce HTML documents */
Always document your code!!!!! I’ll say it again, ALWAYS DOCUMENT YOUR CODE!!! Well documented code is easy for other developers to modify at a later date. In fact you could be that developer, who has to change the undocumented code that you produced a year earlier and can’t remember what all of those 1000 lines of code does.
Do not put obvious comments in. if you have one line of code in a class that prints a string, don’t write “this class prints a string”. On the other hand, if you have a complex class that contains 150 lines of code, comment liberally.Whitespace (tabs, newlines, etc.) in Java is ignored. A line of code is terminated with a semicolon(;) even if that line happens to span several lines on your screen. A block of code is a collection of statements bounded by opening and closing braces. For example:
{
topSpeed = 120;
lowSpeed = 35;
}
You can increase readability by using meaningful class, variable, and method names. Avoid using names like “d1” – rather use something like “firstDate”. The Java convention for identifiers are:
Class identifiers – a noun with the first letter of each word capitalized – for example: class MyCatVariables – a noun with the first letter of the first word lowercase and each subsequent word capitalized – for example: int topSpeed;
Methods – a verb with the same naming convention as variables – for example: public void DropCat()Interfaces – capitalized like class names
Constants (finals) – all letters capitalized. For example:Final int MAX_SPEED = 200;
Also, the use of indentation is invaluable. Consider the following code snippet:
while (counter < 0){
for (loopCounter = 0; loopCounter < 11; loopCounter++){
System.out.println(“this is difficult to read”);
}
System.out.println(“think how this looks with another 100 lines!”);
}or this code snippet:While (counter < 0){
For (loopCounter = 0; loopCounter < 11; loopCounter++){
System.out.println(“this is better”);
}
System.out.println(“this shows the inner loop better”);
}
No comments:
Post a Comment