Java Reference
In-Depth Information
private String name;
}
The programmer declared a local variable
name
in the constructor. In all
likelihood, that was just a typoȌthe programmer's fingers were on autopilot and
typed the keyword
String
, even though the programmer all the time intended
to access the instance field. Unfortunately, the compiler gives no warning in this
situation and quietly sets the local variable to the value of
aName
. The instance
field of the object that is being constructed is never touched, and remains
null
.
Some programmers give all instance field names a special prefix to distinguish
them from other variables. A common convention is to prefix all instance field
names with the prefix
my
, such as
myValue
or
myName
.
361
362
P
RODUCTIVITY
H
INT
8.1: Global Search and Replace
Suppose you chose an unfortunate name for a methodȌsay
perc
instead of
percentOf
Ȍand you regret your choice. Of course, you can locate all
occurrences of
perc
in your code and replace them manually. However, most
programming editors have a command to search for the
perc's
automatically
and replace them with
percentOf
.
You need to specify some details about the search:
ȗ
Do you want it to ignore case? That is, should
Perc
be a match? In Java
you usually don't want that.
ȗ
Do you want it to match whole words only? If not, the
perc
in
superconductor
is also a match. In Java you usually want to match
whole words.
ȗ
Is this a regular-expression search? No, but regular expressions can make
searches even more powerfulȌsee
Productivity Hint 8.2
.
ȗ
Do you want to confirm each replace, or simply go ahead and replace all
matches? I usually confirm the first three or four, and when I see that it
works as expected, I give the go-ahead to replace the rest. (By the way, a
global replace means to replace all occurrences in the document.) Good
text editors can undo a global replace that has gone awry. Find out
whether yours will.