We have this code which, when simplified, looks like this:
public class Room
{
public Client Client { get; set; }
public long ClientId
{
get
{
return Client == null ? 0 : Client.Id;
}
}
}
public class Client
{
public long Id { get; set; }
}
Now we have three viewpoints.
1) This is good code because the Client
property should always be set (i.e. not null) so the Client == null
will never occur and the Id value 0
denotes a false Id anyway (this is the opinion of the writer of the code ;-))
2) You can not rely on the caller to know that 0
is a false value for Id
and when the Client
property should always be set you should throw an exception
in the get
when the Client
property happens to be null
3) When the Client
property should always be set you just return Client.Id
and let the code throw a NullRef
exception when the Client
property happens to be null.
Which of these is most correct? Or is there a fourth possibility?