|
| |
Programming Observations
- It is important to write programs that are understandable and easy to
maintain. Change is the rule rather than the exception. Programmers should
anticipate that their code will be modified.
- Class definitions that begin with keyword public must be stored in
a file that has exactly the same name as the class and ends with the .java
file name extension.
- Every class defined in Java must extend another class. If a class does not
explicitly use keyword extends in its definition, the class
implicitly extends Object.
- Methods tend to fall into a number of different categories:
- methods that
get the values of private instance variables;
- methods that set the values of
private instance variables;
- methods that implement the services of the
class; and
- methods that perform various mechanical chores for the class,
such as initializing class objects, assigning class objects and converting
between classes and built-in types or between classes and other classes.
- Every time new creates an object of a class, that class'
constructor is called to initialize the instance variables of the new
object.
- Clients (or users) of a class can (and should) use the class without
knowing the internal details of how the class is implemented. If the class
implementation is changed (to improve performance, for example), provided
the class' interface remains constant, the class clients' sour code need not
change. This makes it much easier to modify systems.
- Keep all the instance variables of a class private. When necessary
provide public methods to set the values of private instance
variables and to get the values of private instance variables. This
helps hid the implementation of a class from its users, which reduces bugs
and improves program modifiability.
- Class designers user private data and public methods to
enforce the notion of information hiding and the principle of least
privilege. If the user of a class needs access to data in the class, provide
that access through public methods of the class. By doing so, the
programmer of the class controls how the class' data is manipulated
(ensuring data checking, etc.)
- If a method of a class already provides all or part of the job required by
the constructor (or other method) of the class, call that method from the
constructor. This simplifies the coding.
- The class designer need not provide set and/or get methods
for each private data member (e.g. instance variable), these should only be
provided when it makes sense and after careful thought by the class
designer.
- Methods that set the values of private data should verify that the
intended new values are proper; if they are not, the set methods
should place the private instance variables into an appropriate
consistent state.
- Declaring an instance variable as final helps enforce the principle
of least privilege. If an instance variable should not be modified declare
it final to expressly forbid modification.
Source: Deitel, H.M. and Deitel P.J. Java: How to Program. 3rd ed. Upper Saddle River, New
Jersey. Prentice Hall.1999.
ISBN 0-13-012507-5, p 378-9.
|