My.ADVISOR.com Sign-In
ID
Password

Member Center / Sign-Up
   
SUBSCRIPTION STATUS
If you are a subscriber to this publication, sign-in to access locked articles. To subscribe or renew go to www.AdvisorStore.com.
Go to Article

CODING

Reuse Snippets in Microsoft Visual FoxPro

Balance maintenance with performance.

  DOWNLOAD: Download the code snippets shown in this article.
Subscribers: Sign-in to activate download link
By Mike Yearwood

Code reuse has enormous benefits for maintenance and construction, but it presents developers with a trade-off between maintenance and performance. In this article, I'll present an approach that balances the two considerations.

When you need an expression (or formula) in many places in an application, there are too few choices for reusing it. You can copy it everywhere, but that makes maintenance and enhancement a problem. You can make a user-defined function (UDF) of it. That’s usually a good thing because you can document it. It also lets you refer to the formula by name, which can make the calling code easier to follow. But calling a UDF repeatedly can have a potentially serious performance impact. Some also argue that creating a UDF from an expression is overkill. Many developers opt to not reuse these bits of code, despite knowing how important reuse is.

A third option is to find some way to embed the expression where it's required. Formulas and expressions may not be the only thing inserted, so let's allow for something a little bigger and call them snippets.

A typical VFP expression

A pretty common need is to produce a string like "Yearwood, Mike". The formula for this is easy enough, but it must assume the table might be missing any combination of last and first name. It should also specify a length, especially when used in indexes and SQL commands. That length could vary as the formula is used in different parts of the application.

PADR(;
    ALLTRIM(tsf_Last) ;
    + IIF(EMPTY(ALLTRIM(tsf_Last)) ;
    + OR EMPTY(ALLTRIM(tsf_First)),'',', ');
    + ALLTRIM(tsf_First),50)

A typical SQL expression

The expression itself might vary depending on the database server. This is possible in SQL Server, but not in VFP:

 CAST(LTRIM(RTRIM(LAST_NAME)) +
  CASE
    WHEN
      LEN(LTRIM(RTRIM(LAST_NAME))) = 0  
      OR LEN(LTRIM(RTRIM(FIRST_NAME))) = 0
    THEN ''
    ELSE ', '
  END + LTRIM(RTRIM(FIRST_NAME)) AS CHAR(50))

That inline CASE statement qualifies as more than a simple formula or expression, hence the need for the term "snippet."

Mike Yearwood is the senior systems analyst and owner of Fox Ridge Software Inc., a Toronto-based developer of custom desktop and web-based software for various industries. Mike has more than 25 years of experience in IT and has been developing with Fox products since FoxBase version 1.02. mike.yearwood at foxridgesoftware.com

Printer-friendly
page layout

Reuse Snippets

No reader comments ... yet.

    What do YOU think about this topic? Share your advice and thoughts using this form.

    Your Name

    REQUIRED : PUBLIC

    Your E-Mail

    REQUIRED : PRIVATE

    Job, Company

    OPTIONAL : PUBLIC

    City, State, Country

    OPTIONAL : PUBLIC

    Your Web Site

    OPTIONAL : PUBLIC

    Your Comment

    Please help everyone by keeping your comments on-topic, using clean language, and not defaming or making personal attacks.


    Your e-mail address is required, but it will not be displayed to the public or given to anyone. See our Privacy Policy. Comments become visible after they pass our spam filter, and spammers and abusers are permanently blocked. Please report spam or abuse.

    ARTICLE INFO

    Web Edition: 2006 Week 04, Doc #17440

    FREE ACCESS FREE ACCESS

      DOWNLOAD: Download the code snippets shown in this article.
    Subscribers: Sign-in to activate download link

    Keyword Tags: Application Development, Development, Maintenance, Microsoft, Microsoft SQL Server, Microsoft Visual FoxPro, Microsoft Windows, performance, User-defined functions

    Use of this or any other site, content, product or service of Advisor Media constitutes acceptance of Terms of Use.
    Portions copyright ©1983-2010 Advisor Media, LLC. All Rights Reserved.
    Reuse or reproduction of any portion or quantity of Advisor Media's copyrighted content, in any form, for any purpose, requires written permission.
    ADVISOR®, the ADVISOR logo, and other names and logos that incorporate ADVISOR are registered trademarks, trademarks or service marks of Advisor Media, LLC in the United States and/or other countries.
    Other trademarks are used for identification, editorial or descriptive purposes and are the property of their owners.
    Hosted by Prominic.NET Website powered by
    LOTUS SOFTWARE
    ztmsvf0602 YEARM07 posted 2006-1-23 mod 03/15/2010 03:11:00 AM ztdbms/ztdbms
    domino-144.advisor.com my.advisor.com 03/22/2010 02:28:32 AM