Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Re: About Standards and Best Practices

From: rebol:techscribe at: 2-Sep-2003 9:06

To flesh out Andrew's reply a little: 1. Use objects if you want to process assemblies of values (records) that share the same structure uniformly, even though at times some of the values may be missing. The prototype object ensures that all data instances will have a complete set of fields that are initialized with some useful value (in our example we'll be generating reports, and therefore if a record is missing some data, we will want to display N/A in our report). Now, even though our records are deficient (see the two records examples below), they can be treated uniformly, i.e. without implementing exception handling for missing values: Example: person!: make object! [ first-name: "N/A" last-name: "N/A" phone: "N/A" email: "N/A" website: "N/A" address: "N/A" ] persons: make block! 1000 ;- deficient record: no email address or web site append persons make person! [ first-name: "Tom" last-name: "Smith" phone: "555-555-5555" address: "Main Street, Ukiah, CA 95482" ] ;- deficient record: no phone number or web site append persons make person! [ first-name: "Bill" last-name: "Jones" address: "Main Street, Ukiah, CA 95482" email: "[bjones--earthlink--net]" ] ;- generic display function display-list: func [field] [ foreach person persons [ print [ person/first-name person/last-name get in person field ] ] ] list-phone-numbers: does [ display-list 'phone ] list-email-addresses: does [ display-list 'email ] list-addresses: does [ display-list 'address ] list-phone-numbers list-email-addresses list-addresses 2. Use objects, when you have hierarchies of structures: animal!: make object! [ species: "N/A" extinct: "N/A"] dinosaur: make animal! [ species: "Mamal" extinct: "Yes" lived-from: 1965 lived-to: 1979 ] ;- must lookup those numbers! cat: make animal! [ species: "Mamal" extinct: "No" likes: "Milk, Mice, Fireplace" ] 3. Use objects to subcatgorize data: contact!: make object! [ name: "N/A" first-name: "N/A" phone: "N/A" address: "N/A" email: "N/A" website: "N/A" ] company!: make object! [ office: make contact! [] contact: make contact! [] ] rebol-tech: make company! [ office: make contact! [ ....] contact: make contact! [ .... ] ] That will enable you to look up office related data as rebol-tech/office/... and contact related data as rebol-tech/contact/... with both subcategories having the same structure, i.e. you can again implement a single set of functions to process both types of data. Andrew Martin wrote: