<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Code Contracts #6: Modelling constraints and state</title>
	<atom:link href="http://www.minddriven.de/index.php/technology/dot-net/code-contracts/code-contracts-modelling-constraints-and-state/feed" rel="self" type="application/rss+xml" />
	<link>http://www.minddriven.de/index.php/technology/dot-net/code-contracts/code-contracts-modelling-constraints-and-state</link>
	<description>Matthias Jauernig -- .NET technology, architecture and design</description>
	<lastBuildDate>Thu, 02 Feb 2012 06:42:23 +0100</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
	<item>
		<title>By: Matthias Jauernig</title>
		<link>http://www.minddriven.de/index.php/technology/dot-net/code-contracts/code-contracts-modelling-constraints-and-state/comment-page-1#comment-1633</link>
		<dc:creator>Matthias Jauernig</dc:creator>
		<pubDate>Sat, 16 May 2009 17:28:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.leading-edge-dev.de/?p=363#comment-1633</guid>
		<description>I&#039;ve updated the example and put in more information. Waiting for further comments and thoughts :-)</description>
		<content:encoded><![CDATA[<p>I&#8217;ve updated the example and put in more information. Waiting for further comments and thoughts <img src='http://www.minddriven.de/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matthias Jauernig</title>
		<link>http://www.minddriven.de/index.php/technology/dot-net/code-contracts/code-contracts-modelling-constraints-and-state/comment-page-1#comment-1489</link>
		<dc:creator>Matthias Jauernig</dc:creator>
		<pubDate>Mon, 11 May 2009 20:28:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.leading-edge-dev.de/?p=363#comment-1489</guid>
		<description>Hi there,

Thanks for your loong comment. I really enjoy your comments! Discussion is the foundation of every further development. 
In my opinion, the empty EMail object can sometimes not only be necessary in cases where my API expects it. Moreover, a really important case in my opinion is handling data through any kind of UI. 
In a broader range, sometimes it would be necessary to have an empty or temporarily invalid object, thus to bring direct data binding of this object to the UI (e.g. Silverlight) to life. This would temporarily break the contract of the object, but that&#039;s ok, since the user has recently inserted the new/empty item and is now editing it and brings it to valid state. I don&#039;t see that this can easily be managed through contracts directly by using invariants.

Either way, I think we are both agreeing on the &quot;it depends&quot; term. It depends on the situation where to have contracts and for what  to have them. If you like, I would exchange some more information with you via E-Mail (please not empty ;-)), thus to make up my mind even further. There are many questions and I think through discussion I could get a deeper insight. If you like, please send a short ping to (moved).

Thanks, Matthias</description>
		<content:encoded><![CDATA[<p>Hi there,</p>
<p>Thanks for your loong comment. I really enjoy your comments! Discussion is the foundation of every further development.<br />
In my opinion, the empty EMail object can sometimes not only be necessary in cases where my API expects it. Moreover, a really important case in my opinion is handling data through any kind of UI.<br />
In a broader range, sometimes it would be necessary to have an empty or temporarily invalid object, thus to bring direct data binding of this object to the UI (e.g. Silverlight) to life. This would temporarily break the contract of the object, but that&#8217;s ok, since the user has recently inserted the new/empty item and is now editing it and brings it to valid state. I don&#8217;t see that this can easily be managed through contracts directly by using invariants.</p>
<p>Either way, I think we are both agreeing on the &#8220;it depends&#8221; term. It depends on the situation where to have contracts and for what  to have them. If you like, I would exchange some more information with you via E-Mail (please not empty <img src='http://www.minddriven.de/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> ), thus to make up my mind even further. There are many questions and I think through discussion I could get a deeper insight. If you like, please send a short ping to (moved).</p>
<p>Thanks, Matthias</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pop Catalin</title>
		<link>http://www.minddriven.de/index.php/technology/dot-net/code-contracts/code-contracts-modelling-constraints-and-state/comment-page-1#comment-1479</link>
		<dc:creator>Pop Catalin</dc:creator>
		<pubDate>Mon, 11 May 2009 12:58:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.leading-edge-dev.de/?p=363#comment-1479</guid>
		<description>&gt; I see, that you’re using the object invariants for some kind of data validation

Yes it is validation, but not runtime validation instead contractual validation (All contracts are after all data validation ( For example method parameters store data, and you validate the values of the parameters, Contracts add &quot;data&quot; constraints (shape, size, etc) as opposed to  only type constraints which a typed language already has)).

&gt; I could imagine cases of empty EMail objects

This depends on what your API expects, I could just as well design an API that doen&#039;t handle empty mail messages and express that as a contract, on the other hand if I&#039;d design the API to handle empty mail messages I wouldn&#039;t express that as a contract, but some form of runtime validation where necessary, or method contracts. 

&gt; But should this be modelled as invariant? What’s your opinion about this?

Not necessarily as an invariant, you could model it as a contract on the Send() Method for example which only accepts mails that have all the required fields. But the way you design it, it is a choice basically, and I think there are advantages to modeling a eMail class as invariant as it can potentially simplify the validations the code has to to in various places, and bring a bit more consistency to the API (but only if handling empty mail messages is not required)

Anyway great series of posts, and I think the exploration of what code contracts could potentially do to make code better has just begun, and the do&#039;s and don&#039;ts still need to be discovered for the largest part.</description>
		<content:encoded><![CDATA[<p>&gt; I see, that you’re using the object invariants for some kind of data validation</p>
<p>Yes it is validation, but not runtime validation instead contractual validation (All contracts are after all data validation ( For example method parameters store data, and you validate the values of the parameters, Contracts add &#8220;data&#8221; constraints (shape, size, etc) as opposed to  only type constraints which a typed language already has)).</p>
<p>&gt; I could imagine cases of empty EMail objects</p>
<p>This depends on what your API expects, I could just as well design an API that doen&#8217;t handle empty mail messages and express that as a contract, on the other hand if I&#8217;d design the API to handle empty mail messages I wouldn&#8217;t express that as a contract, but some form of runtime validation where necessary, or method contracts. </p>
<p>&gt; But should this be modelled as invariant? What’s your opinion about this?</p>
<p>Not necessarily as an invariant, you could model it as a contract on the Send() Method for example which only accepts mails that have all the required fields. But the way you design it, it is a choice basically, and I think there are advantages to modeling a eMail class as invariant as it can potentially simplify the validations the code has to to in various places, and bring a bit more consistency to the API (but only if handling empty mail messages is not required)</p>
<p>Anyway great series of posts, and I think the exploration of what code contracts could potentially do to make code better has just begun, and the do&#8217;s and don&#8217;ts still need to be discovered for the largest part.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matthias Jauernig</title>
		<link>http://www.minddriven.de/index.php/technology/dot-net/code-contracts/code-contracts-modelling-constraints-and-state/comment-page-1#comment-1477</link>
		<dc:creator>Matthias Jauernig</dc:creator>
		<pubDate>Mon, 11 May 2009 12:14:45 +0000</pubDate>
		<guid isPermaLink="false">http://www.leading-edge-dev.de/?p=363#comment-1477</guid>
		<description>Hi Pop,

Nice to see your example. I see, that you&#039;re using the object invariants for some kind of data validation, right? I don&#039;t know if I would use them in the same way. In general I would say: it depends :-)

I could imagine cases of empty EMail objects, where the invariants don&#039;t fit. Just when I want to save them, I would validate the EMail object. Especially (besides of this EMail example) if I have depending constraints and would bind the object to an UI, perhaps I must be able to temporarily break one constraint, if I change a value, until I change the value of a depending property. 

Thus there could be problems that arise by handling data validation through invariants. It generally leads to the question: what to model through contracts and there is just few concrete information/best practices on this. Well, it really depends, but if there would be some showcases, in my opinion this would really help. First programmers and second DbC itself to get more popular.

~ Matthias</description>
		<content:encoded><![CDATA[<p>Hi Pop,</p>
<p>Nice to see your example. I see, that you&#8217;re using the object invariants for some kind of data validation, right? I don&#8217;t know if I would use them in the same way. In general I would say: it depends <img src='http://www.minddriven.de/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>I could imagine cases of empty EMail objects, where the invariants don&#8217;t fit. Just when I want to save them, I would validate the EMail object. Especially (besides of this EMail example) if I have depending constraints and would bind the object to an UI, perhaps I must be able to temporarily break one constraint, if I change a value, until I change the value of a depending property. </p>
<p>Thus there could be problems that arise by handling data validation through invariants. It generally leads to the question: what to model through contracts and there is just few concrete information/best practices on this. Well, it really depends, but if there would be some showcases, in my opinion this would really help. First programmers and second DbC itself to get more popular.</p>
<p>~ Matthias</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pop Catalin</title>
		<link>http://www.minddriven.de/index.php/technology/dot-net/code-contracts/code-contracts-modelling-constraints-and-state/comment-page-1#comment-1476</link>
		<dc:creator>Pop Catalin</dc:creator>
		<pubDate>Mon, 11 May 2009 12:03:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.leading-edge-dev.de/?p=363#comment-1476</guid>
		<description>Well I think I&#039;d go for an example that involves a EMail class:

public class EMail {
        public string To { get ... set ... 
        public string Subject { get ... set ...
        public string Body { get ... set ...

        [ContractInvariantMethod]
        protected void ClassConstraints()
         {
             Contract.Invariant(To != null);
             Contract.Invariant(Subject != null &#124;&#124; Body != null);
          }
}</description>
		<content:encoded><![CDATA[<p>Well I think I&#8217;d go for an example that involves a EMail class:</p>
<p>public class EMail {<br />
        public string To { get &#8230; set &#8230;<br />
        public string Subject { get &#8230; set &#8230;<br />
        public string Body { get &#8230; set &#8230;</p>
<p>        [ContractInvariantMethod]<br />
        protected void ClassConstraints()<br />
         {<br />
             Contract.Invariant(To != null);<br />
             Contract.Invariant(Subject != null || Body != null);<br />
          }<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matthias Jauernig</title>
		<link>http://www.minddriven.de/index.php/technology/dot-net/code-contracts/code-contracts-modelling-constraints-and-state/comment-page-1#comment-1474</link>
		<dc:creator>Matthias Jauernig</dc:creator>
		<pubDate>Mon, 11 May 2009 11:31:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.leading-edge-dev.de/?p=363#comment-1474</guid>
		<description>Hi Pop,

I definitely think the same way because of the same reasons you mentioned (following the LSP)! This example is not to show good code, but just to have a showcase for the problem at hand. Personally, I wouldn&#039;t implement Square in this way,.

Perhaps I&#039;ll find another (better) example. If you have any ideas, please let me know :-)

~ Matthias</description>
		<content:encoded><![CDATA[<p>Hi Pop,</p>
<p>I definitely think the same way because of the same reasons you mentioned (following the LSP)! This example is not to show good code, but just to have a showcase for the problem at hand. Personally, I wouldn&#8217;t implement Square in this way,.</p>
<p>Perhaps I&#8217;ll find another (better) example. If you have any ideas, please let me know <img src='http://www.minddriven.de/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>~ Matthias</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pop Catalin</title>
		<link>http://www.minddriven.de/index.php/technology/dot-net/code-contracts/code-contracts-modelling-constraints-and-state/comment-page-1#comment-1472</link>
		<dc:creator>Pop Catalin</dc:creator>
		<pubDate>Mon, 11 May 2009 10:24:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.leading-edge-dev.de/?p=363#comment-1472</guid>
		<description>Slightly off topic, but ...
I really think having a Square class that has Width and 
Height writable properties is a bad way to design a square class ... It&#039;s definably a bad way to design because you have two dimensions that are constrained to be the same, while mathematically speaking a square has only one defining dimension: edge length. 

What you are doing is a Rectangle that is constrained to be a square (a special case for rectangle), which breaks Liskov substitution principle. 

The Square inherits Rectangle is a classical &quot;what you should not do&quot; object oriented design problem ... 

The consumers of the Rectangle class expect that all inheritors of the class respect the contracts the base class imposes, and in this case the Square class, breaks the parent class imposed contracts, by narrowing the contract accepted input, anyone working polimorphically with the Rectangle class will run into issues when a Square is received. 

Classic example of Liskov substitution principle being violated:
http://www.oodesign.com/liskov-s-substitution-principle.html</description>
		<content:encoded><![CDATA[<p>Slightly off topic, but &#8230;<br />
I really think having a Square class that has Width and<br />
Height writable properties is a bad way to design a square class &#8230; It&#8217;s definably a bad way to design because you have two dimensions that are constrained to be the same, while mathematically speaking a square has only one defining dimension: edge length. </p>
<p>What you are doing is a Rectangle that is constrained to be a square (a special case for rectangle), which breaks Liskov substitution principle. </p>
<p>The Square inherits Rectangle is a classical &#8220;what you should not do&#8221; object oriented design problem &#8230; </p>
<p>The consumers of the Rectangle class expect that all inheritors of the class respect the contracts the base class imposes, and in this case the Square class, breaks the parent class imposed contracts, by narrowing the contract accepted input, anyone working polimorphically with the Rectangle class will run into issues when a Square is received. </p>
<p>Classic example of Liskov substitution principle being violated:<br />
<a href="http://www.oodesign.com/liskov-s-substitution-principle.html" rel="nofollow">http://www.oodesign.com/liskov-s-substitution-principle.html</a></p>
]]></content:encoded>
	</item>
</channel>
</rss>

