Nullable enumeration values vs. “NoValue” or “Undefined”, etc

I often write code which translates entities in the database to domain objects. These entities often have fields which are constrained and translate to enumerations in the domain objects. In some cases, though, the fields can be null.

For example, imagine I have a PrintJob entity with a Status field that can be New, Submitted, or Completed and a Result field that can be Succeeded, FailedNoPaper, or FailedNoToner but it can also be NULL if the Status is not Completed.

On the one hand, I like having a one-to-one mapping of entity field values to domain object enum values, but on the other hand, it somehow feels “better” to always have values for properties with enumeration types.

My question is: is it better to represent the value of the Result field in my domain object as a nullable value (e.g. MyStatus? in C# or VB.NET) having essentially no value when the corresponding entity field is NULL, or should I create a special enum value along with the others, e.g. NoValue or Undefined?

If a field value is one of a list of predefined values, your domain will have a real-world value that you can correspond to NULL. All you have to do is look for it. If the Status is not completed its result could be ‘NotFinished’, ‘Unavailable’, ‘DoesNotExist’ or ‘Unknown’.

Think about this in a non-code way. If you print something and ask a colleague to bring over the stack of papers from the printer, how would he let you know that there is no printout on the printer? He might say it isn’t finished yet, it wasn’t available yet, there was nothing on the printer…

As far as enums go, I’m not a big fan of nullable enums. Add a single, default field such as ‘Unknown’ and you NEVER EVER have to worry about it being NULL. Otherwise, you will spend reams of code in your application to first check for NULL before doing anything sensible… Enums, like booleans, should shy away from being NULL.

1

Looking at this as a state machine problem, where each state was represented by an enum, it would be reasonable to provide an enum for all states, including the “not yet” state. I’d be tempted to name it something meaningful, though, like “PROCESSING” rather than a synonym for “NULL”.

Setting aside the programmatic problems with NULL, representing the state as NULL would be a valid way to say “no state at all”. But putting the second value into some known initial state is more definitive.

1

Compare your situation to the empty string / null string:

An empty string (in C: ”) means the string is known to have no value, whereas the null string means it is not known to have a value.

As the result value is not known until the job finishes, the null value seems correct. Of course you can use an enum value to indicate “not known to be known”. It also means you cannot query the result value until you have checked the status is “completed”.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Nullable enumeration values vs. “NoValue” or “Undefined”, etc

I often write code which translates entities in the database to domain objects. These entities often have fields which are constrained and translate to enumerations in the domain objects. In some cases, though, the fields can be null.

For example, imagine I have a PrintJob entity with a Status field that can be New, Submitted, or Completed and a Result field that can be Succeeded, FailedNoPaper, or FailedNoToner but it can also be NULL if the Status is not Completed.

On the one hand, I like having a one-to-one mapping of entity field values to domain object enum values, but on the other hand, it somehow feels “better” to always have values for properties with enumeration types.

My question is: is it better to represent the value of the Result field in my domain object as a nullable value (e.g. MyStatus? in C# or VB.NET) having essentially no value when the corresponding entity field is NULL, or should I create a special enum value along with the others, e.g. NoValue or Undefined?

If a field value is one of a list of predefined values, your domain will have a real-world value that you can correspond to NULL. All you have to do is look for it. If the Status is not completed its result could be ‘NotFinished’, ‘Unavailable’, ‘DoesNotExist’ or ‘Unknown’.

Think about this in a non-code way. If you print something and ask a colleague to bring over the stack of papers from the printer, how would he let you know that there is no printout on the printer? He might say it isn’t finished yet, it wasn’t available yet, there was nothing on the printer…

As far as enums go, I’m not a big fan of nullable enums. Add a single, default field such as ‘Unknown’ and you NEVER EVER have to worry about it being NULL. Otherwise, you will spend reams of code in your application to first check for NULL before doing anything sensible… Enums, like booleans, should shy away from being NULL.

1

Looking at this as a state machine problem, where each state was represented by an enum, it would be reasonable to provide an enum for all states, including the “not yet” state. I’d be tempted to name it something meaningful, though, like “PROCESSING” rather than a synonym for “NULL”.

Setting aside the programmatic problems with NULL, representing the state as NULL would be a valid way to say “no state at all”. But putting the second value into some known initial state is more definitive.

1

Compare your situation to the empty string / null string:

An empty string (in C: ”) means the string is known to have no value, whereas the null string means it is not known to have a value.

As the result value is not known until the job finishes, the null value seems correct. Of course you can use an enum value to indicate “not known to be known”. It also means you cannot query the result value until you have checked the status is “completed”.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật