Home arrow Practices arrow Page 4 - Finishing the System`s Outlines

Constant Avoidance - Practices

This second part of a two-part article completes our coverage of how to talk to a client so that you are both on the same page when designing a system and understanding what it will be required to do. It is excerpted from Prefactoring, written by Ken Pugh (O'Reilly; ISBN: 596008740). Copyright 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

TABLE OF CONTENTS:
  1. Finishing the System`s Outlines
  2. Abstracting
  3. Not Just a String
  4. Constant Avoidance
  5. Prototypes Are Worth a Thousand Words
By: O'Reilly Media
Rating: starstarstarstarstar / 2
May 22, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Similar to the way in which most strings are more than strings, most constant values are more than just constants. A constant value can usually be assigned a name that denotes the meaning of that constant. Avoid using the explicit value in a specification or executable code.* Declare the value as a constant and use the name of that constant in the document or the code. 

If Sam mentions that the late fee for a rental is $3, I create a constant:

  Dollar RENTAL_LATE_FEE = 3.00;

When reading the relevant documents later on, I need not concentrate on the actual value, only on the assigned name. Suppose this value was not transformed into a constant and the value of 3.00 was used frequently in the documents for other purposes. If I went searching for it, I would have to examine each appearance carefully to see if it was a reference to the rental late fee or to some other value.

You might not get rid of every constant value. The value 0 often appears in initializing variables or setting the initial index for an array. There is little to be gained by creating a named constant for zero.

If the value that a name represents is subject to change, the value should be kept with a configuration mechanism. In that case, the code would use the symbolic name to look up the configured value. The configuration mechanism could use an XML configuration file, a database table, or another form of persistence to store the values. For example, RENTAL_ LATE_FEE is probably something that should exist in a configuration file rather than a con-

NEVER LET A CONSTANT SLIP INTO CODE

Use a symbolic name for all values.



 
 
>>> More Practices Articles          >>> More By O'Reilly Media
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PRACTICES ARTICLES

- Calculating Development Project Costs
- More Techniques for Finding Things
- Finding Things
- Finishing the System`s Outlines
- The System in So Many Words
- Basic Data Types and Calculations
- What`s the Address? Pointers
- Design with ArgoUML
- Pragmatic Guidelines: Diagrams That Work
- Five-Step UML: OOAD for Short Attention Span...
- Five-Step UML: OOAD for Short Attention Span...
- Introducing UML: Object-Oriented Analysis an...
- Class and Object Diagrams
- Class Relationships
- Classes

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: