There is no more difficult choice than choosing between two equally good options.
We had to make hundreds of such decisions when developing the JSight language. Making a decision entails more than just choosing between two alternatives. It's not so much about the decision, as about the criteria for making decisions. Firstly, you need to understand why the existing decision criteria do not help you choose the right option. Then, you need to modify these criteria. After that, making the decision itself is no longer difficult.
Today we are going to share about one of such cases.
In most data structures, certain fields are mandatory while others are optional. This needs to be somehow marked in the data schema. In our case, there were two possible solutions: add either a rule required
or a rule optional
to the language:
{
"id": 123 // {required: true}
"name": "Tom" // {required: false}
}
Or this way:
{
"id": 123 // {optional: false}
"name": "Tom" // {optional: true}
}
The second question following the first is what should be the default value: required: true
or required: false
? And accordingly in the case of optional
, optional: true
or optional: false
? When I see a property in an example of valid data, it intuitively seems to me that this property “should be”, unless otherwise stated. This means that by default, required: true
or optional: false
is correct. Then we have the following options:
{
"id": 123
"name": "Tom" // {required: false}
}
Or this way:
{
"id": 123
"name": "Tom" // {optional: true}
}
Which option do you prefer? Please write in the comments to this post, we are interested!
After comparing both options, we decided to rely on this criteria: which at first glance is perceived as faster and more in line with intuition?
The brain is used to interpret information in the simplest way. When we see the word required...
, our initial assumption is that this property is required, since that is what the word required
means. Then, reading the rule till the end required: false
, we experience cognitive dissonance. We initially assumed that the field is required, but it turns out that required: false
.
There is no cognitive dissonance in the case of optional: true
. We see the word optional...
and we automatically assume that this property is optional. Then we read the rule till the end optional: true
and find confirmation of our initial thought.
So, considering how quickly the schema is intuitively perceived, optional
fits better than required
. However, another strong criteria works in favor of required
: this word is more familiar to us, it is used, for example, in JSON-Schema, OpenAPI, RAML.
What is more important: convenience or habit? After long debates, we concluded that since we were creating a completely new language, convenience is more important, therefore we went with optional
. Time will tell whether we were right or not.