3 ms·
Sometimes you need to know if a value has been explicitly defined or not. Also, sometimes you need to know if a value is different from what it was before. Null
by DougWebb 9y ago
Sometimes you need to know if a value has been explicitly defined or not. Also, sometimes you need to know if a value is different from what it was before. Nullable values help in both cases, because you can tell the difference between the "default" value (false for boolean, "" for strings, 0 for numbers) and "no" value (null). If you have a form being submitted, you can tell the difference between the user setting fields to their default vs the fields not being returned to you at all.
Another example is an ORM system. A data entity could be initialized from an existing database record, or it could be blank. Each property of the data entity could be initialized or not, depending on the SQL query that was used to initialize it. For fields that are non-nullable in the database, you can compare two entities for the same record to determine if fields have been updated or just weren't initialized. That lets you perform database updates only when necessary, and prevents accidentally deleting information.
- sdenton4 9y agoAll booleans - in the fullness of history, brought into the harsh light of business logic - are destined to become enums.
- DougWebb 9y agoThat's deep and thought-provoking. I like it! One of my clients has a database flag field that I needed to use to determine whether or not to show something on their website. Should be a simple boolean field, I thought. Nope; their field can contain one of half a dozen character codes, none of which are Y or N. So you're right; I created an enum to represent the allowed codes and map them to meaningful names, and then I had to create a second boolean property to contain the business logic that decides whether or not to do the display based on the various codes.