Positive comparison in Boolean expressions


Link to this posting

Postby Ursego » 21 Feb 2013, 21:42

Comparing values, try to use "equal" instead of "not equal".

*** BAD code: ***

PB:
Code: Select all
if ls_order_status <> n_order_status.CLOSED) then
   [actions for orders which are not closed]
else
   [actions for closed orders]
end if

Java:
Code: Select all
if (orderStatus <> OrderStatuses.CLOSED) {
   [actions for orders which are not closed]
} else {
   [actions for closed orders]
}


*** GOOD code: ***

PB:
Code: Select all
if ls_order_status = n_order_status.CLOSED) then
   [actions for closed orders]
else
   [actions for orders which are not closed]
end if

Java:
Code: Select all
if (orderStatus = OrderStatuses.CLOSED) {
   [actions for closed orders]
} else {
   [actions for orders which are not closed]
}


This advice is especially important if the value comparison is nested into a Boolean expression:

*** BAD code: ***

PB:
Code: Select all
if ls_order_status <> n_order_status.CLOSED or ls_state <> n_state.MONTANA or ls_day_type <> n_day_type.WEEKEND then
   [actions when order is not closed OR state differs from Montana OR day is not a weekend]
else
   [actions for other cases]
end if

Java:
Code: Select all
if (orderStatus <> OrderStatuses.Closed || state <> States.MONTANA || dayType <> DayTypes.WEEKEND) {
   [actions when order is not closed OR state differs from Montana OR day is not a weekend]
} else {
   [actions for other cases]
}


*** GOOD code: ***

PB:
Code: Select all
if ls_order_status = n_order_status.CLOSED and ls_state = n_state.MONTANA and ls_day_type = n_day_type.WEEKEND then
   [actions when order is closed AND state is Montana AND day is a weekend]
else
   [actions for other cases]
end if

Java:
Code: Select all
if (orderStatus = OrderStatuses.CLOSED && state = States.MONTANA && dayType = DayTypes.WEEKEND) {
   [actions when order is closed AND state is Montana AND day is a weekend]
} else {
   [actions for other cases]
}
User avatar
Ursego
Site Admin
 
Posts: 124
Joined: 19 Feb 2013, 20:33



IF you want to ((lose weight) OR (have unbelievable (brain function AND mental clarity))) THEN click:




cron
free counters

eXTReMe Tracker