Java Reference
In-Depth Information
of attributes with local variables plague us. Or we might decide to take a more
extreme stance and revise it based on program readability.
Reserved names
Java does allow us to reuse package and class names from the standard Java
packages and objects, although Sun strongly discourages this practice. It's easy
to forget about reserved names. For instance, when we were building the EJB
examples for this topic, we originally chose the names of common bulletin
board objects using the language common to that domain: posts, boards, and
threads. Of course, a thread is also a Java construct. If we were to import both
bbs.thread and java.lang.* , it wouldn't be clear which thread we would
instantiate with this:
Thread = new Thread();
Braces and indentation
Few coding standards prompt as much controversy as the treatment of braces
and indentation. This is probably another area where it is best to pick a stan-
dard and stick with it, but some rules should be established more strongly
than others. Consider the following code fragment. Assume some of our pre-
mium customers are not charged for shipping.
if (purchaseOrder.isChargedForShipping())
totalCost = totalCost+purchaseOrder.addShipping();
Now, let's assume that we want to add shipping surcharges for items under
$10. We simply add the condition like this:
if (purchaseOrder.isChargedForShipping())
totalCost = totalCost+purchaseOrder.addShipping();
if (totalCost < 10)
totalCost = totalCost+SMALL _ ORDER _ SUR _ CHARGE;
We have just accidentally charged our premium customers. For this reason, it's
best to bracket all if , while , and for statements with {} whether or not more
than one line of code is used:
if (purchaseOrder.isChargedForShipping()) {
totalCost = totalCost+purchaseOrder.addShipping();
You can avoid this error, and others like it, by using a good editor that auto-
indents. We cannot know which editor or environment future owners of this
code are likely to prefer, so it's best to be safe.
Search WWH ::

Custom Search