At the time you are writing a line of code, it's probably very clear
what it's doing. However, weeks or months later, it might not be
obvious anymore. Commenting your code helps but writing very clear code
in the first place is even better. The clearer the code is, the less
commenting is required. This tip will show a technique to make your
code clearer for one specific case.
Many functions in REALbasic return an object. GetOpenFolderItem is an
example of such a function. It returns a folderitem that represents the
file selected by the user in the Open File dialog box. If the user
clicks the Cancel button, GetOpenFolderItem returns NIL. Most of the
time you'll need to make sure that what was returned is not nil before
proceeding. The following code shows an example of this:
Dim f as folderitem
f = GetOpenFolderItem("????")
if f <> nil then
'continue processing
end if
Testing the value returned to determine if it's not nil works but it's
a double-negative. What you really want to know is whether or not
you've got a valid object. You can test for this using the IsA operator
and make your code easier to read at the same time. Here's an example
of the same code using the IsA operator:
Dim f as folderitem
f = GetOpenFolderItem("????")
if f IsA object then
'continue processing
end if
This works in all versions except v5.0. If you have v5.0, download the
latest release (v5.2.1) from our web site.
--
Geoff Perlman
President and CEO
REAL Software, Inc.
512-328-7325 x711 (voice)
512-328-7372 (fax)
- - - - - - - - - -
Got a useful tip to share? Send it to us at:
<REALbasic-tips at lists dot realsoftware dot com>
Click here to unsubscribe:
<http://support.realsoftware.com/listmanager/>
|