Glenn
Glenn

Reputation: 5081

What's the best practice for an "n/a" result when it would normally be a boolean value?

I'm working on an api that provides JSON responses. One of the items is typically a bool, but in some cases, it is "n/a".

The two options I'm considering are:

  1. Returning a string like "N/A"
  2. Returning a value of null

The first option is "truthy" though and if someone implemented the API poorly, it could result in a false positive (although it's a little more clear to a human reader). The second result is "falsey" but doesn't really provide the same clarity to a human reader of the result.

Upvotes: 0

Views: 169

Answers (1)

rkm
rkm

Reputation: 3141

I would go with null which means that property does not have any value, regardless if it's boolean, string etc. It will keep data type consistent, otherwise you would have to mix Boolean with String which is probably bad idea and will definitely not make life of API consumers easier. Or keep the value string and pass Boolean as a String, which also sounds like a workaround.

N/A seems to be more like a representation of null value on the consumer side. And by the way different consumers can represent it in different way. Or it could be not the same for different locales.

Upvotes: 1

Related Questions