Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property never null c#

When refactoring code, I come up with instances like the following

private string _property = string.Empty;
public string Property
{
    set { _property = value ?? string.Empty); }
}

Later on in a method I see the following:

if (_property != null)
{
    //...
}

Assuming that _property is only set by the setter of Property, is this code redundant?

I.e is there any way, through reflection wizardry or other methods that _property can ever be null?

like image 711
H Bellamy Avatar asked May 12 '26 17:05

H Bellamy


1 Answers

Assuming that _property is only set by the setter of Property, is this code redundant?

Exactly, it is redundant. This is the actual purpose of Properties. We shouldn't access the fields of a class directly. We should access them using a Property. So in the corresponding setter, we can embed any logic and we can rest assure that each time we try to set a value this logic would be verified once more.This argument holds even for the methods of a class. In a method we must use the properties and not the actual fields. Furthermore, when we want to read the value of a field, we should make use of the corresponding getter.

In general, properties enhances the concept of encapsulation, which is one of the pillars of object oriented programming OOP.

Many times there isn't any logic that should be applied when we want to set a value. Take for instance the following example:

public class Customer
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

We have declared a class for representing a Customer. A Customer object should have three properties an Id, a FirstName and a LastName.

An immediate question, when someones read this class is why should someone make use of properties here?

The answer is again the same, they provide a mechanism of encapsulation. But let's consider how can this help us in the long run. Let's say that one day someone decides that the first name of a customer should be a string of length less than 20. If the above class had been declared as below:

public class Customer
{
    public int Id;
    public string FirstName;
    public string LastName;
}

then we should check for the length of FirstName in each instance we had created ! Otherwise, if we had picked the declaration with the properties, we could just easily make use of Data Annotations

public class Customer
{
    public int Id { get; set; }
    [StringLength(20)]
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

and that's it. Another approach it could be the following:

public class Customer
{
    public int Id { get; set; }
    private string firstName;
    public string FirstName 
    { 
        get { return firstName }
        set
        {
            if(value!=null && value.length<20)
            {
                firstName = value;
            }
            else
            {
                throw new ArgumentException("The first name must have at maxium 20 characters", "value");
            }
        } 
    }
    public string LastName { get; set; }
}

Consider both of the above approaches with having to revisit all your codebase and make this check. It's crystal clear that properties win.

like image 130
Christos Avatar answered May 14 '26 09:05

Christos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!