Indicating status of methods


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Indicating status of methods

  1. #1
    Jim Munn Guest

    Indicating status of methods


    Tim;

    Your book looks like just the sort of thing I have been looking for. I have
    a fair amount of experience in FoxPro/Visual FoxPro, and am working on getting
    up to speed in VB and ASP.

    One thing I'm curious about is when to raise errors vs returning some sort
    of status. For example, in a Windows DNA app, I have a VB COM object with
    an AddUser method. The add user method needs to confirm that the new user's
    email address is unique in the system, add the user, and return the user's
    generated integer ID. How should that method call indicate that a duplicate
    email was found? Is it preferred to raise and error, or return a status value?
    i.e.

    'returns new user ID
    Public function AddUser( ByVal strEmail as String ) as Integer
    'code code code
    'duplicate email found
    err.raise ERR_DUPLICATE_EMAIL

    or

    'returns status integer value, user ID set by reference param
    Public function AddUser( ByVal strEmail as String, _
    ByRef intID as Integer) as Integer (or an enumerated type)
    'code code code
    'duplicate email found
    AddUser = STATUS_DUPLICATE_EMAIL

    Which method is going to make it easier for someone calling the method to
    be able to deal with it? Which will work better in an MTS environment? Is
    this the sort of thing that should be done consistently throughout an application?
    (I assume it should, I'm just trying to figure out the best approach)

    Thanks;

    Jim
    Share on Google+

  2. #2
    Tim Patrick Guest

    Re: Indicating status of methods


    Generally, when you are writing a routine that is private to a class or to
    an application, it is fine to return a result code indicating the status.
    However, if you are writing a component with methods called from outside
    of the component, you should raise errors (and document those error codes).
    The goal in a component is to completely separate the inside of the component
    from the outside world. Raising error codes helps accomplish this.

    You should get into this habit now because Next Generation Windows Services
    (NGWS), which works hand-in-hand with VB.NET, requires that errors are reported
    by raising them, not by returning status codes. This makes error reporting
    consistent.

    It is acceptable to return a status code from a method if it is not really
    reporting an error. For example, let's say you write a method that returns
    the full name of a state when you pass it the two letter abbreviation of
    the state.

    sFullName = objGeog.GetStateName("MT") ' Returns "Montana"

    It is valid to document this routine so that it returns the string "???"
    when an unknown state abbreviation is passed. Or, you could have it raise
    an error. If you are developing such a component for a single project, and
    one of these styles meets your needs, you can use either one. Just be sure
    to document the way in which invalid results or errors are processed.

    In the example you give, that of adding a user to a set, it is probably best
    to raise the error, since not adding a user is a big deal, much more than
    returning "???" for a state name.

    -----
    Tim Patrick
    tim@vbstyleguide

    Share on Google+

  3. #3
    Jim Munn Guest

    Re: Indicating status of methods


    Tim;

    Thanks for the reply. This is the best explanation I've gotten on this topic
    so far. I've been asking around on Compuserve, universalthread.com and some
    newsgroups, but haven't gotten many good responses. BTW, I'm gonna go order
    your book now. Hopefully its full of more good stuff like this.

    Jim Munn, MCP
    Visual Data Solutions


    "Tim Patrick" <tim@vbstyleguide.com> wrote:
    >
    >Generally, when you are writing a routine that is private to a class or

    to
    >an application, it is fine to return a result code indicating the status.
    > However, if you are writing a component with methods called from outside
    >of the component, you should raise errors (and document those error codes).
    > The goal in a component is to completely separate the inside of the component
    >from the outside world. Raising error codes helps accomplish this.
    >
    >You should get into this habit now because Next Generation Windows Services
    >(NGWS), which works hand-in-hand with VB.NET, requires that errors are reported
    >by raising them, not by returning status codes. This makes error reporting
    >consistent.
    >
    >It is acceptable to return a status code from a method if it is not really
    >reporting an error. For example, let's say you write a method that returns
    >the full name of a state when you pass it the two letter abbreviation of
    >the state.
    >
    > sFullName = objGeog.GetStateName("MT") ' Returns "Montana"
    >
    >It is valid to document this routine so that it returns the string "???"
    >when an unknown state abbreviation is passed. Or, you could have it raise
    >an error. If you are developing such a component for a single project,

    and
    >one of these styles meets your needs, you can use either one. Just be sure
    >to document the way in which invalid results or errors are processed.
    >
    >In the example you give, that of adding a user to a set, it is probably

    best
    >to raise the error, since not adding a user is a big deal, much more than
    >returning "???" for a state name.
    >
    >-----
    >Tim Patrick
    >tim@vbstyleguide
    >


    Share on Google+

Similar Threads

  1. Comparison methods
    By ericelysia1 in forum Java
    Replies: 34
    Last Post: 05-15-2005, 06:39 PM
  2. Too many methods problem
    By blp in forum Java
    Replies: 2
    Last Post: 05-03-2005, 05:52 AM
  3. How to model "virtual" attributes and methods in UML
    By Larry Hayashi in forum Architecture and Design
    Replies: 2
    Last Post: 05-01-2003, 11:33 AM
  4. Hiding methods from COM
    By Frank Oquendo in forum .NET
    Replies: 0
    Last Post: 07-24-2002, 01:41 PM
  5. Free/busy Time Status Not Updated
    By nhooling in forum Enterprise
    Replies: 0
    Last Post: 10-16-2001, 05:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center