Article Preview
Buy Now
| Print: | |
| PDF: |
Object-Oriented Thinking
Mark the SPOT With Constants
Improve Your Code With No Effort
Issue: 5.5 (July/August 2007)
Author: Charles Yeomans
Article Description: No description available.
Article Length (in bytes): 4,191
Starting Page Number: 41
RBD Number: 5514
Resource File(s): None
Related Web Link(s):
http://www.declareSub.com/
Known Limitations: None
Excerpt of article text...
Using symbolic constants is probably the very simplest thing that you can do to improve your code. The use of constants also provides a simple illustration of a fundamental practice of good design.
Consider the following bit of code.
dim twoDaysLater as new Date
twoDaysLater.TotalSeconds = twoDaysLater.TotalSeconds + 172800.0
What does the literal 172800.0 represent? If you had written the code, perhaps you might remember. But I wrote the code, so you're reduced to asking me, or figuring it out from context; in this case, the name of the Date variable provides a clue.
You can save yourself and others the effort of divining the meaning of 172800.0 by using a symbolic constant.
dim twoDaysLater as new Date
const SecondsInTwoDays = 172800.0
twoDaysLater.TotalSeconds = twoDaysLater.TotalSeconds + SecondsInTwoDays
Now the meaning of the number is explicitly stated.
The compiler replaces all occurrences of the constant with its value, so there is no runtime cost for using symbolic constants.
...End of Excerpt. Please purchase the magazine to read the full article.
Article copyrighted by REALbasic Developer magazine. All rights reserved.
|








