Question

We have several classes where a call to a member function may change state and sometimes not depending on a boolean with default value.

 A a;
 a.set( "foobar" );
 assert( a.changed() == true );

versus

 A a;
 a.set( "foobar", false );
 assert( a.changed() == false );

Please note that those member functions are virtual. I would favor to use an alias to better read code and make a private member function still allowing the old method and give only the wrappers to the public:

 a.silentlySet( "foobar" ) {
    a.set( "foobar", false );
 }

I guess this would be inlined then anyway. I know I could write enums and constants which would also improve readability: a.set( "foobar", SILENTLY ); but I don't feel good with it, as the problem is of boolean nature (do or not). On the other side. However I would like to hear how you handle such a situation. (I assume I will get an "it depends" answer as I've certainly missed some crucial details) Live with it?

Those member functions are not very often called and used, but also not seldom. What I fear is that sometimes you may misinterpret a boolean as silent/not_silent-flag when this has another meaning.

An example where this could also be misleading:

if( b->getData( false ) && something_other )

this could be mixed up with b->getData() == false

Was it helpful?

Solution

but I don't feel good with [using an enum here], as the problem is of boolean nature (do or not)

No, an enum is perfect for this, use it. FWIW, there’s another discussion about this case on CodeReview.

That said, yes, the wrapper function will most likely be inlined. C++ is all about providing abstractions without runtime cost. Inlining function calls is a fundamental tool in this regard, so compilers use it where ever applicable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top