Development Cycles
  Home arrow Development Cycles arrow Page 3 - Thoughts on the Craft of Programming: Abst...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
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? 
DEVELOPMENT CYCLES

Thoughts on the Craft of Programming: Abstraction, Refactoring, and How Changes Introduce Bugs
By: The Rational Edge
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2003-08-13

    Table of Contents:
  • Thoughts on the Craft of Programming: Abstraction, Refactoring, and How Changes Introduce Bugs
  • Bugs
  • Cleaning Code
  • Vowels
  • An Aside on Programming by Contract
  • Lesson

  • 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


    Thoughts on the Craft of Programming: Abstraction, Refactoring, and How Changes Introduce Bugs - Cleaning Code


    (Page 3 of 6 )

    Cleaning Up the Switch Statement

    That fixed the bug, but the switch statement looks a bit silly now, and would look even sillier if I had stripped it to its bare essentials by also removing the default case and the preceding continue (which is made redundant by the removal of the default) -- even though, if compiled using a branch table, the switch statement would be quite efficient. In any case, I tend to look askance at continue statements3. Figure 3 shows how we can replace the switch with a more straightforward if statement.

     

    Figure 3: Cleaning Up the switch Statement Code

    Replace the Ugly if

    That's reasonable and not too inefficient, but the if statement looks a bit ugly. We now need to switch our focus from the scientific -- getting the code correct -- to the artistic -- making the code good. Let's replace the ugly statement with a nicer test, using the C run-time library: a tiny4 example of component-based development, with the library acting as a component.

    Usually, strchr is employed to search for a given character in an arbitrary string; here we invert that to search for an arbitrary character in a given string. Its specification is "strchr searches a string for the first occurrence of a character; if the character is found in the string, a pointer to the first occurrence is returned, otherwise a null pointer is returned." We use it in the logically equivalent way: The character occurs in the string if and only if strchr returns a nonzero value. On x86 CPUs with a decent compiler this version will also be a little faster. Notice that the total amount of text (Figure 4) is now substantially reduced, which also contributes to the code's being easier to read and understand.

    Figure 4: Replace the Ugly if Code

    Making a Simple Enhancement

    As shown in Figure 4, we might notice that only lowercase letters are counted as vowels. Let's improve the code so that it will accept uppercase vowels as well. Given the improvements we've made thus far, this is now very easy and safe to do, since the only change needed is to extend a static array. Note that if we had made an equivalent change to Version 3, the if statement would have become really ugly and significantly inefficient. The change shown in Figure 5 is by no means the only way to implement this enhancement; arguably it would be just as good, for example, to initialize the vowels array with only the uppercase vowels, and pass toupper(*s) to strchr.

     

    Figure 5: Enhancement to Accept Uppercase Vowels

    More Development Cycles Articles
    More By The Rational Edge


     

    DEVELOPMENT CYCLES ARTICLES

    - Branch and Bound Algorithm Technique
    - Dynamic Programming Algorithm Technique
    - Genetic Algorithm Techniques
    - Greedy Strategy as an Algorithm Technique
    - Divide and Conquer Algorithm Technique
    - The Backtracking Algorithm Technique
    - More Pattern Matching Algorithms: B-M
    - Pattern Matching Algorithms Demystified: KMP
    - Coding Standards
    - A Peek into the Future: Transactional Memory
    - Learning About the Graph Construct using Gam...
    - Learning About the Graph Construct using Gam...
    - Learning About the Graph Construct using Gam...
    - How to Strike a Match
    - Entity Relationship Modeling






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