Is it common to want to write an assert statement ...
# random
c
Is it common to want to write an assert statement in a regular method (not a test?) example: I want to make sure the arg that people pass in is greater than 0
y
Usually, people use the
require
function (for parameters) or the
ensure
function (for the state of a class). Or you could have a
value class PositiveInt internal constructor(val underlying: Int)
that has a
require
statement in its initializer and a nullable factory function
fun PositiveInt(int: Int): PositiveInt?
☝️ 1
c
often do
require
or
check
to handle verification conditions.
☝️ 1
s
It's sometimes useful to do. It's related to concepts of defensive programming & fail-fast
c
it’s technically ‘offensive programming’, a subset of defensive programming. defensive would be compensating for the invalid inputs (if string == null string = “”) vs failing fast on invalid inputs.
👍 1
c
Ah. require was what i was looking for. Throws IAE. chefs kiss
c
yep,
require
is for validating inputs,
check
is for validating state, and
error
can be used for anything else.