Article Preview
Buy Now
| Print: | |
| PDF: |
Object-Oriented Thinking
Design Eye for the Programming Guy
Rethinking the PopupMenu Interface
Issue: 2.5 (May/June 2004)
Author: Charles Yeomans
Author Bio: Charles Yeomans is a software developer in Lexington, Kentucky.
Article Description: No description available.
Article Length (in bytes): 4,496
Starting Page Number: 35
RBD Number: 2517
Resource File(s): None
Related Link(s): None
Known Limitations: None
Excerpt of article text...
Let's consider a familiar example, the PopupMenu class. PopupMenu.ListIndex returns the index of the selected row, if one has been selected; otherwise it returns -1. Perhaps this seems like a reasonable, even natural design. I claim that it is not.
The problem is that PopupMenu.ListIndex is telling you two things; first, whether or not a menu item has been selected, and, second, the index of the selected row, if a row has been selected. It does this through the use of a magic value; that is, it returns -1 to signify that no row has been selected. The result is code like this:
If PopupMenu1.ListIndex > -1 then
DoSomethingWith PopupMenu1.ListIndex
Else //no row selected
DoSomethingElse()
End if
in which the meaning of PopupMenu.ListIndex is made clear only through a comment. Instead, I would prefer to be able to write
If PopupMenu1.Selected then
DoSomethingWith PopupMenu1.SelectedRow
Else
DoSomethingElse()
End if
To my eye, the second code snippet is entirely self-documenting, and thus clearer. When I look at the code later, I am not required to know that ListIndex = -1 means "no row selected".
...End of Excerpt. Please purchase the magazine to read the full article.
Article copyrighted by REALbasic Developer magazine. All rights reserved.
|








