Skip to main content
replaced http://programmers.stackexchange.com/ with https://softwareengineering.stackexchange.com/
Source Link

I disagree with all three of the opinions. If Client can never be null, then don't even make it possible for it to be null!

  1. Set the value of Client in the constructor
  2. Throw an ArgumentNullException in the constructor.

So your code would be something like:

public class Room
{
    private Client theClient;

    public Room(Client client) {
        if(client == null) throw new ArgumentNullException();
        this.theClient = client;
    }

    public Client Client { 
        get { return theClient; }
        set 
        {
            if (value == null) 
                throw new ArgumentNullException();
            theClient = value;
        }
    }

    public long ClientId
    {
        get
        {
            return theClient.Id;
        }
    }
}
public class Client 
{
    public long Id { get; set; }
}

This is unrelated to your code, but you would probably be better off using an immutable version of Room by making theClient readonly, and then if the client changes, making a new room. This will improve the thread safety of your code in addition to the null safety of the other aspects of my answer. See this discussion on mutable vs immutablethis discussion on mutable vs immutable

I disagree with all three of the opinions. If Client can never be null, then don't even make it possible for it to be null!

  1. Set the value of Client in the constructor
  2. Throw an ArgumentNullException in the constructor.

So your code would be something like:

public class Room
{
    private Client theClient;

    public Room(Client client) {
        if(client == null) throw new ArgumentNullException();
        this.theClient = client;
    }

    public Client Client { 
        get { return theClient; }
        set 
        {
            if (value == null) 
                throw new ArgumentNullException();
            theClient = value;
        }
    }

    public long ClientId
    {
        get
        {
            return theClient.Id;
        }
    }
}
public class Client 
{
    public long Id { get; set; }
}

This is unrelated to your code, but you would probably be better off using an immutable version of Room by making theClient readonly, and then if the client changes, making a new room. This will improve the thread safety of your code in addition to the null safety of the other aspects of my answer. See this discussion on mutable vs immutable

I disagree with all three of the opinions. If Client can never be null, then don't even make it possible for it to be null!

  1. Set the value of Client in the constructor
  2. Throw an ArgumentNullException in the constructor.

So your code would be something like:

public class Room
{
    private Client theClient;

    public Room(Client client) {
        if(client == null) throw new ArgumentNullException();
        this.theClient = client;
    }

    public Client Client { 
        get { return theClient; }
        set 
        {
            if (value == null) 
                throw new ArgumentNullException();
            theClient = value;
        }
    }

    public long ClientId
    {
        get
        {
            return theClient.Id;
        }
    }
}
public class Client 
{
    public long Id { get; set; }
}

This is unrelated to your code, but you would probably be better off using an immutable version of Room by making theClient readonly, and then if the client changes, making a new room. This will improve the thread safety of your code in addition to the null safety of the other aspects of my answer. See this discussion on mutable vs immutable

added 5 characters in body
Source Link
durron597
  • 7.6k
  • 10
  • 38
  • 67

I disagree with all three of the opinions. If Client can never be null, then don't even make it possible for it to be null!

  1. Set the value of Client in the constructor
  2. Throw an ArgumentNullException in the constructor.

So your code would be something like:

public class Room
{
    private Client theClient;

    public Room(Client client) {
        if(client == null) throw new ArgumentNullException();
        this.theClient = client;
    }

    public Client Client { 
        get { return theClient; }
        set 
        {
            if (value == null) 
                throw new ArgumentNullException();
            theClient = value;
        }
    }

    public long ClientId
    {
        get
        {
            return theClient.Id;
        }
    }
}
public class Client 
{
    public long Id { get; set; }
}

This is unrelated to your code, but you would probably be better off using an immutable version of Room by making theClient finalreadonly, and then if the client changes, making a new room. This will improve the thread safety of your code in addition to the null safety of the other aspects of my answer. See this discussion on mutable vs immutable

I disagree with all three of the opinions. If Client can never be null, then don't even make it possible for it to be null!

  1. Set the value of Client in the constructor
  2. Throw an ArgumentNullException in the constructor.

So your code would be something like:

public class Room
{
    private Client theClient;

    public Room(Client client) {
        if(client == null) throw new ArgumentNullException();
        this.theClient = client;
    }

    public Client Client { 
        get { return theClient; }
        set 
        {
            if (value == null) 
                throw new ArgumentNullException();
            theClient = value;
        }
    }

    public long ClientId
    {
        get
        {
            return theClient.Id;
        }
    }
}
public class Client 
{
    public long Id { get; set; }
}

This is unrelated to your code, but you would probably be better off using an immutable version of Room by making theClient final, and then if the client changes, making a new room. This will improve the thread safety of your code in addition to the null safety of the other aspects of my answer. See this discussion on mutable vs immutable

I disagree with all three of the opinions. If Client can never be null, then don't even make it possible for it to be null!

  1. Set the value of Client in the constructor
  2. Throw an ArgumentNullException in the constructor.

So your code would be something like:

public class Room
{
    private Client theClient;

    public Room(Client client) {
        if(client == null) throw new ArgumentNullException();
        this.theClient = client;
    }

    public Client Client { 
        get { return theClient; }
        set 
        {
            if (value == null) 
                throw new ArgumentNullException();
            theClient = value;
        }
    }

    public long ClientId
    {
        get
        {
            return theClient.Id;
        }
    }
}
public class Client 
{
    public long Id { get; set; }
}

This is unrelated to your code, but you would probably be better off using an immutable version of Room by making theClient readonly, and then if the client changes, making a new room. This will improve the thread safety of your code in addition to the null safety of the other aspects of my answer. See this discussion on mutable vs immutable

deleted 34 characters in body
Source Link
durron597
  • 7.6k
  • 10
  • 38
  • 67

I disagree with all three of the opinions. If Client can never be null, then don't even make it possible for it to be null!

  1. Set the value of Client in the constructor
  2. Throw a NullReferenceException or an IllegalArgumentExceptionArgumentNullException in the setterconstructor.

So your code would be something like:

public class Room
{
    private Client theClient;

    public Room(Client client) {
        if(client == null) throw new NullReferenceExceptionArgumentNullException();
        this.theClient = client;
    }

    public Client Client { 
        get { return theClient; }
        set 
        {
            if (value == null) 
                throw new NullReferenceExceptionArgumentNullException();
            theClient = value;
        }
    }

    public long ClientId
    {
        get
        {
            return theClient.Id;
        }
    }
}
public class Client 
{
    public long Id { get; set; }
}

This is unrelated to your code, but you would probably be better off using an immutable version of Room by making theClient final, and then if the client changes, making a new room. This will improve the thread safety of your code in addition to the null safety of the other aspects of my answer. See this discussion on mutable vs immutable

I disagree with all three of the opinions. If Client can never be null, then don't even make it possible for it to be null!

  1. Set the value of Client in the constructor
  2. Throw a NullReferenceException or an IllegalArgumentException in the setter.

So your code would be something like:

public class Room
{
    private Client theClient;

    public Room(Client client) {
        if(client == null) throw new NullReferenceException();
        this.theClient = client;
    }

    public Client Client { 
        get { return theClient; }
        set 
        {
            if (value == null) 
                throw new NullReferenceException();
            theClient = value;
        }
    }

    public long ClientId
    {
        get
        {
            return theClient.Id;
        }
    }
}
public class Client 
{
    public long Id { get; set; }
}

This is unrelated to your code, but you would probably be better off using an immutable version of Room by making theClient final, and then if the client changes, making a new room. This will improve the thread safety of your code in addition to the null safety of the other aspects of my answer. See this discussion on mutable vs immutable

I disagree with all three of the opinions. If Client can never be null, then don't even make it possible for it to be null!

  1. Set the value of Client in the constructor
  2. Throw an ArgumentNullException in the constructor.

So your code would be something like:

public class Room
{
    private Client theClient;

    public Room(Client client) {
        if(client == null) throw new ArgumentNullException();
        this.theClient = client;
    }

    public Client Client { 
        get { return theClient; }
        set 
        {
            if (value == null) 
                throw new ArgumentNullException();
            theClient = value;
        }
    }

    public long ClientId
    {
        get
        {
            return theClient.Id;
        }
    }
}
public class Client 
{
    public long Id { get; set; }
}

This is unrelated to your code, but you would probably be better off using an immutable version of Room by making theClient final, and then if the client changes, making a new room. This will improve the thread safety of your code in addition to the null safety of the other aspects of my answer. See this discussion on mutable vs immutable

added 68 characters in body
Source Link
durron597
  • 7.6k
  • 10
  • 38
  • 67
Loading
added 414 characters in body
Source Link
durron597
  • 7.6k
  • 10
  • 38
  • 67
Loading
minor idiomatic change
Source Link
user53019
user53019
Loading
Source Link
durron597
  • 7.6k
  • 10
  • 38
  • 67
Loading