Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Basics Making Properties Atomic

It's been given by Microsoft as a framework design guideline that properties should be independent of one another and not rely upon being set in any specific order.

Assume you have a triangle class that needs to support dimmensions and an area calculation. How would you model this?

This is of course the design that is considered gauche because Area is dependent on Base and Height being set first:

class Triangle{
    public double Base {get;set;}
    public double Height {get;set;}
    public double Area {
        get{
            return (Base * Height) / 2;
        }
    }
}

Assuming you use a constructor, you can ensure defaults for this case but is this the correct approach?

class Triangle{
    public Triangle(double b, double h){
        Base = b;
        Height = h;
    }

    public double Base {get;set;}
    public double Height {get;set;}
    public double Area {
        get{
            return (Base * Height) / 2;
        }
    }
}

You still have a property that has a dependency on other properties. In order to be a purist I can see only a few approaches (I guess they could be combined):

  1. Make Base/Height have readonly members that can only be set in the constructor

  2. Make the Area calculation into a method.

  3. Use some kind of factory pattern + readonly members to ensure that although the dependency may exist, the values can only be set by the method that instantiates the Triangle class.

Questions:

  1. Is the guideline practical (do you have to model a lot of complexity into your classes in order to support it)? [for example, a SqlConnection class allows you to initialize the connection string property but lets you change individual pieces of it such as command timeout]

  2. How do you manage keeping your properties independent of each other?

  3. Extra for people who use Silverlight / MVVM type architecture, do you accept dependencies in properties because of the way databinding works to a object? For example, binding a triangle instance that shows height, base and area on screen.

like image 518
t3rse Avatar asked Sep 11 '09 23:09

t3rse


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


2 Answers

Microsoft is really trying to say "Don't design your class in such a way that calling properties in an arbitrary order will cause unexpected behavior." Users of your class do not expect requesting a value (using a property) to have awkward side effects.

This falls under the principle of least surprise.

I think this is a completely practical guideline to follow. Properties should not have unexpected side effects.

You bring up an excellent question: "How do you manage keeping your properties independent of each other?". Very carefully! I eliminate state (and reduce the number of properties accordingly) as much as possible. Also, partitioning state by breaking up classes is another tactic. This would make an excellent question on its own...

In terms of the triangle class, I think both solutions you presented in code are valid. If it were up to me, I would design the triangle so that it would be immutable, and take in the width and height in the constructor.

like image 134
Robert Venables Avatar answered Nov 05 '22 07:11

Robert Venables


The simplest way would be to accept the base and height values only on the constructor and then expose all of them as read-only properties:

class Triangle
{
   private double base;
   private double height;

   private Triangle() { }

   public Triangle(double base, double height)
   {
      this.base = base;
      this.height = height;
   }

   public double Base 
   {
      get 
      {
         return this.base;
      }
   }

   public double Height 
   {
      get 
      {
         return this.height;
      }
   }

   public double Area 
   {
      get 
      {
         return (this.base * this.height) / 2;
      }
   }
}
like image 20
Scott Dorman Avatar answered Nov 05 '22 08:11

Scott Dorman