r/programminghorror • • Mar 05 '26

C# Found this in my game's code

Post image
290 Upvotes

44 comments sorted by

View all comments

Show parent comment

1

u/simulacrumgames Mar 08 '26

Are we in a world where != doesn't exist? Or am I misreading the point of the code?

1

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo "You live" Mar 08 '26

I'm still not seeing where != would help much.

1

u/simulacrumgames Mar 08 '26 edited Mar 10 '26
if (CurrentGameMode.ModeCondition != GameModeBase.PrimaryModeCondition.ScoreBased)
  return;

changed = false;
for each (team in teams)
{
  if (teamScores[team] != CurrentGameMode.TeamScores[team])
  {
    changed = true;
    CurrentGameMode.TeamScores[team] = teamScores[team];
  }
}

if (changed)
  UpdateAndEmitScore();

The only real reason to do the check would be to prevent emitting the score unnecessarily. If the stuff that happens on the other side of the emit isn't worth preventing, you shouldn't even check if they're different, just update all the scores and emit every time.

1

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo "You live" Mar 10 '26

In one case the score is updated if it is less than CurrentGameMode.TeamScores[team], in the other it is updated if it is greater. This seems to have very different behavior.

1

u/simulacrumgames Mar 10 '26

Which is exactly saying "if the score is different update it"

1

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo "You live" Mar 10 '26

Yeah, but unless I'm misreading the ternary or something, the logic in the OP is if the game mode is score based, only update the score if it is less than, and if it is not score based, then only update the score if it is greater than. Your code always returns early if the mode is not score based, and if it is, it always updates the score if it differs. I mean, for all I know, your version is how it should work, but this would be why I didn't see how != helps.

1

u/simulacrumgames Mar 10 '26

Ah, I see. That really makes no sense to me as what this is for lol. We can go full insaneo mode then:

inverter = -1 + 2 * (CurrentGameMode == ScoreBased);

for each (team in teams)
{
  lesser = teamScores[team] - CurrentGameMode.Scores[team];
  if (lesser * inverter < 0)
  {
    teamScores[team] = CurrentGameMode.Scores[team];
  }
}

UpdateAndEmitScore();