Are nullable types preferable to magic numbers?

Posted by Matt H on Programmers See other posts from Programmers or by Matt H
Published on 2012-03-30T16:08:54Z Indexed on 2012/03/30 17:40 UTC
Read the original article Hit count: 304

Filed under:

I have been having a little bit of a debate with a coworker lately. We are specifically using C#, but this could apply to any language with nullable types. Say for example you have a value that represents a maximum. However, this maximum value is optional. I argue that a nullable number would be preferable. My coworker favors the use of zero, citing precedent. Granted, things like network sockets have often used zero to represent an unlimited timeout. If I were to write code dealing with sockets today, I would personally use a nullable value, since I feel it would better represent the fact that there is NO timeout.

Which representation is better? Both require a condition checking for the value meaning "none", but I believe that a nullable type conveys the intent a little bit better.

© Programmers or respective owner

Related posts about null