Special

Clearance Sale!

We've been publishing for over five years now and it's time to clear out our inventory of back issues, so we're slashing prices!

RBD Magazines

Check out this amazing clearance sale of all our past issues. Missing some issues? This is a great time to complete your RBD collection. Save up to 40% off the regular price of our printed back issue packages. These prices are only good until the end of the year May 2008 and supplies are limited, so place your order today.

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.


 


|

 


Weblog Commenting and Trackback by HaloScan.com