Practices
  Home arrow Practices arrow Page 3 - Finishing the System`s Outlines
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PRACTICES

Finishing the System`s Outlines
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 1
    2008-05-22


    Table of Contents:
  • Finishing the System`s Outlines
  • Abstracting
  • Not Just a String
  • Constant Avoidance
  • Prototypes Are Worth a Thousand Words

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Finishing the System`s Outlines - Not Just a String
    ( Page 3 of 5 )

    A more descriptive data type can represent many data types represented typically by a String. For example, although a name can be declared as a String, you could declare it as a

    String . For example, although a name can be declared as a String , you could declare it as a A more descriptive data type can represent many data types represented typically by a String. For example, although a name can be declared as a String, you could declare it as a clumped data object:

      class Name
          {
          String first_name;
          String last_name;
          String title;        // e.g. Mr. Mrs.
          String suffix;    // e.g. Jr. III
          };

    To avoid the "everything is a String" syndrome, come up with a different type name to describe a variable that holds a set of characters that does not have any validation, formatting, or other meta-information associated with it. Suppose you decide on CommonString. Use that name in place of String to declare the data types of attributes, and reserve String as an implementation type. Then ask the question "Is that attribute really a CommonString?

    Let us revisit the Address class. Using CommonString, we can describe the class as follows:

      class Address
          {
          CommonString line1;
          CommonString line2;
          CommonString city;
          CommonString state;
          CommonString zipcode;
          }

    CommonStrings can contain any characters, just like an int can contain any integer values (within hardware limits). In Address, some fields are definitely not CommonStrings. A state is not a CommonString. Only certain values represent a valid state. For U.S. postal addresses, if we use abbreviations to represent the state, the abbreviation must appear on the U.S.

    Postal Service's official list of abbreviations. So the state should be declared as a data type called State. This data type can provide an appropriate validation mechanism. That mechanism can check to see that a string is in the official list, or it can supply a set of strings of all official abbreviations for use in a drop-down display box.

    A U.S. Zip Code is not just a CommonString either. You can describe it as a NumericString data type (e.g., one with all digits), as a FormattedString data type (one with five digits plus a dash plus four digits), or as a ZipCode data type. If any combination of digits was valid, using NumericString or FormattedString might be appropriate. However, declaring the attribute as a ZipCode type allows us to abstract away its actual representation.

    Do not combine classes simply for implementation purposes. You can define both SocialSecurityNumbers and PhoneNumbers as strings of digits with two dashes. That does not make them equivalent--that is just accidental cohesion. They are two distinctly different classes with different validation. A phone number in the U.S. cannot begin with a 1 or a 0. Certain ranges of Social Security numbers are not used. These numbers can use the same type of formatted string for input or display purposes, but the semantics of each class are entirely different. You would never send a Social Security number to be dialed, nor would you attempt to record payroll taxes against a phone number. (All right, someone at some time will come up with a counterexample, so perhaps I should never say "never.")

    Much data that might be a CommonString can be assigned its own data type. For example, filenames are usually typed as strings, but they cannot contain certain characters. In the Windows world, you cannot have any of the following characters in a filename: \,/,:,*,",?,<, >, or |. A FileName data type can represent a filename and enforce this limitation. An advantage of using a data type becomes apparent in graphical user interface (GUI) development. For instance, the user interface code could recognize the FileName data type and automatically insert a browse button next to a text field.

    On the Web, parameter values for the Hypertext Transfer Protocol (HTTP) commands GET and PUT use encoded strings. Characters that are not alphabetic or numeric are encoded using their hexadecimal values. The encoded string is sent to the web server. Although the unencoded string and the encoded string are both implemented with strings, they are different. You can have invalid encoded strings--ones with unencoded punctuation, such as a hacker might send to a server. You could use an EncodedWebString class to represent strings on a server. If an input were not validated as an EncodedWebString, it would be rejected.

    MOST STRINGS ARE MORE THAN JUST A STRING

    Treat String as a primitive data type. Describe attributes with abstract data types, instead of as Strings.



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

       

    PRACTICES ARTICLES

    - 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
    - Basic Ideas





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek