Skip to main content
Tweeted twitter.com/#!/StackProgrammer/status/185786564658475008
edited tags
Link
gnat
  • 20.5k
  • 29
  • 117
  • 310
Source Link
Matt H
  • 2.2k
  • 17
  • 22

Are nullable types preferable to magic numbers?

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.