0

The question I am about to ask is somehow general and may apply to all OOP languages but I'm coding in C#.

How we decide to make a property read-only? In my current program I have made all the properties read-only for all the classes and pass the values via constructor when I initialize them. So all of my classes look like this:

public class Point
{
   private double _x;
   private double _y;

   public double X
   {
      get { return _x; }
   }

   public double Y
   {
      get { return _y; }
   }


   public Point(double x, double y)
   {
      _x = x;
      _y = y;
   }
}

But now I doubt if this is a good idea!? Because now some of the constructors in my classes are getting big and ugly with too many arguments! I'm a bit lost now! What are some guidelines to decide what properties are allowed to be set later and do not need to be passed via constructor!?

Vahid
  • 5,144
  • 13
  • 70
  • 146
  • 1
    What you're basically asking is when to make your objects immutable. Check out this question and the answers it got: http://stackoverflow.com/questions/214714/mutable-vs-immutable-objects – MarcinJuraszek Feb 05 '17 at 20:43
  • @MarcinJuraszek Thanks I will look into it. – Vahid Feb 05 '17 at 20:44

2 Answers2

1

The answer seems to be easy: you need to make them all read-only when the object's associated data must be immutable. This is troublesome when associated data is also composed by other objects, because maybe you can't set the whole property but the associated object's property can be set anyway. BTW, at least you know that a given associated object wouldn't be lost in the jungle.

In the other hand, initializing objects with constructors isn't directly tied to the fact that values passed as arguments on these constructors should be necessarily immutable. They can mutate later, why not.

Maybe your question has some other intrinsic question: when to provide constructors?

Basically, you provide constructors when you want to initialize an object in a concrete way or, like in your case, you want to initialize readonly fields and/or read-only properties. Otherwise, providing no specific constructor other than the default one is fine:

public class Person
{
     public string Name { get; set; }
     public string LastName { get; set; }
}

// Default constructor with an object initializer
var person = new Person { Name = "Matías", LastName = "Fidemraizer" };
Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
1

It is a good idea to start with read-only fields. Then, if the need arises, free this field and remove "readonly" keyword. It is easy to relax this rule, but it is hard to add "readonly" to already existent field. This may cause a lot of refactoring.

To shorten a number of lines use succinct syntax.

//no need to implement y property explicitly
public double Y { get; private set; }

In case of C# 6 you can even use an Expression body

public double Y => 51123;
Sloven
  • 335
  • 3
  • 10
  • An expression bodied property is read-only but it doesn't generate a read-only field. `public double Y { get; } = 51123;` should be a better option. – Matías Fidemraizer Feb 05 '17 at 21:48