Web site directory - Chapter 8 Object-Based Programming 285 Good Programming Practice
Chapter 8 Object-Based Programming 285 Good Programming Practice 8.2 Members in a class definition should be grouped by their member access modifiers to enhance clarity and readability. Lines 9 11 declare each of the three int instance variables hour, minute and second with member access modifier private, indicating that these instance variables of the class are accessible only to members of the class this is known as data hiding. When an object of the class encapsulates such instance variables, only methods of that object s class can access the variables. Normally, instance variables are declared private and methods are declared public. However, it is possible to have private methods and public instance variables, as we will see later. Often, private methods are called utility methods, or helper methods, because they can be called only by other methods of that class. The purpose of utility methods is to support the operation of a class s other methods. Using public data in a class is an uncommon and dangerous programming practice. Providing such access to data members is unsafe foreign code (i.e., code in other classes) could set public data members to invalid values, producing potentially disastrous results. Good Programming Practice 8.3 We prefer to list instance variables of a class first, so that, when reading the code, programmers see the name and type of each instance variable before it is used in the methods of the class. Good Programming Practice 8.4 Even though private and public members can be intermixed, list all the private members of a class first in one group, then list all the public members in another group. Software Engineering Observation 8.3 Declare all instance variables of a class as private. The architecture of accessing private data through public properties which first validate the data allows the developer to ensure that an object s data remains in a consistent state. Software Engineering Observation 8.4 Make a class member private if there is no reason for that member to be accessed outside of the class definition. Classes often include access methods that can read or display data. Another common use for access methods is to test the truth of conditions such methods often are called predicate methods. For example, we could design predicate method IsEmpty for a container class a class capable of holding many objects, such as a linked list, a stack or a queue. (These data structures are discussed in detail in Chapter 23, Data Structures.) IsEmpty would return true if the container is empty and false otherwise. A program might test IsEmpty before attempting to read another item from the container object. Similarly, a program might test another predicate method (e.g., IsFull) before attempting to insert an item into a container object. Class Time1 contains constructor Time1(lines 15 18) and methods SetTime (lines 22 32), ToUniversalString (lines 35 39) and ToStandardString (lines 42 47). These are the public methods (also called the public services or the public interface) of the class. Clients of class Time1, such as class TimeTest1 (Fig. 8.2), use a Time1 s public methods to manipulate the data stored in Time1 objects or to cause class Time1 to perform some service.
If you are searching for cheap webhost for your web application, please visit MySQL5 Web Hosting services.