Code Examples
  Home arrow Code Examples arrow Page 5 - Style Case Studies: Index Tables
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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? 
CODE EXAMPLES

Style Case Studies: Index Tables
By: Addison-Wesley/Prentice Hall PTR
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 14
    2004-09-01

    Table of Contents:
  • Style Case Studies: Index Tables
  • Solution to Item 34: Dissecting Index Tables
  • Correcting Mechanical Errors
  • Improving Style
  • Reuse and Preincrement
  • Summary

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


    Style Case Studies: Index Tables - Reuse and Preincrement


    (Page 5 of 6 )

    Guideline: Know about and (re)use the standard library’s facilities wherever appropriate instead of hand-rolling your own.

    11. Reuse Part 2: Kill two birds with one stone by making the implementation itself more reusable. Of the original implementation, nothing is directly reusable other than the function itself. The helper sort_idxtbl_pair class is hardwired for its purpose and is not independently reusable.

    12. Reuse Part 3: Improve the signature. The original signature

    template <class RAIter>
    void sort_idxtbl( RAIter first, RAIter last, int* pidxtbl )

    takes a bald int* pointer to the output area, which I generally try to avoid in favor of managed storage (say, a vector). But at the end of the day the user ought to be able to call sort_idxtbl and put the output into a plain array or a vector or anything else. Well, the requirement “be able to put the output into any container” simply cries out for an iterator, doesn’t it? (See also Items 5 and 6.)

    template< class RAIn, class Out >
    void sort_idxtbl( RAIn first, RAIn last, Out result )

    Guideline: Widen the reusability of your generic components by not hardcoding types that don’t need to be hardcoded.

    13. Reuse Part 4, or Prefer comparing iterators using !=: When comparing iterators, always use != (which works for all kinds of iterators) instead of < (which works only for random-access iterators), unless of course you really need to use < and only intend to support random-access iterators. The original program uses < to compare the iterators it’s given to work on, which is fine for random-access iterators, which was the program’s initial intent: to create indexes into vectors and arrays, both of which support random-access iteration. But there’s no reason we might not want to do exactly the same thing for other kinds of containers, like lists and sets, that don’t support random-access iteration, and the only reason the original code won’t work for such containers is that it uses < instead of != to compare iterators.

    As Scott Meyers puts it eloquently in Item 32 of [Meyers96], “Program in the future tense.” He elaborates:

    Good software adapts well to change. It accommodates new features, it ports to new platforms, it adjusts to new demands, it handles new inputs. Software this flexible, this robust, and this reliable does not come about by accident. It is designed and implemented by programmers who conform to the constraints of today while keeping in mind the probable needs of tomorrow. This kind of software—software that accepts change gracefully—is written by people who program in the future tense.

    Guideline: Prefer to compare iterators using !=, not <.

    14. Prefer preincrement unless you really need the old value. Here, for the iterators, writing preincrement (++i) should habitually be preferred over writing postincrement (i++); see [Sutter00, Item 39]. True, that might not make a material difference in the original code because vector<T>::iterator might be a cheap-to-copy T* (although it might not be, particularly on checked STL implementations), but if we implement point 13 we may no longer be limited to just vector<T>::iterators, and most other iterators are of class type—perhaps often still not too expensive to copy, but why introduce this possible inefficiency needlessly?

    Guideline: Prefer to write preincrement rather than postincrement, unless you really need to use the previous value.

    That covers most of the important issues. There are other things we could critique but that I didn’t consider important enough to call attention to here; for example, production code should have comments that document each class’s and function’s purpose and semantics, but that doesn’t apply to code accompanying magazine articles where the explanation is typically written in better English and in greater detail than code comments have any right to expect.

    I’m deliberately not critiquing the mainline for style (as opposed to the mechanical errors already noted that would cause the mainline to fail to compile), because, after all, this particular mainline is only meant to be a demonstration harness to help readers of the magazine article see how the index table apparatus is meant to work, and it’s the index table apparatus that’s the intended focus.

    This chapter is from Exceptional C++ Style, by Herb Sutter (ISBN 0201760428, copyright 2005. All rights reserved. It is reprinted with permission from Addison-Wesley Professional). Check it out at your favorite bookstore today.

    Buy this book now.

    More Code Examples Articles
    More By Addison-Wesley/Prentice Hall PTR


       · Although the article was about programming style, I was also very pleased to read...
     

    CODE EXAMPLES ARTICLES

    - Handling Animations and Bitmaps Using GDI+ f...
    - Download a Web Page using the WebClient
    - Creating a Chart using Data from a Database ...
    - The Basics of Charting with the MS Chart Con...
    - Searching Body Text with textRange: Enter th...
    - Searching Body Text with textRange: Building...
    - Searching Body Text with textRange, part 1: ...
    - First Steps in Programming
    - Programming in C
    - Quick Introduction to ASF,ASX, and Networkin...
    - SatView: Pointer Perfect, Part 2: Constructi...
    - SatView: Pointer Perfect, Part 1
    - Style Case Studies: Construction Unions
    - Creating an Engine for Games for Windows
    - Style Case Studies: Generic Callbacks

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT