tags:

views:

68

answers:

3

i created one flag Method1HasExecuted ,,the logic which i wanted to achieve is if that flag true i need to do this function rp = Spa.Modify(val); otherwise return rp as false.For that i did like this

        if (Method1HasExecuted)
        {
            rp = Spa.Modify(val);//Save operation, this function also return boolean value
        }
        else rp = false;

Then for optimization i used ternary operator and did like this

return Method1HasExecuted ? Spa.ClientModify() : false;

But by doing this way down side i saw some unreachable code which was already there like this

1.how to make it reachable or here using ternary operator is not required?

2.After catch block we can see return retval; on bottom is that required since the outcome of all work is either true which will do the Save operation or false means display serverdown,which already there in my work

for second question the whole structure is like this now

+1  A: 

Your problem is that you're using the RETURN..

you should do:

rp = Method1HasExecuted ? Spa.ClientModify() : false;

and the rest should be the same..

remember.. return will just return :)

MindFold
+1  A: 

You translated an if-else statement into a ternary operator, but you also added a return statement. The problem is that the return statement will always return, so if you have code below that in that method, it will never get executed.

You can keep the ternary operator, just don't return:

rp = Method1HasExecuted ? Spa.ClientModify() : false;

The ternary operator will produce IL very similar to the if-else statement you had before. It will not be materially faster, so only use it if you think it improves maintainability or readability of the code.

Chris Schmich
While I agree that this is not an optimization the IL will not be the same. if is a statement and the ternary operator is an expression. This will make the IL different if only for the pop instruction needed to balance the stack.
Stilgar
Yea thats great,,then what about the second question if false situation is already there in rp = Method1HasExecuted ? Spa.ClientModify() : false; is that required Down or thats related to Catch exception block
peter
@peter: I imagine that your method is still expected to return something, so the `return retval;` is still required. The `if (rp) ...` logic should probably stay too since it is presumably setting some state in the class `databaseLocked` and writing some diagnostic message.
Chris Schmich
ternary will increase the attraction,,more look and feel rite ?
peter
@Stilgar: fair point, I was being too broad. There are certainly some cases where the IL will be identical, however. I've updated my answer.
Chris Schmich
I have edited my question
peter
@peter: This still appears to be just a portion of that method. I can't say from just that if `return retval;` is still required. Ask yourself this: what is your method expected to return? In what cases should it return those values? Does `return retval;` actually work in those cases?
Chris Schmich
@Stilgar: nice name, by the way :)
Chris Schmich
yeaa,,readability is the factor
peter
+1  A: 

Actually, you should do:

rp = Method1HasExecuted && Spa.ClientModify();