What is the best way to check for a value that was passed in the controller?
Is it assert or if?
if this is the sample url....
http://example.com/read/1/2
I would like to check if 1 and 2 is number and if it's null. Like if the user changed the url to http://example.com/read/1asdf/2asdfqwer
We are using assert in our company. And I'm just thinking that what will happen if it's already in production mode and assert is disabled.
Somebody give me an insight with this.
assert
is intended as a debugging tool for checking conditions under you control. If it fires, it indicates a bug in your code. Checking user supplied input does not fall into this category, instead it is a feature of correct programs. Thus you should use if statements for checking user input.
Asserts are mainly used to check preconditions.
Its like if given preconditions are not mate throw AssertionError
.
Checking preconditions this way is actually good practice in testing environments. Where we can make sure all preconditions are mate and satisfied.
In production environment if debug mode is not yet activated then it will surely will not harm. this statement will work as if it is commented.
In your case if you merely want to check which value is accepted at controller level use if's.
but If you want to check that as precondition and then you want throw error if these preconditions are not met and ideally in debug mode. (AssertionError.) then you can use asserts.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With