<p>The code in <a href="https://stackoverflow.com/questions/17181/test-cases-vs-assertion-statement">this question</a> made me think</p>

<pre><code>assert(value&gt;0); //Precondition
if (value&gt;0)
{
  //Doit
}
</code></pre>

<p>I never write the if-statement. Asserting is enough/all you <em>can</em> do.
"Crash early, crash often"</p>

<p><a href="https://rads.stackoverflow.com/amzn/click/com/0735619670" rel="nofollow noreferrer" rel="nofollow noreferrer">CodeComplete</a> states:</p>

<ul>
<li>The assert-statement makes the application Correct </li>
<li>The if-test makes the application Robust</li>
</ul>

<p>I don't think you've made an application more robust by correcting invalid input values, or skipping code:</p>

<pre><code>assert(value &gt;= 0 );  //Precondition
assert(value &lt;= 90);  //Precondition
if(value &lt; 0)         //Just in case
  value = 0;
if (value &gt; 90)       //Just in case
  value = 90;
//Doit
</code></pre>

<p>These corrections are based on assumptions you made about the outside world.
Only the caller knows what "a valid input value" is for your function, and he must check its validity before he calls your function.</p>

<p>To paraphrase <a href="https://rads.stackoverflow.com/amzn/click/com/0735619670" rel="nofollow noreferrer" rel="nofollow noreferrer">CodeComplete</a>:
"Real-world programs <em>become</em> too messy when we <em>don't</em> rely solely on assertions."</p>

<p>Question: Am I wrong, stuborn, stupid, too non-defensive...</p>

[{'answer_id': 56173, 'author': 'Tnilsson', 'author_id': 4165, 'author_profile': 'https://Stackoverflow.com/users/4165', 'pm_score': 3, 'selected': False, 'text': '<p>The problem with trusting just Asserts, is that they may be turned off in a production environment. To quote the wikipedia article:</p>\n\n<blockquote>\n  <p>Most languages allow assertions to be\n  enabled or disabled globally, and\n  sometimes independently. Assertions\n  are often enabled during development\n  and disabled during final testing and\n  on release to the customer. Not\n  checking assertions avoiding the cost\n  of evaluating the assertions while,\n  assuming the assertions are free of\n  side effects, still producing the same\n  result under normal conditions. Under\n  abnormal conditions, disabling\n  assertion checking can mean that a\n  program that would have aborted will\n  continue to run. This is sometimes\n  preferable.\n  <a href="http://en.wikipedia.org/wiki/Assertion_(computing)" rel="noreferrer">Wikipedia</a></p>\n</blockquote>\n\n<p>So if the correctness of your code relies on the Asserts to be there you may run into serious problems. Sure, if the code worked during testing it should work during production... Now enter the second guy that works on the code and is just going to fix a small problem...</p>\n'}, {'answer_id': 56174, 'author': 'Rik', 'author_id': 5409, 'author_profile': 'https://Stackoverflow.com/users/5409', 'pm_score': 2, 'selected': False, 'text': '<p>I some cases, asserts are disabled when building for release. You may not have control over this (otherwise, you could build with asserts on), so it might be a good idea to do it like this.</p>\n\n<p>The problem with "correcting" the input values is that the caller will not get what they expect, and this can lead to problems or even crashes in wholly different parts of the program, making debugging a nightmare.</p>\n\n<p>I usually throw an exception in the if-statement to take over the role of the assert in case they are disabled  </p>\n\n<pre><code>assert(value&gt;0);\nif(value&lt;=0) throw new ArgumentOutOfRangeException("value");\n//do stuff\n</code></pre>\n'}, {'answer_id': 56178, 'author': 'Mendelt', 'author_id': 3320, 'author_profile': 'https://Stackoverflow.com/users/3320', 'pm_score': 1, 'selected': False, 'text': "<p>If I remember correctly from CS-class</p>\n\n<p>Preconditions define on what conditions the output of your function is defined. If you make your function handle errorconditions your function is defined for those condition and you don't need the assert statement.</p>\n\n<p>So I agree. Usually you don't need both.</p>\n\n<p>As Rik commented this can cause problems if you remove asserts in released code. Usually I don't do that except in performance-critical places.</p>\n"}, {'answer_id': 56179, 'author': 'Vinko Vrsalovic', 'author_id': 5190, 'author_profile': 'https://Stackoverflow.com/users/5190', 'pm_score': -1, 'selected': False, 'text': '<p>A problem with assertions is that they can (and usually will) be compiled out of the code, so you need to add both walls in case one gets thrown away by the compiler.</p>\n'}, {'answer_id': 56189, 'author': 'Andrew', 'author_id': 1389, 'author_profile': 'https://Stackoverflow.com/users/1389', 'pm_score': 1, 'selected': False, 'text': '<p>Don\'t forget that most languages allow you to turn off assertions...  Personally, if I was prepared to write if tests to protect against <strong>all</strong> ranges of invalid input, I wouldn\'t bother with the assertion in the first place.</p>\n\n<p>If, on the other hand you don\'t write logic to handle all cases (possibly because it\'s not sensible to try and continue with invalid input) then I would be using the assertion statement and going for the "fail early" approach.</p>\n'}, {'answer_id': 56192, 'author': 'Andrew Johnson', 'author_id': 5109, 'author_profile': 'https://Stackoverflow.com/users/5109', 'pm_score': 0, 'selected': False, 'text': "<p>For internal functions, ones that only you will use, use <strong>asserts</strong> only.  The asserts will help catch bugs during your testing, but won't hamper performance in production.</p>\n\n<p>Check inputs that originate externally with <strong>if-conditions</strong>.  By externally, that's anywhere outside the code that you/your team control and test.</p>\n\n<p>Optionally, you can have <strong>both</strong>.  This would be for external facing functions where integration testing is going to be done before production.</p>\n"}, {'answer_id': 56194, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 2, 'selected': False, 'text': '<p>I would disagree with this statement:</p>\n\n<blockquote>\n  <p>Only the caller knows what "a valid\n  input value" is for your function, and\n  he must check its validity before he\n  calls your function.</p>\n</blockquote>\n\n<p>Caller might <em>think</em> that he know that input value is correct. Only method author knows how it suppose to work. Programmer\'s best goal is to make client to fall into "<a href="http://blogs.msdn.com/brada/archive/2003/10/02/50420.aspx" rel="nofollow noreferrer">pit of success</a>". You should decide what behavior is more appropriate in given case. In some cases incorrect input values can be forgivable, in other you should throw exception\\return error.</p>\n\n<p>As for Asserts, I\'d repeat other commenters, assert is a <em>debug</em> time check for code author, not code clients.</p>\n'}, {'answer_id': 56243, 'author': 'jan', 'author_id': 1163, 'author_profile': 'https://Stackoverflow.com/users/1163', 'pm_score': 0, 'selected': False, 'text': '<p>I should have stated I was aware of the fact that asserts (here) dissappear in production code.</p>\n\n<p>If the if-statement actually corrects invalid input data in production code, this means the assert never went off during testing on debug code, this means you wrote code that you never executed.</p>\n\n<p>For me it\'s an OR situation:</p>\n\n<p>(quote Andrew) "protect against all ranges of invalid input, I wouldn\'t bother with the assertion in the first place." -> write an if-test.</p>\n\n<p>(quote aku) "incorrect input values can be forgivable" -> write an assert.</p>\n\n<p>I can\'t <em>stand</em> both...</p>\n'}, {'answer_id': 56252, 'author': 'Daren Thomas', 'author_id': 2260, 'author_profile': 'https://Stackoverflow.com/users/2260', 'pm_score': 2, 'selected': False, 'text': "<p>Use assertions for validating input you control: private methods and such.</p>\n\n<p>Use if statements for validating input you don't control: public interfaces designed for consumption by the user, user input testing etc.</p>\n\n<p>Test you application with assertions built in. Then deploy without the assertions.</p>\n"}]
['https://Stackoverflow.com/questions/56168', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1163/']