Link to home
Start Free TrialLog in
Avatar of RonMexico
RonMexico

asked on

Is this bad code?

So I have inherited and must finish some code written by a departing developer.  In going through it I saw the code below.  The intent is fairly obvious (or at least lets assume it is how it looks), it is trying to set flags on the transition of the PressureTimerStatusBit.

It jumped out at me because it uses '&' instead of '&&'.  To me the logical operator is more appropriate here, you should never mix and match logical tests and bitwise operators, because you can get unexpected results like 6&1=FALSE.  On the other hand, maybe its okay because all operands are boolean or 1/0.

I am trying to touch as little working code as possible, so I'm torn: should I 'fix' it (replace '&' with '&&') or is [dont mix and match logical and bitwise] not a hard and fast rule?

Thanks for any thoughts.


if (oldPressureTimerStatusBit == 0 & PressureTimerStatusBit == 1)  // these are bools
{
   self.thresholdTrippedHi = 1;
   _isPressurized = true;
}

if (oldPressureTimerStatusBit == 1 & PressureTimerStatusBit == 0)
{
   self.thresholdTrippedLo = 1;
   _isPressurized = false;
}
oldPressureTimerStatusBit  = PressureTimerStatusBit ;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial