Chapter 6 Methods 255 (such as g.drawLine( x1,
Friday, November 30th, 2007Chapter 6 Methods 255 (such as g.drawLine( x1, y1, x2, y2 )), and a class name followed by a method name (such as Integer.parseInt(stringToConvert)). The last syntax is only for static methods of a class (discussed in detail in Chapter
. Common Programming Error 6.3 Omitting the return-value-type in a method definition is a syntax error. Common Programming Error 6.4 Forgetting to return a value from a method that should return a value is a syntax error. If a return value type other than void is specified, the method must contain a return statement. Common Programming Error 6.5 Returning a value from a method whose return type has been declared void is a syntax error. Common Programming Error 6.6 Declaring method parameters of the same type as float x, y instead of float x, float y is a syntax error, because types are required for each parameter in the parameter list. Common Programming Error 6.7 Placing a semicolon after the right parenthesis enclosing the parameter list of a method definition is a syntax error. Common Programming Error 6.8 Redefining a method parameter in the method s body is a syntax error. Common Programming Error 6.9 Passing to a method an argument that is not compatible with the corresponding parameter s type is a syntax error. Common Programming Error 6.10 Defining a method inside another method is a syntax error. Good Programming Practice 6.3 Avoid using the same names for instance variables and local variables. This helps readers of your program distinguish variables used in different parts of a class definition. Good Programming Practice 6.4 Choosing meaningful method names and meaningful parameter names makes programs more readable and helps avoid excessive use of comments. Software Engineering Observation 6.5 A method should usually be no longer than one page. Better yet, a method should usually be no longer than half a page. Regardless of how long a method is, it should perform one task well. Small methods promote software reusability. Software Engineering Observation 6.6 Programs should be written as collections of small methods. This makes programs easier to write, debug, maintain and modify. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01