Java programming pitfalls – Apple WebObjects 3.5 User Manual

Page 61

Advertising
background image

Programming Pitfalls to Avoid

61

WebScript supports only objects that inherit from NSObject. As most
objects inherit from NSObject, this limitation is easy to overlook.
Notably, EOFault does not inherit from NSObject, so you cannot use it
in WebScript code.

The == operator is supported only for NSNumber objects. If you use
== to compare two objects of any other class, the operator compares the
addresses of the two objects, not the values of the two objects. To test
the equality of two objects, use the

isEqual:

method.

NSString *string1, *string2;

// WRONG!
if (aString1 == aString2) ...

// Right
if ([aString1 isEqual:string2]) ...

The postincrement and postdecrement operators are not supported. If
you use them, you won’t receive an error message. Instead, they behave
like preincrement and predecrement operators.

i = 0;
if (i++ < 1 )

// This code never gets executed.

WebScript always evaluates both sides of a Boolean expression (such as
&& and ||). You should make sure that the second half of an expression
does not produce an error.

// WRONG! produces a divide by 0 if a is 0.
if ((a == 0) || (b / a) > 5) ...

For more information, see the chapter “The WebScript Language” (page
163).

Java Programming Pitfalls

When debugging Java code, watch out for the following tricky spots:

You can’t define multiple constructors or overloaded methods for the
classes WebApplication, WebSession, Component, or any other class
that originates as an Objective-C class. For example, the following code
causes your application to crash:

public class MyComponent extends Component {

public void myMethod() { .... }

//WRONG! Overloaded method causes runtime error.
public void myMethod(int anInt) { ... }

}

Advertising