Simplifying the if else statement I am trying to simplify the below if else statement, and I think it can be done.

Algorithm is like this - 


  If user_ptr exists, then lock attrmap, find user_ptr. And if it
  is a batch, then don't ping. And else if user_ptr doesn't exists
  and if it is not a batch, then do other stuff.  But if it is a
  batch and user_ptr doesn't exists, then don't do anything.


With the above algorithm, this is what I've came up with, but I'm not sure whether it can be simplified or not.

if (user_ptr) {

    if(batchOrTimestamp != "batch") {
        user_ptr-&gt;ping(); // dont ping it if its a batch update
    }

    // lock attribute map from outside access and modification
    AttributeMap::GuardSPtr guard = user_ptr-&gt;m_attrMap.get_guard();
    user_ptr-&gt;m_attrMap.find(column_key.c_str(), actual_binary_value, attributeLength, schemaId, lastModifiedDate);
} else if(!user_ptr &amp;&amp; batchOrTimestamp != "batch") {
    user_ptr = User::MakeUser(user_id);
    user_ptr-&gt;m_attrMap.find(column_key.c_str(), actual_binary_value, attributeLength, schemaId, lastModifiedDate);
    User::Find(user_ptr);
}


Any thoughts on this?
What you have is essentially:

if (a) {
   if (b) c();
   d();
}
else if (b) e();


Where:

a = user_ptr
b = batchOrTimestamp != "batch"
c = user_ptr-&gt;ping();
d = 2 lines, starting with `AttributeMap::GuardSPtr guard = user_ptr-&gt;m_attrMap.get_guard();`
e = 3 lines, starting with `user_ptr = User::MakeUser(user_id);`


One way to rewrite this is:

if (a &amp;&amp; b) c();
if (a) d();
else if (b) e();


And another way is:

if (b) {
   if (a) c();
   else e();
}
if (a) d();


Which of these ways you prefer is up to you. The problem is that you have a total of three branches. One for a, one for b &amp;&amp; !a and one for a &amp;&amp; b.

I don't believe it's possible to simplify this in a better way (I would gladly be proven wrong).