Skip to main content
deleted 2 characters in body
Source Link
Simon Forsberg
  • 59.4k
  • 9
  • 156
  • 309
if (guess < toGuess)
{
    status = ...
}
else if (guess > toGuess)
{
    status = ...
}
else if (guess == toGuess)
{
    status = ...
}
else
{
    ...
}


if (status == HIGH)
{
    ...
}
else if (status == LOW)
{
    ...
}
else if (status == CORRECT)
{
    ...
}
else
{
    ...
}

Trust me, neither of these last else statements will ever, ever, ever, ever happen.

You'd much better do this instead:

if (guess < toGuess)
{
    status = ...
}
else if (guess > toGuess)
{
    status = ...
}
else { // guess == toGuess
{
    status = ...
}


if (status == HIGH)
{
    ...
}
else if (status == LOW)
{
    ...
}
else
{
    ...
}

Because if it's not more, and not less, it has to be equal.

If you really, really, really, really think that it can happen, throw an Exception

if (guess < toGuess)
{
    status = ...
}
else if (guess > toGuess)
{
    status = ...
}
else if (guess == toGuess)
{
    status = ...
}
else
{
    ...
}


if (status == HIGH)
{
    ...
}
else if (status == LOW)
{
    ...
}
else if (status == CORRECT)
{
    ...
}
else
{
    ...
}

Trust me, neither of these last else statements will ever, ever, ever, ever happen.

You'd much better do this instead:

if (guess < toGuess)
{
    status = ...
}
else if (guess > toGuess)
{
    status = ...
}
else { // guess == toGuess
{
    status = ...
}


if (status == HIGH)
{
    ...
}
else if (status == LOW)
{
    ...
}
else
{
    ...
}

Because if it's not more, and not less, it has to be equal.

If you really, really, really, really think that it can happen, throw an Exception

if (guess < toGuess)
{
    status = ...
}
else if (guess > toGuess)
{
    status = ...
}
else if (guess == toGuess)
{
    status = ...
}
else
{
    ...
}


if (status == HIGH)
{
    ...
}
else if (status == LOW)
{
    ...
}
else if (status == CORRECT)
{
    ...
}
else
{
    ...
}

Trust me, neither of these last else statements will ever, ever, ever, ever happen.

You'd much better do this instead:

if (guess < toGuess)
{
    status = ...
}
else if (guess > toGuess)
{
    status = ...
}
else // guess == toGuess
{
    status = ...
}


if (status == HIGH)
{
    ...
}
else if (status == LOW)
{
    ...
}
else
{
    ...
}

Because if it's not more, and not less, it has to be equal.

If you really, really, really, really think that it can happen, throw an Exception

Source Link
Simon Forsberg
  • 59.4k
  • 9
  • 156
  • 309

if (guess < toGuess)
{
    status = ...
}
else if (guess > toGuess)
{
    status = ...
}
else if (guess == toGuess)
{
    status = ...
}
else
{
    ...
}


if (status == HIGH)
{
    ...
}
else if (status == LOW)
{
    ...
}
else if (status == CORRECT)
{
    ...
}
else
{
    ...
}

Trust me, neither of these last else statements will ever, ever, ever, ever happen.

You'd much better do this instead:

if (guess < toGuess)
{
    status = ...
}
else if (guess > toGuess)
{
    status = ...
}
else { // guess == toGuess
{
    status = ...
}


if (status == HIGH)
{
    ...
}
else if (status == LOW)
{
    ...
}
else
{
    ...
}

Because if it's not more, and not less, it has to be equal.

If you really, really, really, really think that it can happen, throw an Exception