Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
12
Coding
Standards
CERTIFICATION OBJECTIVES
P:\010Comp\CertPrs8\360-6\CH12.vp
Saturday, November 26, 2005 2:47:30 PM
Color profile: Generic CMYK printer profile
Composite DefaultCertPrs8(SUN)
screen / Sun Certified Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 12
CERTIFICATION OBJECTIVE
Spacing Standards
This section covers the standards for indenting, line-length limits, line breaking, and
white space.
Indenting
We said this was going to be fascinating didn’t we? Each level of indentation must
be four spaces, exactly four spaces, always four spaces. Tabs must be set to eight
spaces. If you are in several levels of indentation you can use a combination of tabs
and (sets of four) spaces to accomplish the correct indentation. So if you are in a
method and you need to indent 12 spaces, you can either press SPACEBAR 12 times,
or press TAB once and then press SPACEBAR four times. (Slow down coach.) We
recommend not using the TAB key, and sticking to the SPACEBAR—it’s just a bit safer.
When to Indent If you indent like this, you’ll make your assessor proud:
P:\010Comp\CertPrs8\360-6\CH12.vp
Saturday, November 26, 2005 2:47:31 PM
Color profile: Generic CMYK printer profile
CertPrs8(SUN) / Sun Certified
Composite Default screen Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 12
public Indent() { }
int x = 0;
P:\010Comp\CertPrs8\360-6\CH12.vp
Saturday, November 26, 2005 2:47:31 PM
Color profile: Generic CMYK printer profile
Composite DefaultCertPrs8(SUN)
screen / Sun Certified Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 12
■ Align the new line a tab (or eight spaces) beyond the beginning of the line
being broken.
■ Try not to break inside an inner parenthesized expression. (Hang on, the
example is coming.)
White Space
Can you believe we have to go to this level of detail? It turns out that if you don’t
parcel out your blank spaces as the standards say you should, you can lose points.
With that happy thought in mind, let’s discuss the proper use of blank lines and
blank statements.
The Proper Use of Blank Lines Blank lines are used to help readers of your
code (which might be you, months after you wrote it) to easily spot the logical
blocks within your source file. If you follow these recommendations in your source
files, your blank line worries will be over.
Use a blank line,
Use two blank lines between the major sections of the source file: the package, the
import statement(s), the class, and the interface.
P:\010Comp\CertPrs8\360-6\CH12.vp
Saturday, November 26, 2005 2:47:31 PM
Color profile: Generic CMYK printer profile
CertPrs8(SUN) / Sun Certified
Composite Default screen Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 12
The Proper Use of Blank Spaces Blank spaces are used to make statements
more readable, and less squished together. Use a blank space,
The following code sample demonstrates proper form to use when indenting,
skipping lines, wrapping lines, and using spaces. We haven’t covered all of the
rules associated with the proper use of comments; therefore, this sample does not
demonstrate standard comments:
/*
* This listing demonstrates only proper spacing standards
*
* The Javadoc comments will be discussed in a later chapter
*/
package com.wickedlysmart.utilities;
import java.util.*;
/**
* CoolClass description
*
* @version .97 10 Oct 2002
* @author Joe Beets
*/
public class CoolClass {
P:\010Comp\CertPrs8\360-6\CH12.vp
Saturday, November 26, 2005 2:47:31 PM
Color profile: Generic CMYK printer profile
Composite DefaultCertPrs8(SUN)
screen / Sun Certified Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 12
P:\010Comp\CertPrs8\360-6\CH12.vp
Saturday, November 26, 2005 2:47:31 PM
Color profile: Generic CMYK printer profile
CertPrs8(SUN) / Sun Certified
Composite Default screen Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 12
topic for lots of folks; we’re just letting you know what your assessor will be looking
for, so please don’t attempt to drag from us what our real feelings are about
curly braces.
int id;
public Moe() {
id = 42;
}
static int x = 0;
P:\010Comp\CertPrs8\360-6\CH12.vp
Saturday, November 26, 2005 2:47:31 PM
Color profile: Generic CMYK printer profile
Composite DefaultCertPrs8(SUN)
screen / Sun Certified Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 12
static int y = 5;
if (x > 4) {
System.out.println("x > 4"); // if test
x++;
}
do { // do loop
x++;
System.out.println("in a do loop");
} while (x < 10);
P:\010Comp\CertPrs8\360-6\CH12.vp
Saturday, November 26, 2005 2:47:31 PM
Color profile: Generic CMYK printer profile
CertPrs8(SUN) / Sun Certified
Composite Default screen Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 12
case 14:
System.out.print("x is 14");
/* falls through */
default:
break;
}
} finally {
x = 100;
}
}
You might want those Exceptions above to be RuntimeExceptions. javac does not
mind, but jikes will give you a warning.
One interesting thing to notice about the example above is the use of the /* falls
through */ comment in the switch statement. This comment should be used at the
end of every case block that doesn’t contain a break statement.
P:\010Comp\CertPrs8\360-6\CH12.vp
Saturday, November 26, 2005 2:47:31 PM
Color profile: Generic CMYK printer profile
Composite DefaultCertPrs8(SUN)
screen / Sun Certified Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 12
This chapter will focus on implementation comments; Chapter 17 will cover javadoc
comments. There are several standard forms that your implementation comments
can take. Based on the results of extensive research and worldwide polling we will
recommend an approach, which we believe represents the most common of the
standard approaches. If you choose not to use our recommendation, the most
important thing that you can do is to pick a standard approach and stick with it.
There are several types of comments that commonly occur within source
code listings. We will discuss each of them with our recommendations and other
possible uses.
Block Comments
Use a block comment in your code when you have to describe aspects of your
program that require more than a single line. They can be used most anywhere,
as source file or method headers, within methods, or to describe key variables.
Typically, they should be preceded by a blank line and they should take the
following form:
/*
*this is a block comment
*it occupies several lines
*/
P:\010Comp\CertPrs8\360-6\CH12.vp
Saturday, November 26, 2005 2:47:32 PM
Color profile: Generic CMYK printer profile
CertPrs8(SUN) / Sun Certified
Composite Default screen Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 12
Masking Comments
Often in the course of developing software, you might want to mask a code segment
from the compiler without removing it from the file. This technique is useful during
development, but be sure to remove any such code segments from your code before
finishing your project. Masking comments should look like this:
// if (moreRecs == true) {
// ProcessRecord();
// }
// else {
// doFileCleanUp();
// }
P:\010Comp\CertPrs8\360-6\CH12.vp
Saturday, November 26, 2005 2:47:32 PM
Color profile: Generic CMYK printer profile
Composite DefaultCertPrs8(SUN)
screen / Sun Certified Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 12
guidelines is intended to make your code more readable, more debuggable, and
more maintainable.
■ class comments
■ package declaration
■ import statements
■ class declaration
■ static variables
■ instance variables
■ constructors
■ methods
■ Within methods:
■ Declare and initialize local variables before other statements (whenever
possible).
■ Declare and initialize block variables before other block statements
(when possible).
■ Declare only one member per line.
■ Avoid shadowing variables. This occurs when an instance variable has
the same name as a local or block variable. While the compiler will allow
it, shadowing is considered very unfriendly towards the next co-worker
(remember: potentially psychopathic) who has to maintain your code.
P:\010Comp\CertPrs8\360-6\CH12.vp
Saturday, November 26, 2005 2:47:32 PM
Color profile: Generic CMYK printer profile
CertPrs8(SUN) / Sun Certified
Composite Default screen Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 12
Capitalization
Three guesses. You better use capitalization correctly when you declare and use
your package, class, interface, method, variable, and constant names. The rules
are pretty simple:
■ Class and Interface names Typically they should be nouns; capitalize the
first letter and any other first letters in secondary words within the name:
Customer or CustomTable
■ Method names Typically they should be verbs; the first word should be
lowercase, and if there are secondary words, the first letter of each should
be capitalized:
initialize(); or getTelescopicOrientation();
P:\010Comp\CertPrs8\360-6\CH12.vp
Saturday, November 26, 2005 2:47:32 PM
Color profile: Generic CMYK printer profile
Composite DefaultCertPrs8(SUN)
screen / Sun Certified Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 12
P:\010Comp\CertPrs8\360-6\CH12.vp
Saturday, November 26, 2005 2:47:32 PM