They are trying to avoid returning null in this scenario.
Unfortunately, if the API is designed poorly you're stuck with it.
I can sort of understand why people hate checked exceptions but working without exceptions is just terrible.
They are trying to avoid returning null in this scenario.
Unfortunately, if the API is designed poorly you're stuck with it.
I can sort of understand why people hate checked exceptions but working without exceptions is just terrible.
There's no one-size-fits-all solution. In a lot of ways, the best response to "record not found" is that you get the same result as finding one, except with zero answers. That means your main line can process the same way -- unless that doesn't produce the result you need. In which case maybe you an if(null) somewhere, or a special fake record, or an exception.
At the same time before NULL, devs used to use "guard values", so NULL is really just a convenience.
for instance, just to illustrate what I'm saying:
let NULL = {} /* should every lib define its own null value? */
function GetOneRecord(){
let dbResult = queryRecordsFromDB();
if (dbResult.length === 0){
return NULL
}
return dbResult.records[0];
}
Optional types, are better, but one needs functional programming features for it to be really useful.What's people opinion on zero values by the way?