Prafull Mishra
1 min readAug 31, 2020

--

Thanks for asking, great question!

The answer to this question is very subjective.

For brevity, I just took the example of `id` attribute of Person class, but yes this can be extended to its other properties as well.

As for me, I think that whenever a class's property is involved in some kind of critical decision making (i.e. statements inside if and else seem to be radically different based on a property's value), then its a good call to promote that data type to a bounded type.

After all, the point is to reduce the states your code can be in, which usually increases with increase in number of conditional statements, sometimes even exponentially.

If we were to consider your case where a Person's age and name and other factors also need to be considered, we can even make Person an enum class & position the checks for the same in a companion method for creating instance, instead of making custom types for each and every property.

Cheers!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Prafull Mishra
Prafull Mishra

No responses yet

Write a response