r/programminghorror • • Mar 05 '26

C# Found this in my game's code

Post image
289 Upvotes

44 comments sorted by

View all comments

6

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

I'm trying to think of a way to rewrite that in a way that makes the conditional less complicated that doesn't make you put teamScores[team] = CurrentGameMode.TeamScores[team]; twice, and I'm drawing a blank. I'm not willing to transcribe the image and play with it, but I totally would if I could select text and copy and paste it.

For sure, this needs several more line breaks.

7

u/orbital1337 Mar 05 '26

I probably would have done something like:

private void SyncScoresFromGameMode() {
    foreach (Player.Team team in CurrentGameMode.TeamScores.Keys) {
        if (CurrentGameMode.ModeCondition == GameModeBase.PrimaryModeCondition.ScoreBased) {
            teamScores[team] = Math.Min(teamScores[team], CurrentGameMode.TeamScores[team]);
        } else {
            teamScores[team] = Math.Max(teamScores[team], CurrentGameMode.TeamScores[team]);
        }
    }

    UpdateAndEmitScore();
}

1

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

Is that better than

private void SyncScoresFromGameMode() {
    foreach (Player.Team team in CurrentGameMode.TeamScores.Keys) {
        if (CurrentGameMode.ModeCondition == GameModeBase.PrimaryModeCondition.ScoreBased && teamScores[team] < CurrentGameMode.TeamScores[team]) {
            teamScores[team] = CurrentGameMode.TeamScores[team];
        } else if (teamScores[team] > CurrentGameMode.TeamScores[team]) {
            teamScores[team] = CurrentGameMode.TeamScores[team];
        }
    }

    UpdateAndEmitScore();
}

?

Actually, maybe. That if expression gets pretty big that way. Also, unless my brain is failing, I think you have the min and max reversed. Either way, there are two places that need updating if the assignment to teamScores[team] changes.

1

u/Kinrany Mar 08 '26 edited Mar 08 '26

Min and max really are better because they express the intent directly

teamScores[team] = CurrentGameMode.TeamScores[team]; happening twice is a coincidence, not repetition

This is some kind of cleanup for incorrect calculation of game score that should have happened right away during the game. It's entirely possible that one of these will later include something like team.kickOnePlayer(), and the other wont. Because those are two separate game modes, they can have totally different rules

2

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

Did you mean to mean to repeat yourself there?

1

u/Kinrany Mar 08 '26

No, thank you :)

1

u/Kinrany Mar 08 '26

And then make this happen when teamScores is updated, not later

1

u/Kinrany Mar 08 '26

I would also put the if outside since it doesn't depend on team

1

u/leikabau5 Mar 05 '26

Couldn't you just combine conditionals like this?

private void SyncScoresFromGameMode() {
	foreach (Player.Team team in CurrentGameMode.TeamScores.Keys) {
	    if ((CurrentGameMode.ModeCondition == GameModeBase.PrimaryModeCondition.ScoreBased && teamScores [team] < CurrentGameMode.TeamScores[team]) 
		    || teamScores [team] > CurrentGameMode.TeamScores[team]) {
	        teamScores[team] = CurrentGameMode.TeamScores[team];
		}
		UpdateAndEmitScore();
	}
}

Still doesn't look pretty but it's better than before.

1

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

You have A && B || C. I think if A is true and B is false, then the expression becomes true if C is true, which isn't what you want.

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();