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
Youssef Shoaib [MOD]
04/04/2023, 3:35 PM
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
Chris Lee
04/04/2023, 3:36 PM
often do
require
or
check
to handle verification conditions.
☝️ 1
s
Szymon Chaber
04/04/2023, 3:40 PM
It's sometimes useful to do. It's related to concepts of defensive programming & fail-fast
c
Chris Lee
04/04/2023, 3:43 PM
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
Colton Idle
04/04/2023, 3:53 PM
Ah. require was what i was looking for. Throws IAE. chefs kiss