Results 1 to 3 of 3

Thread: Best way to write a function to do something like this

  1. #1
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 04
    Location
    CT
    Posts
    14,416

    Best way to write a function to do something like this

    I am not totally familiar with all the different syntax constructs for doing something like this - I started with the SWITCH version

    Code:
    	bool isPunc(int &cc) {
    		switch (cc) {
    			case cDash:
    				return true;
    				break;
    			case cPeriod:
    				return true;
    				break;
    			case cSpace:
    				return true;
    				break;
    			default:
    				return false;
    				break;
    		}
    	}
    First question - those BREAK; statements aren't really needed - or maybe it just isn't a good practice to do RETURN's from CASE statements?

    At any rate - I guess I could have just declare a BOOL and set it as TRUE in each CASE - then simply return that BOOL...

    But then I thought of doing it this way.

    Code:
    	bool isPunc(int &cc) {
    		if (cc == cDash) {
    			return true;
    		} else if (cc == cPeriod) {
    			return true;
    		} else if (cc == cSpace) {
    			return true;
    		} else {
    			return false;
    		}
    	}
    Opinions?

    Better methods??

    Other C++ syntax that I'm not aware of that is better suited for this type of simple check??

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 02
    Posts
    21,655

    Re: Best way to write a function to do something like this

    Personally I'd use the switch statement... but I'd reduce it down some and use the one entry, one exit methodology:
    Code:
    	bool isPunc(int &cc) {
               bool retVal;
    		switch (cc) {
    			case cDash:
    			case cPeriod:
    			case cSpace:
    				retVal = true;
    				break;
    			default:
    				retVal = false;
    		}
                    return retVal;
    	}
    So now if it's a Dash, Period or a Space, it'll set retVal to true... then break out of the switch.... if it's anything else, it drops into the default case and sets retVal to false. Then the last thing to do is return the result.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    *Proof positive that searching the forums does work: View Thread *
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *
    * Use Offensive Programming, not Defensive Programming. * On Error Resume Next is error ignoring, not error handling(tm).
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 04
    Location
    CT
    Posts
    14,416

    Re: Best way to write a function to do something like this

    Thank you so much - I didn't know that syntax was allowed - it's easy on the eyes - easy to maintain - thanks!

    I guess CASE without BREAK makes sense as a "drop through" - I always wondered what situations would lead to someone using a CASE without BREAKS - and this is one!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •