Only one point of return

J

Jerry Coffin

* (e-mail address removed):

[ ... ]
Bad, because it uses global variables, and is unclear.

Are you really want it to do one of the things without doing both?

I think you're right that the usual intent is more like this:

bool f() {

if ( pointer1 == 0 || pointer2 == 0)
return false;

pointer1->doSomething();
pointer2->doSomething();
return true;
}
But if so, try

bool f()
{
if( pointer1 != 0 )
{
pointer1->doSomething();
if( pointer1 != 0 )

Presumably you meant pointer2 here.
{
pointer2->doSomething1();
return true;
}
}
return false;
}

Personally, I think I'd generally prefer something like this:

bool f() {
if (pointer1 != 0) {
pointer1->doSomething();
if (pointer2 != 0)
pointer2->doSomething();
}
return (pointer1 != 0) && (pointer2 != 0);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top