I have an if statement that becoming a bit cumbersome. I want to know if there's a better way of going about multiple similar if statements such as combining into one or using a different conditional statement such as a while or do loop. Any suggestions are appreciated.
if (options.OpenCloseOverridesOptions != null && !options.OpenCloseOverridesOptions.AreEqual(OpenCloseOverridesOptions))
return false;
if (options.DeliveryOpenCloseOverridesOptions != null && !options.DeliveryOpenCloseOverridesOptions.AreEqual(DeliveryOpenCloseOverridesOptions))
return false;
if (options.PickupOpenCloseOverridesOptions != null && !options.PickupOpenCloseOverridesOptions.AreEqual(PickupOpenCloseOverridesOptions))
return false;
if (options.PickupServiceWindowOverridesOptions != null && !options.PickupServiceWindowOverridesOptions.AreEqual(PickupServiceWindowOverridesOptions))
return false;
if (options.DeliveryServiceWindowOverridesOptions != null && !options.DeliveryServiceWindowOverridesOptions.AreEqual(DeliveryServiceWindowOverridesOptions))
return false;
if (options.ServiceWindowOverridesOptions != null && !options.ServiceWindowOverridesOptions.AreEqual(ServiceWindowOverridesOptions))
return false;
if (options.LineItemsOptions != null && !options.LineItemsOptions.AreEqual(LineItemsOptions))
return false;
the rundown is I am basically checking if an object is null, if not use an extension method to determine if a similar object is equal. (I'm not overriding isEquals and getHashCode ). if the object is null I cannot call the areEquals extension method so that check is necessary.
If you want to return bool you can return the condition directly.
We can use another skill (De Morgan's laws) let! into the statement, that will reverse all logic, let the code more clear.
return
(options.OpenCloseOverridesOptions == null || options.OpenCloseOverridesOptions.AreEqual(OpenCloseOverridesOptions)) &&
(options.DeliveryOpenCloseOverridesOptions == null || options.DeliveryOpenCloseOverridesOptions.AreEqual(DeliveryOpenCloseOverridesOptions)) &&
(options.PickupOpenCloseOverridesOptions == null || options.PickupOpenCloseOverridesOptions.AreEqual(PickupOpenCloseOverridesOptions))&&
(options.PickupServiceWindowOverridesOptions == null || options.PickupServiceWindowOverridesOptions.AreEqual(PickupServiceWindowOverridesOptions) &&
(options.DeliveryServiceWindowOverridesOptions == null || options.DeliveryServiceWindowOverridesOptions.AreEqual(DeliveryServiceWindowOverridesOptions)&&
(options.ServiceWindowOverridesOptions == null || options.ServiceWindowOverridesOptions.AreEqual(ServiceWindowOverridesOptions)&&
(options.LineItemsOptions == null || options.LineItemsOptions.AreEqual(LineItemsOptions)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With