Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: equality op...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 40 of 45 Topic 45751 of 46738
Post > Topic >>

Re: equality operator question

by James Kanze <james.kanze@[EMAIL PROTECTED] > May 7, 2008 at 01:53 AM

On May 6, 5:13 pm, Eberhard Schefold <eberhard.schef...@[EMAIL PROTECTED]
>
wrote:
> Stefan Ram wrote:
> >   That's why a sentence is a better name for a predicate than =BBa=AB.

> > if( server_is_up() )...

> >   Now, this is more natural-language like than

> > if( server_is_up() =3D=3D true )...

> Generally I certainly agree with Lionel and you. But it's not
> always that easy. Sometimes you don't have control over the
> naming, and even if you have, often the return value only
> indicates success or failure of a larger procedure with lots
> of side effects, say,

>     if( GenerateRevenueRe****ts() )
>        ...

> I wouldn't want to change that name into something more "boolean", say,

>     if( GenerateRevenueRe****tsWasSuccessful() )
>        ...

> It's not clear now if the function actually performs the
> action or only queries for its status. So, even with all the
> arguments, I still understand why some people might find

>     if( GenerateRevenueRe****ts() =3D=3D true )
>        ...

> clearer to understand.

It doesn't say anything more than just:
    if ( GenerateRevenueRe****ts() ) ...

I think it was Herb Sutter who first brought up the issue, but
there's already a serious problem if a function named
GenerateRevenueRe****ts() returns a bool to indicate success: is
it true, the function succeeded, or true, there was an error?
The correct way to handle this is with an enum:

    enum Result { success, failure } ;
    Result GenerateRevenueRe****ts() ;

In which case, of course, you write:

    if ( GenerateRevenueRe****ts() =3D=3D success )
        ...

My general rule is that function names are verbs or verbal
clauses, and that predicate functions always use a form of the
verb to be or to have, e.g.: isRunning(), hasNoProblems().  Any
other function will use an enum (or some other user defined
type---I rather like a user defined type which asserts in its
destructor if it hasn't been tested) to re****t success or
failure.

There are exceptions, of course.  The iostream idiom is too
ubiquious to be ignored, for example (and you can't change the
standard library even where it is completely irrational).  And
there are cases where it makes sense to treat an extended type
as a bool, or verbs other than to be or to have which logically
respond with a bool (e.g. RegularExpression::matches()).

--
James Kanze (GABI Software)             email:james.kanze@[EMAIL PROTECTED]
 en informatique orient=E9e objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
 




 45 Posts in Topic:
equality operator question
"barcaroller" &  2008-05-04 22:40:28 
Re: equality operator question
Jack Klein <jackklein@  2008-05-04 21:51:54 
Re: equality operator question
"Jim Langston"   2008-05-05 02:51:12 
Re: equality operator question
"kwikius" <a  2008-05-05 11:58:28 
Re: equality operator question
James Kanze <james.kan  2008-05-05 06:19:44 
Re: equality operator question
pjb@[EMAIL PROTECTED] (P  2008-05-05 15:28:16 
Re: equality operator question
James Kanze <james.kan  2008-05-05 07:12:07 
Re: equality operator question
Krice <paulkp@[EMAIL P  2008-05-05 07:48:06 
Re: equality operator question
pjb@[EMAIL PROTECTED] (P  2008-05-05 17:07:23 
Re: equality operator question
Eberhard Schefold <eba  2008-05-06 08:02:24 
Re: equality operator question
Pete Becker <pete@[EMA  2008-05-06 06:53:14 
Re: equality operator question
=?UTF-8?B?RXJpayBXaWtzdHL  2008-05-06 16:08:43 
Re: equality operator question
"kwikius" <a  2008-05-06 17:32:37 
Re: equality operator question
Pete Becker <pete@[EMA  2008-05-06 13:39:06 
Re: equality operator question
Eberhard Schefold <ebe  2008-05-06 19:46:31 
Re: equality operator question
"Bo Persson" &l  2008-05-06 20:00:21 
Re: equality operator question
Pete Becker <pete@[EMA  2008-05-06 15:18:44 
Re: equality operator question
=?UTF-8?B?RXJpayBXaWtzdHL  2008-05-06 18:17:40 
Re: equality operator question
ram@[EMAIL PROTECTED] (S  2008-05-07 22:02:04 
Re: equality operator question
Jerry Coffin <jcoffin@  2008-05-07 22:38:59 
Re: equality operator question
Pete Becker <pete@[EMA  2008-05-05 12:32:00 
Re: equality operator question
peter koch <peter.koch  2008-05-05 11:08:13 
Re: equality operator question
Krice <paulkp@[EMAIL P  2008-05-05 15:33:29 
Re: equality operator question
Pete Becker <pete@[EMA  2008-05-05 20:36:41 
Re: equality operator question
"barcaroller" &  2008-05-05 23:01:32 
Re: equality operator question
Christian Hackl <hacki  2008-05-06 09:34:51 
Re: equality operator question
Pete Becker <pete@[EMA  2008-05-06 06:49:33 
Re: equality operator question
Krice <paulkp@[EMAIL P  2008-05-05 23:58:01 
Re: equality operator question
Pete Becker <pete@[EMA  2008-05-06 06:52:13 
Re: equality operator question
James Kanze <james.kan  2008-05-06 03:04:22 
Re: equality operator question
Eberhard Schefold <ebe  2008-05-06 16:04:19 
Re: equality operator question
pjb@[EMAIL PROTECTED] (P  2008-05-06 16:23:15 
Re: equality operator question
Eberhard Schefold <ebe  2008-05-06 16:35:26 
Re: equality operator question
ram@[EMAIL PROTECTED] (S  2008-05-06 14:48:53 
Re: equality operator question
Eberhard Schefold <ebe  2008-05-06 17:13:01 
Re: equality operator question
James Kanze <james.kan  2008-05-06 03:09:18 
Re: equality operator question
James Kanze <james.kan  2008-05-06 05:06:53 
Re: equality operator question
Nick Keighley <nick_ke  2008-05-06 09:06:24 
Re: equality operator question
Krice <paulkp@[EMAIL P  2008-05-06 11:50:27 
Re: equality operator question
James Kanze <james.kan  2008-05-07 01:53:25 
Re: equality operator question
ram@[EMAIL PROTECTED] (S  2008-05-07 15:16:59 
Re: equality operator question
James Kanze <james.kan  2008-05-09 04:34:21 
Re: equality operator question
Lionel B <me@[EMAIL PR  2008-05-06 14:31:59 
Re: equality operator question
Eberhard Schefold <ebe  2008-05-06 16:36:25 
Re: equality operator question
Lionel B <me@[EMAIL PR  2008-05-07 09:08:00 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Jul 9 0:16:23 CDT 2008.