c# - How can i identify which condition failed in a if statement with multiple OR conditions? -


how can identify condition failed in if statement multiple or conditions.example following.

if ((null == emailnotificationdata || string.isnullorempty(emailnotificationdata.sender))                         || null == emailnotificationdata.torecipients)   {     logprovider.log(typeof(notification), loglevel.error, "error sending email notification 'here want log failed argument'");     return;   } 

you can't, without rechecking each condition. i'd write as:

if (emailnotificationdata == null) {     // helper method calling logprovider.log(...)     logemailnotificationerror("no email notification data");     return; } if (string.isnullorempty(emailnotificationdata.sender)) {     logemailnotificationerror("no sender");     return; } if (emailnotificationdata.torecipients == null) {     logemailnotificationerror("no recipients");     return; } 

you extract validateandlog extension method on notification data type though - making extension method means can handle being null, too:

// validateandlog returns true if okay, false otherwise. if (!emailnotificationdata.validateandlog()) {     return; } 

that way doesn't need clutter other code.

note there's never benefit in c# writing:

if (null == x) 

... unless you're comparing boolean values, "normal" reason preferring constant-first comparison (catching typo of = ==) doesn't apply, if (x = null) wouldn't compile anyway.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -