<p>Do you use Design by Contract professionally? Is it something you have to do from the beginning of a project, or can you change gears and start to incorporate it into your software development lifecycle? What have you found to be the pros/cons of the design approach?</p>

<p>I came across the <a href="http://en.wikipedia.org/wiki/Design_by_contract" rel="noreferrer">Design by Contract</a> approach in a grad school course. In the academic setting, it seemed to be a pretty useful technique. But I don't currently use Design by Contract professionally, and I don't know any other developers that are using it. It would be good to hear about its actual usage from the SO crowd.</p>

[{'answer_id': 26484, 'author': 'James A. Rosen', 'author_id': 1190, 'author_profile': 'https://Stackoverflow.com/users/1190', 'pm_score': 5, 'selected': True, 'text': "<p>I can't recommend it highly enough.  It's particularly nice if you have a suite that takes inline documentation contract specifications, like so:</p>\n\n<pre><code>// @returns null iff x = 0\npublic foo(int x) {\n  ...\n}\n</code></pre>\n\n<p>and turns them into generated unit tests, like so:</p>\n\n<pre><code>public test_foo_returns_null_iff_x_equals_0() {\n  assertNull foo(0);\n}\n</code></pre>\n\n<p>That way, you can actually see the tests you're running, but they're auto-generated.  Generated tests shouldn't be checked into source control, by the way.</p>\n"}, {'answer_id': 26485, 'author': 'Greg Hurlman', 'author_id': 35, 'author_profile': 'https://Stackoverflow.com/users/35', 'pm_score': 2, 'selected': False, 'text': "<p>It's absolutely foolish to not design by contract when doing anything in an SOA realm, and it's always helpful if you're working on any sort of modular work, where bits &amp; pieces might be swapped out later on, especially if any black boxen are involved.</p>\n"}, {'answer_id': 26542, 'author': 'Frank Krueger', 'author_id': 338, 'author_profile': 'https://Stackoverflow.com/users/338', 'pm_score': 1, 'selected': False, 'text': '<p>In lieu of more expressive type systems, I would absolutely use design by contract on military grade projects.</p>\n\n<p>For weakly typed languages or languages with dynamic scope (PHP, JavaScript), functional contracts are also very handy.</p>\n\n<p>For everything else, I would toss it aside an rely upon beta testers and unit tests.</p>\n\n<p><a href="https://stackoverflow.com/users/1190/gaius">Gaius</a>: A Null Pointer exception gets thrown for you automatically by the runtime, there is no benefit to testing that stuff in the function prologue. If you are more interested in documentation, then I would use annotations that can be used with static analyzers and the like (to make sure the code isn\'t breaking your annotations for example).</p>\n\n<p>A stronger type system coupled with Design by Contract seems to be the way to go. Take a look at <a href="http://research.microsoft.com/SpecSharp/" rel="nofollow noreferrer">Spec#</a> for an example:</p>\n\n<blockquote>\n  <p>The Spec# programming language.  Spec#\n  is an extension of the object-oriented\n  language C#.  It extends the type\n  system to include non-null types and\n  checked exceptions.  It provides\n  method contracts in the form of pre-\n  and postconditions as well as object\n  invariants.</p>\n</blockquote>\n'}, {'answer_id': 26890, 'author': 'Brad Gilbert', 'author_id': 1337, 'author_profile': 'https://Stackoverflow.com/users/1337', 'pm_score': 0, 'selected': False, 'text': '<p>I don\'t actually use Design by Contract, on a daily basis. I do, however know that it has been incorporated into the <a href="http://www.digitalmars.com/d/2.0/dbc.html" rel="nofollow noreferrer">D</a> language, as part of the language.</p>\n'}, {'answer_id': 26965, 'author': 'popopome', 'author_id': 1556, 'author_profile': 'https://Stackoverflow.com/users/1556', 'pm_score': 2, 'selected': False, 'text': '<p>If you look into STL, boost, MFC, ATL and many open source projects, you can see there are so many ASSERTION statements and that makes project going further more safely.</p>\n\n<p>Design-By-Contract! It really works in real product.</p>\n'}, {'answer_id': 34811, 'author': 'James A. Rosen', 'author_id': 1190, 'author_profile': 'https://Stackoverflow.com/users/1190', 'pm_score': 2, 'selected': False, 'text': '<p><a href="https://stackoverflow.com/users/338/frank-krueger">Frank Krueger</a> writes:</p>\n\n<blockquote>\n  <p>Gaius: A Null Pointer exception gets thrown for you automatically by the runtime, there is no benefit to testing that stuff in the function prologue.</p>\n</blockquote>\n\n<p>I have two responses to this:</p>\n\n<ol>\n<li><p>Null was just an example.  For square(x), I\'d want to test that the square root of the result is (approximately) the value of the parameter.  For setters, I\'d want to test that the value actually changed.  For atomic operations, I\'d want to check that all component operations succeeded or all failed (really one test for success and n tests for failure).  For factory methods in weakly-typed languages, I want to check that the right kind of object is returned.  The list goes on and on.  Basically, anything that can be tested in one line of code is a very good candidate for a code contract in a prologue comment.</p></li>\n<li><p>I disagree that you shouldn\'t test things because they generate runtime exceptions.  If anything, you <em>should</em> test things that might generate runtime exceptions.  I like runtime exceptions because they make the system <a href="http://en.wikipedia.org/wiki/Fail-fast" rel="nofollow noreferrer">fail fast</a>, which helps debugging.  But the <code>null</code> in the example was a result value for some possible input.  There\'s an argument to be made for never returning <code>null</code>, but if you\'re going to, you should test it.</p></li>\n</ol>\n'}, {'answer_id': 123667, 'author': 'Ged Byrne', 'author_id': 10167, 'author_profile': 'https://Stackoverflow.com/users/10167', 'pm_score': 4, 'selected': False, 'text': "<p>You really get to appreciate design by contract when you have an interface between to applications that have to talk to each other.</p>\n\n<p>Without contracts this situation quickly becomes a game of blame tennis.  The teams keep knocking accusations back and forth and huge amounts of time get wasted.</p>\n\n<p>With a contract, the blame is clear.</p>\n\n<p>Did the caller satisfy the preconditions?  If not the client team need to fix it.</p>\n\n<p>Given a valid request, did the receiver satisfy the post conditions?  If not the server team need to fix that.</p>\n\n<p>Did both parties adhere to the contract, but the result is unsatisfactory?  The contract is insufficient and the issue needs to be escalated.</p>\n\n<p>For this you don't need to have the contracts implemented in the form of assertions, you just need to make sure they are documented and agreed on by all parties.</p>\n"}, {'answer_id': 1526669, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>Both Unit testing and Design by Contract are valuable test approaches in my experince.</p>\n\n<p>I have tried using Design by Contract in a System Automatic Testing framework and my experience is that is gives a flexibility and possibilities not easily obtained by unit testing. For example its possible to run longer sequence and verify that \nthe respons times are within limits every time an action is executed.</p>\n\n<p>Looking at the presentations at InfoQ it appears that Design by contract is a valuable addition to the conventional Unit tests in the integration phase of components.\nFor example it possible to create a mock interface first and then use the component after-\nor when a new version of a component is released.</p>\n\n<p>I have not found a toolkit covering all my design requirement to design by contract testing\nin the .Net/Microsoft platform.</p>\n'}, {'answer_id': 2158480, 'author': 'Verhagen', 'author_id': 261378, 'author_profile': 'https://Stackoverflow.com/users/261378', 'pm_score': 0, 'selected': False, 'text': '<p>Yes, it does! Actually a few years ago, I designed a little framework for Argument Validation. I was doing a SOA project, in which the different back-end system, did all kind of validation and checking. But to increase response times (in cases where the input was invalid, and to reduce to load those back-end systems), we started to validate the input parameters of the provided services. Not only for Not Null, but also for String patterns. Or values from within sets. And also the cases where parameters had dependencies between them.</p>\n\n<p>Now I realize we implemented at that time a small design by contract framework :)</p>\n\n<p>Here is the link for those who are interested in the small <a href="http://java-arg-val.sourceforge.net/" rel="nofollow noreferrer">Java Argument Validation</a> framework. Which is implemented as plain Java solution.</p>\n'}, {'answer_id': 4191245, 'author': 'necode', 'author_id': 414678, 'author_profile': 'https://Stackoverflow.com/users/414678', 'pm_score': 1, 'selected': False, 'text': "<p>I find it telling that Go programming language has no constructs that make design by contract possible. panic/defer/recover aren't exactly that as defer and recover logic make it possible to ignore panic, IOW to ignore broken contract. What's needed at very least is some form of unrecoverable panic, which is assert really. Or, at best, direct language support of design by contract constructs (pre and post-conditions, implementation and class invariants). But given strong-headedness of language purists at the helm of Go ship, I give little change of any of this done.</p>\n\n<p>One can implement assert-like behaviour by checking for special assert error in last defer function in panicking function and calling runtime.Breakpoint() to dump stack during recovery. To be assert-like that behaviour needs to be conditional. Of course this approach fells apart when new defer function is added after the one doing assert. Which will happen in large project exactly at the wrong time, resulting in missed bugs.</p>\n\n<p>My point is that is that assert is useful in so many ways that having to dance around it may be a headache.</p>\n"}]
['https://Stackoverflow.com/questions/26455', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']