Skip to main content
added 426 characters in body
Source Link
janos
  • 113.1k
  • 15
  • 154
  • 396

If Score cannot jump suddenly in the levels, for example after Score=50 it cannot jump to Score=550, then you could simplify by using half-ranges in the if statements, so instead of Score >= X && Score <= X + C you could use just Score >= X, like this:

void IncreaseScore() { if (Score >= 100 && !Increased100500) { Increase(); Increased100500 = true; } else if (Score >= 500 && !Increased5001500) { Increase(); Increased5001500 = true; } else if (Score >= 1500 && !Increased15003000) { Increase(); Increased15003000 = true; } } 

But this is just treating the symptoms, not the real problem. It's not good that you need 3 state variables Increased100500, Increased5001500 , and Increased15003000, and the hardcoded constants 100, 500, 1500 to keep track of this. 

It would be better to refactor somehow without using those boolean variables at all.

  For example, here's an implementation in Java (sorry it's not C#):

private finalconst int[] thresholds = new int[]{100, 500, 1500}; private finalconst Set<Integer>HashSet<int> passedThresholds = new HashSet<>HashSet<int>(); public void increaseScoreIncreaseScore()   { for (int threshold : thresholds)  { if (scoreScore >= threshold && !passedThresholds.containsContains(threshold))  { passedThresholds.add(threshold); increaseIncrease(); break; } } } 

The behavior of this code is equivalent, and you don't need so many boolean variables to track the state. The increaseScore method doesn't need to know the exact values of the thresholds either, which is a good thing. And you can easily change the thresholds, simply in the declaration of thresholds, and without editing variable names.

If Score cannot jump suddenly in the levels, for example after Score=50 it cannot jump to Score=550, then you could simplify by using half-ranges in the if statements, so instead of Score >= X && Score <= X + C you could use just Score >= X, like this:

void IncreaseScore() { if (Score >= 100 && !Increased100500) { Increase(); Increased100500 = true; } else if (Score >= 500 && !Increased5001500) { Increase(); Increased5001500 = true; } else if (Score >= 1500 && !Increased15003000) { Increase(); Increased15003000 = true; } } 

But this is just treating the symptoms, not the real problem. It's not good that you need 3 state variables Increased100500, Increased5001500 , and Increased15003000, and the hardcoded constants 100, 500, 1500 to keep track of this. It would be better to refactor somehow without using those boolean variables at all.

  For example, here's an implementation in Java (sorry it's not C#):

private final int[] thresholds = new int[]{100, 500, 1500}; private final Set<Integer> passedThresholds = new HashSet<>(); public void increaseScore() { for (int threshold : thresholds) { if (score >= threshold && !passedThresholds.contains(threshold)) { passedThresholds.add(threshold); increase(); break; } } } 

The behavior of this code is equivalent, and you don't need so many boolean variables to track the state. The increaseScore method doesn't need to know the exact values of the thresholds either, which is a good thing. And you can easily change the thresholds, simply in the declaration of thresholds, and without editing variable names.

If Score cannot jump suddenly in the levels, for example after Score=50 it cannot jump to Score=550, then you could simplify by using half-ranges in the if statements, so instead of Score >= X && Score <= X + C you could use just Score >= X, like this:

void IncreaseScore() { if (Score >= 100 && !Increased100500) { Increase(); Increased100500 = true; } else if (Score >= 500 && !Increased5001500) { Increase(); Increased5001500 = true; } else if (Score >= 1500 && !Increased15003000) { Increase(); Increased15003000 = true; } } 

But this is just treating the symptoms, not the real problem. It's not good that you need 3 state variables Increased100500, Increased5001500 , and Increased15003000, and the hardcoded constants 100, 500, 1500 to keep track of this. 

It would be better to refactor somehow without using those boolean variables at all. For example:

private const int[] thresholds = {100, 500, 1500}; private const HashSet<int> passedThresholds = new HashSet<int>(); void IncreaseScore()  { for (int threshold : thresholds)  { if (Score >= threshold && !passedThresholds.Contains(threshold))  { passedThresholds.add(threshold); Increase(); break; } } } 

The behavior of this code is equivalent, and you don't need so many boolean variables to track the state. The increaseScore method doesn't need to know the exact values of the thresholds either, which is a good thing. And you can easily change the thresholds, simply in the declaration of thresholds, and without editing variable names.

added 426 characters in body
Source Link
janos
  • 113.1k
  • 15
  • 154
  • 396

If Score cannot jump suddenly in the levels, for example after Score=50 it cannot jump to Score=550, then you could simplify by using half-ranges in the if statements, so instead of Score >= X && Score <= X + C you could use just Score >= X, like this:

void IncreaseScore() { if (Score >= 100 && !Increased100500) { Increase(); Increased100500 = true; } else if (Score >= 500 && !Increased5001500) { Increase(); Increased5001500 = true; } else if (Score >= 1500 && !Increased15003000) { Increase(); Increased15003000 = true; } } 

But this is just treating the symptoms, not the real problem. It's not good that you need 3 state variables Increased100500, Increased5001500 , and Increased15003000, and the hardcoded constants 100, 500, 1500 to keep track of this. It would be better to refactor somehow without using those boolean variables at all.

For example, here's an implementation in Java (sorry it's not C#):

private final int[] thresholds = new int[]{100, 500, 1500}; private final Set<Integer> passedThresholds = new HashSet<>(); public void increaseScore() { for (int threshold : thresholds) { if (score >= threshold && !passedThresholds.contains(threshold)) { passedThresholds.add(threshold); increase(); break; } } } 

The behavior of this code is equivalent, and you don't need so many boolean variables to track the state. The increaseScore method doesn't need to know the exact values of the thresholds either, which is a good thing. And you can easily change the thresholds, simply in the declaration of thresholds, and without editing variable names.

If Score cannot jump suddenly in the levels, for example after Score=50 it cannot jump to Score=550, then you could simplify by using half-ranges in the if statements, so instead of Score >= X && Score <= X + C you could use just Score >= X, like this:

void IncreaseScore() { if (Score >= 100 && !Increased100500) { Increase(); Increased100500 = true; } else if (Score >= 500 && !Increased5001500) { Increase(); Increased5001500 = true; } else if (Score >= 1500 && !Increased15003000) { Increase(); Increased15003000 = true; } } 

But this is just treating the symptoms, not the real problem. It's not good that you need 3 state variables Increased100500, Increased5001500 , and Increased15003000, and the hardcoded constants 100, 500, 1500 to keep track of this. It would be better to refactor somehow without using those boolean variables at all.

If Score cannot jump suddenly in the levels, for example after Score=50 it cannot jump to Score=550, then you could simplify by using half-ranges in the if statements, so instead of Score >= X && Score <= X + C you could use just Score >= X, like this:

void IncreaseScore() { if (Score >= 100 && !Increased100500) { Increase(); Increased100500 = true; } else if (Score >= 500 && !Increased5001500) { Increase(); Increased5001500 = true; } else if (Score >= 1500 && !Increased15003000) { Increase(); Increased15003000 = true; } } 

But this is just treating the symptoms, not the real problem. It's not good that you need 3 state variables Increased100500, Increased5001500 , and Increased15003000, and the hardcoded constants 100, 500, 1500 to keep track of this. It would be better to refactor somehow without using those boolean variables at all.

For example, here's an implementation in Java (sorry it's not C#):

private final int[] thresholds = new int[]{100, 500, 1500}; private final Set<Integer> passedThresholds = new HashSet<>(); public void increaseScore() { for (int threshold : thresholds) { if (score >= threshold && !passedThresholds.contains(threshold)) { passedThresholds.add(threshold); increase(); break; } } } 

The behavior of this code is equivalent, and you don't need so many boolean variables to track the state. The increaseScore method doesn't need to know the exact values of the thresholds either, which is a good thing. And you can easily change the thresholds, simply in the declaration of thresholds, and without editing variable names.

added 71 characters in body
Source Link
janos
  • 113.1k
  • 15
  • 154
  • 396

YouIf Score cannot jump suddenly in the levels, for example after Score=50 it cannot jump to Score=550, then you could simplify by reordering your ifusing half-elseranges in the if statements:

void IncreaseScore() { if (Score >= 1500) { Increase(); Increased15003000 = true; } else if (Score >= 500) { Increase(); Increased5001500 = true; } else if (Score >= 100) { Increase(); Increased100500 = true; } } 

Based on your follow-up comments, I suppose this is whatso instead of Score >= X && Score <= X + C you really wantedcould use just Score >= X, like this:

void IncreaseScore() { if (Score >= 100 && !Increased100500) { Increase(); Increased100500 = true; } else if (Score >= 500 && !Increased5001500) { Increase(); Increased5001500 = true; } else if (Score >= 1500 && !Increased15003000) { Increase(); Increased15003000 = true; } } 

But this is just treating the symptoms, not the real problem. It's not good that you need 3 state variables Increased100500, Increased5001500 , and Increased15003000, and the hardcoded constants 100, 500, 1500 to keep track of this. It would be better to refactor somehow without using those boolean variables at all.

You could simplify by reordering your if-else statements:

void IncreaseScore() { if (Score >= 1500) { Increase(); Increased15003000 = true; } else if (Score >= 500) { Increase(); Increased5001500 = true; } else if (Score >= 100) { Increase(); Increased100500 = true; } } 

Based on your follow-up comments, I suppose this is what you really wanted:

void IncreaseScore() { if (Score >= 100 && !Increased100500) { Increase(); Increased100500 = true; } else if (Score >= 500 && !Increased5001500) { Increase(); Increased5001500 = true; } else if (Score >= 1500 && !Increased15003000) { Increase(); Increased15003000 = true; } } 

If Score cannot jump suddenly in the levels, for example after Score=50 it cannot jump to Score=550, then you could simplify by using half-ranges in the if statements, so instead of Score >= X && Score <= X + C you could use just Score >= X, like this:

void IncreaseScore() { if (Score >= 100 && !Increased100500) { Increase(); Increased100500 = true; } else if (Score >= 500 && !Increased5001500) { Increase(); Increased5001500 = true; } else if (Score >= 1500 && !Increased15003000) { Increase(); Increased15003000 = true; } } 

But this is just treating the symptoms, not the real problem. It's not good that you need 3 state variables Increased100500, Increased5001500 , and Increased15003000, and the hardcoded constants 100, 500, 1500 to keep track of this. It would be better to refactor somehow without using those boolean variables at all.

deleted 191 characters in body
Source Link
janos
  • 113.1k
  • 15
  • 154
  • 396
Loading
Source Link
janos
  • 113.1k
  • 15
  • 154
  • 396
Loading