{"id":4558,"date":"2014-03-30T13:32:05","date_gmt":"2014-03-30T13:32:05","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/tell-dont-ask-principle-and-password-expiration-collection-of-common-programming-errors\/"},"modified":"2014-03-30T13:32:05","modified_gmt":"2014-03-30T13:32:05","slug":"tell-dont-ask-principle-and-password-expiration-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/tell-dont-ask-principle-and-password-expiration-collection-of-common-programming-errors\/","title":{"rendered":"Tell, Don&#39;t Ask Principle and Password Expiration-Collection of common programming errors"},"content":{"rendered":"<p>I personally don&#8217;t like to program arround return values \/ <code>Enum<\/code> types. The more return types you have, the more paths you have to test \/ work with. Also, using exceptions to control flow is imo a bad practice (Unless you really can&#8217;t find any other option &#8211; but there&#8217;s usually a better one).<\/p>\n<p>An expired password is not really exceptional to me. Its a valid state after all (Or else you&#8217;d do something against passwords to expire at all)<\/p>\n<p>I try to keep it simple and either return a <code>bool<\/code> or something like a <code>Func<\/code> which can be directly invoked by the caller.<\/p>\n<p>Probably something like that:<\/p>\n<pre><code>public class User\n    {\n        private DateTime _lastChangeDate;\n        public Action Validate()\n        {\n            if (_lastChangeDate &gt;= DateTime.Now.AddDays(-30))\n            {\n                return new Action(() =&gt; this.Login());\n            }\n            else\n            {\n                return new Action(() =&gt; this.ChangePassword());\n            }\n        }\n        private void Login()\n        {\n            Console.WriteLine(\"Login\");\n        }\n        private void ChangePassword()\n        {\n            Console.WriteLine(\"Change Password\");\n        }\n    }\n<\/code><\/pre>\n<p>On the caller side:<\/p>\n<pre><code>user.Validate().Invoke();\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I personally don&#8217;t like to program arround return values \/ Enum types. The more return types you have, the more paths you have to test \/ work with. Also, using exceptions to control flow is imo a bad practice (Unless you really can&#8217;t find any other option &#8211; but there&#8217;s usually a better one). An [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4558","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4558","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4558"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4558\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4558"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4558"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4558"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}