- Home /
Answer by AnomalusUndrdog · Jun 12, 2015 at 10:44 PM
Assertions are used to check your code to make sure it's doing what you intended it to do.
Let's say you're making an RPG and wearing armor is supposed to reduce incoming damage, right before the code that deals damage, you could put an assert to check if the incoming damage did indeed get reduced if the character is wearing armor.
It may be similar to just placing an if check, but the point here is to catch errors in the code that should not be there in the first place, i.e. bugs. It's a way to catch these bugs right there and then as they happen. With an Assert, you're making an assertion that the code works how you wanted it to.
It may seem silly to have to bother adding a check to code that you wrote yourself, but for a team of programmers, where someone else might end up editing code that they didn't write, this is useful to make sure they didn't break anything.
It's also useful even if you're working alone and you're editing code you wrote perhaps months ago, the assert helps to make sure that you didn't break anything.
Assertions are different from catching erroneous user-input. Errors in user-input are to be expected/anticipated. So usual if checks should be used there, not asserts.
Very nice! This would be very useful especially if stripped in Release builds.
Your answer
Follow this Question
Related Questions
#if not working after api update 1 Answer
Assets/scripts/fbLogin.cs(60,52): error CS1501: No overload for method `API' takes `3' arguments 1 Answer
Why am I getting NullReferenceException on my .Contains()? 1 Answer
How can i get Viking Village to work with error code? 0 Answers
threading if() problem 1 Answer