Equal (==) is an error-prone operator in my opinion

Hi,
just for discussion:

I noticed that often in the series, I worked with <= / >= operators, when the instructor is unsing isEqual (==). In my opinion this is a bit tricky. There will be errors if you forgot to clamp variables.

When you fogot to clamp or clamp on the wrong position, maybe a variable gets above/below the targeted number.

E.g. for health: when you forgot to clamp, the branch with “==” (equal) will neber be true and the next action never executes.

So I say, always use less then or greater then. For the “isDead” do

if health <= 0 then DO sth (kill player; restart game, etc…).

Other opinions are welcome.

Cheers!

1 Like

I do see this is fairly old now.

It is true that there are times when it could cause an extra issue. I often use >= and <= as well in the big picture.

However, there are times when something should only run on a specific number and no others. So there are situations where it must be used or the opposite is true and your idea is then error-prone.

Conversely, the code may be able to be reworked to function some other way and not use anything here. Decisions :slight_smile:

As with programming, stupid things happen no matter how hard we try to keep it from happening. Testing is the only real way to avoid problems.

1 Like

Thank you for pointing this out! I actually had clamped my health but for some reason == wasn’t working for me, but <= worked fine

1 Like

Privacy & Terms