Java Reference
In-Depth Information
Generally, methods should be kept reasonably short and focused on one task.
If other tasks need to be done, it is possible that additional methods might be
needed, rather than cluttering a single method with too much code. It is impor-
tant to understand exactly what a given method needs to do in order to keep
the focus on this single task and to delegate ancillary tasks to other methods.
Keep Methods Short and Focused
Keep methods short and focused on a single task. In general, keep
the code to one or two printed pages, at a maximum. If you can
see all of the code in a method at once, it is easier to grasp what
it is doing.
Coding public Instance Methods
The two primary actions of the Password class are setting a new password
value as the current password and validating a password entered by a user as
current and not expired. These actions are implemented in the set() and
validate() methods, respectively. As
previously discussed, both the set()
method and validate() method must
be public to allow programs to access
their functionality.
The set() method sets a new
password value as the current value
of the Password object. Setting a new
password value is the essential action
when an existing password value is
changed or when a password initially is
created. This is why the set() method is
called in the constructors of the
Password class. Figure 9-29 displays the
pseudocode that outlines the logic for
the set() method.
As illustrated by the pseudocode
in Figure 9-29, before the set()
method establishes a new string value
as the new password, several other
actions are taken on the string value
entered by the user. First, the trim()
method of the String class is called to
remove any leading or trailing white
space, as the user may have entered
these spaces accidentally.
set() method
(no return data; parameter is String: new password)
Begin
remove leading and trailing white space from new password
verify format of new password
encrypt new password
If new encrypted password not in list
If list is at max size
Remove oldest element in list
End If
Add new encrypted password to end of list
If password not added
throw Exception: internal list error
End If
If password has expired
Reset password to not expired
End If
If password autoexpires
remaining uses = maximum uses
End If
Else
throw Exception: password recently used
End If
End
FIGURE 9-29
Search WWH ::




Custom Search