hts.StevenWood.com

[ Last Page ]

Programming Observations

  1. 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.
  2. 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.
  3. 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.
  4. Methods tend to fall into a number of different categories:
    1. methods that get the values of private instance variables;
    2. methods that set the values of private instance variables; 
    3. methods that implement the services of the class; and 
    4. 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.
  5. Every time new creates an object of a class, that class' constructor is called to initialize the instance variables of the new object.
  6. 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.
  7. 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.
  8. 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.)
  9. 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.
  10. 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.
  11. 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.
  12. 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. 

[Last Page]
All material on this site is copyright © 1997- by Steven Wood or as credited. All right reserved. Use of this site indicates you agreement with the  terms of use.
Send comments or questions to about this page.
Every attempt has been made to credit work under copyright. If there are any claims that copyright has been missed please contact the .
+SDG+