180 THE STATIC STRUCTURE:CLASSES $7.6 The discussion section of this chapter examines the rationale behind the Result See"Denoting the convention and compares it with other techniques such as return instructions.Although resulrofafimction". this convention addresses an issue that arises in all design and programming languages,it page 210. blends particularly well with the rest of the object-oriented approach. Style rules The class texts in this book follow precise style conventions regarding indentation,fonts (for typeset output),choice ofnames for features and classes,use of lower and upper case. The discussion will point out these conventions,under the heading "style rules",as we go along.They should not be dismissed as mere cosmetics:quality software requires consistency and attention to all details,of form as well as of content.The reusability goal makes these observations even more important,since it implies that software texts will have a long life,during which many people will need to understand and improve them. You should apply the style rules right from the time you start writing a class.For example you should never write a routine without immediately including its header comment.This does not take long,and is not wasted time;in fact it is time saved for all future work on the class,whether by you or by others,whether after half an hour or after half a decade.Using regular indentation,proper spelling for comments and identifiers, adequate lexical conventions-a space before each opening parenthesis but not after,and so on-does not make your task any longer than ignoring these rules,but compounded over months of work and heaps of software produces a tremendous difference.Attention to such details,although not sufficient,is a necessary condition for quality software(and quality,the general theme of this book,is what defines software engineering). The elementary style rules are clear from the preceding class example.Since our Chapter 26 is immediate goal is to explore the basic mechanisms of object technology,their precise devoted to style description will only appear in a later chapter. rules. Inheriting general-purpose facilities Another aspect of class POINT which requires clarification is the presence of calls to the sgrt function (in rho and distance).This function should clearly return the square root of a real number,but where does it come from? Since it does not seem appropriate to encumber a general-purpose language with specialized arithmetic operations,the best technique is to define such operations as features of some specialized class-say AR/THMET/C-and then simply require any class that needs these facilities to inherit from the specialized class.As will be seen in detail in a later chapter,it suffices then to write POINT as class POINTinherit ARITHMETIC feature The rest as before .. end
180 THE STATIC STRUCTURE: CLASSES §7.6 The discussion section of this chapter examines the rationale behind the Result convention and compares it with other techniques such as return instructions. Although this convention addresses an issue that arises in all design and programming languages, it blends particularly well with the rest of the object-oriented approach. Style rules The class texts in this book follow precise style conventions regarding indentation, fonts (for typeset output), choice of names for features and classes, use of lower and upper case. The discussion will point out these conventions, under the heading “style rules”, as we go along. They should not be dismissed as mere cosmetics: quality software requires consistency and attention to all details, of form as well as of content. The reusability goal makes these observations even more important, since it implies that software texts will have a long life, during which many people will need to understand and improve them. You should apply the style rules right from the time you start writing a class. For example you should never write a routine without immediately including its header comment. This does not take long, and is not wasted time; in fact it is time saved for all future work on the class, whether by you or by others, whether after half an hour or after half a decade. Using regular indentation, proper spelling for comments and identifiers, adequate lexical conventions — a space before each opening parenthesis but not after, and so on — does not make your task any longer than ignoring these rules, but compounded over months of work and heaps of software produces a tremendous difference. Attention to such details, although not sufficient, is a necessary condition for quality software (and quality, the general theme of this book, is what defines software engineering). The elementary style rules are clear from the preceding class example. Since our immediate goal is to explore the basic mechanisms of object technology, their precise description will only appear in a later chapter. Inheriting general-purpose facilities Another aspect of class POINT which requires clarification is the presence of calls to the sqrt function (in rho and distance). This function should clearly return the square root of a real number, but where does it come from? Since it does not seem appropriate to encumber a general-purpose language with specialized arithmetic operations, the best technique is to define such operations as features of some specialized class — say ARITHMETIC — and then simply require any class that needs these facilities to inherit from the specialized class. As will be seen in detail in a later chapter, it suffices then to write POINT as class POINT inherit ARITHMETIC feature … The rest as before … end See “Denoting the result of a function”, page 210. Chapter 26 is devoted to style rules
$7.7 THE OBJECT-ORIENTED STYLE OF COMPUTATION 181 See“FACILITY This technique ofinheriting general-purpose facilities is somewhat controversial;one can INHERITANCE”, argue that O-O principles suggest making a function such as sgrt a feature of the class 24.9,page847. representing the object to which it applies,for example RE4L.But there are many operations on real numbers,not all of which can be included in the class.Square root may be sufficiently fundamental to justify making it a feature of class RE4L;then we would write a.sqrt rather than sqrt(x).We will return,in the discussion of design principles,to the question of whether"facilities"classes such as AR/THMETIC are desirable. 7.7 THE OBJECT-ORIENTED STYLE OF COMPUTATION Let us now move to the fundamental properties of class POINT by trying to understand a typical routine body and its instructions,then studying how the class and its features may be used by other classes-clients. The current instance Here again is the text of one of our example routines,procedure translate: translate (a,b:REAL)is --Move by a horizontally,b vertically do x:=x+a y:=y+b end At first sight this text appears clear enough:to translate a point by a horizontally,b vertically,we add a to its x and b to its y.But if you look at it more carefully,it may not be so obvious anymore!Nowhere in the text have we stated what point we were talking about.To whose x and whose y are we adding a and b?In the answer to this question will lie one of the most distinctive aspects ofthe object-oriented development style.Before we are ready to discover that answer we must understand a few intermediate topics A class text describes the properties and behavior of objects of a certain type,points in this example.It does so by describing the properties and behavior of a typical instance of that type-what we could call the"point in the street"in the way newspapers report the opinion of the "man or woman in the street".We will settle for a more formal name: the current instance of the class. Once in a while,we may need to refer to the current instance explicitly.The reserved word Current will serve that purpose.In a class text,Current denotes the current instance of the enclosing class.As an example of when Current is needed,assume we rewrite distance so that it checks first whether the argument p is the same point as the current instance,in which case the result is 0 with no need for further computation.Then distance will appear as
§7.7 THE OBJECT-ORIENTED STYLE OF COMPUTATION 181 This technique of inheriting general-purpose facilities is somewhat controversial; one can argue that O-O principles suggest making a function such as sqrt a feature of the class representing the object to which it applies, for example REAL. But there are many operations on real numbers, not all of which can be included in the class. Square root may be sufficiently fundamental to justify making it a feature of class REAL; then we would write a ● sqrt rather than sqrt (x). We will return, in the discussion of design principles, to the question of whether “facilities” classes such as ARITHMETIC are desirable. 7.7 THE OBJECT-ORIENTED STYLE OF COMPUTATION Let us now move to the fundamental properties of class POINT by trying to understand a typical routine body and its instructions, then studying how the class and its features may be used by other classes — clients. The current instance Here again is the text of one of our example routines, procedure translate: translate (a, b: REAL) is -- Move by a horizontally, b vertically do x := x + a y := y + b end At first sight this text appears clear enough: to translate a point by a horizontally, b vertically, we add a to its x and b to its y. But if you look at it more carefully, it may not be so obvious anymore! Nowhere in the text have we stated what point we were talking about. To whose x and whose y are we adding a and b? In the answer to this question will lie one of the most distinctive aspects of the object-oriented development style. Before we are ready to discover that answer we must understand a few intermediate topics. A class text describes the properties and behavior of objects of a certain type, points in this example. It does so by describing the properties and behavior of a typical instance of that type — what we could call the “point in the street” in the way newspapers report the opinion of the “man or woman in the street”. We will settle for a more formal name: the current instance of the class. Once in a while, we may need to refer to the current instance explicitly. The reserved word Current will serve that purpose. In a class text, Current denotes the current instance of the enclosing class. As an example of when Current is needed, assume we rewrite distance so that it checks first whether the argument p is the same point as the current instance, in which case the result is 0 with no need for further computation. Then distance will appear as See “FACILITY INHERITANCE”, 24.9, page 847
182 THE STATIC STRUCTURE:CLASSES $7.7 distance (p:POINT):REAL is --Distance to p do if p/=Current then Result :sqrt ((x-px)2+(y-p.y)^2) end end (/=is the inequality operator.Because of the initialization rule mentioned above,the conditional instruction does not need an else part:if p=Current the result is zero.) In most circumstances,however,the current instance is implicit and we will not need to refer to Current by its name.For example,references to x in the body of translate and the other routines simply mean,if not further qualified:"thex of the current instance". This only pushes back the mystery,of course:"who"really is Current?The answer will come with the study of routine calls below.As long as we only look at the routine text, it will suffice to know that all operations are relative,by default,to an implicitly defined object,the current instance. Clients and suppliers Ignoring for a few moments the enigma of Current's identity,we know how to define simple classes.We must now study how to use their definitions.Such uses will be in other classes-since in a pure object-oriented approach every software element is part of some class text. There are only two ways to use a class such as POINT.One is to inherit from it;this Chapters 14 to 16 is studied in detail in later chapters.The other one is to become a client of PO/NT. study inheritance. The simplest and most common way to become a client of a class is to declare an entity of the corresponding type: Definition:client,supplier Let S be a class.A class C which contains a declaration of the form a:S is said to be a client of S.S is then said to be a supplier of C. In this definition,a may be an attribute or function of C,or a local entity or argument of a routine of C. For example,the declarations ofx,y,rho,theta and distance above make class POINT a client of REAL.Other classes may in turn become clients of PO/N7.Here is an example:
182 THE STATIC STRUCTURE: CLASSES §7.7 distance (p: POINT): REAL is -- Distance to p do if p /= Current then Result := sqrt ((x — p ● x) ^ 2 + (y — p ● y) ^ 2) end end (/= is the inequality operator. Because of the initialization rule mentioned above, the conditional instruction does not need an else part: if p = Current the result is zero.) In most circumstances, however, the current instance is implicit and we will not need to refer to Current by its name. For example, references to x in the body of translate and the other routines simply mean, if not further qualified: “the x of the current instance”. This only pushes back the mystery, of course: “who” really is Current? The answer will come with the study of routine calls below. As long as we only look at the routine text, it will suffice to know that all operations are relative, by default, to an implicitly defined object, the current instance. Clients and suppliers Ignoring for a few moments the enigma of Current’s identity, we know how to define simple classes. We must now study how to use their definitions. Such uses will be in other classes — since in a pure object-oriented approach every software element is part of some class text. There are only two ways to use a class such as POINT. One is to inherit from it; this is studied in detail in later chapters. The other one is to become a client of POINT. The simplest and most common way to become a client of a class is to declare an entity of the corresponding type: In this definition, a may be an attribute or function of C, or a local entity or argument of a routine of C. For example, the declarations of x, y, rho, theta and distance above make class POINT a client of REAL. Other classes may in turn become clients of POINT. Here is an example: Definition: client, supplier Let S be a class. A class C which contains a declaration of the form a: S is said to be a client of S. S is then said to be a supplier of C. Chapters 14 to 16 study inheritance