{"id":459,"date":"2009-09-22T00:36:41","date_gmt":"2009-09-21T23:36:41","guid":{"rendered":"http:\/\/blog.eweibel.net\/?p=459"},"modified":"2009-09-22T00:44:32","modified_gmt":"2009-09-21T23:44:32","slug":"fail-fast-principle","status":"publish","type":"post","link":"https:\/\/blog.eweibel.net\/?p=459","title":{"rendered":"Fail Fast principle"},"content":{"rendered":"<p>Recently I received a NullReference-Exception when I called another method from a foreign component. Fortunately I had the source code of this component and I found the following code:<br \/>\n[code language=&#8221;csharp&#8221;]<br \/>\npublic class AgeValidator<br \/>\n{<br \/>\n\tpublic Dictionary<string, int> Config { get; set; }<\/p>\n<p>\tpublic bool Validate(int nAge)<br \/>\n\t{<br \/>\n\t\tint nMinAge = FindValue(&#8220;MinAge&#8221;);<\/p>\n<p>\t\treturn nAge > nMinAge;<br \/>\n\t}<\/p>\n<p>\tprivate int FindValue(string strParameter)<br \/>\n\t{<br \/>\n\t\tint nValue = -1;<\/p>\n<p>\t\tif(this.Config.ContainsKey(strParameter)) nValue = this.Config[strParameter];<\/p>\n<p>\t\treturn nValue;<br \/>\n\t}<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>There are several problems with this code. First, there are no preconditions which checks if the object is in a valid state or the parameters are valid. This cause to behaviour which isn&#8217;t intended. The problem with a not available configuration could already detect in the Validate method. The Find method could check if the parameter is valid, if not it doesn&#8217;t make any sense to continue. The FindValue method doesn&#8217;t follow the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Fail-fast\">Fail Fast principle<\/a>. Jim Shore wrote an excellent <a href=\"http:\/\/martinfowler.com\/ieeeSoftware\/failFast.pdf\">article<\/a> about the Fail Fast principle. After a refactoring the code looks like that:<br \/>\n[code language=&#8221;csharp&#8221;]<br \/>\npublic class AgeValidator<br \/>\n{<br \/>\n\tpublic Dictionary<string, int> Config { get; set; }<\/p>\n<p>\tpublic bool Validate(int nAge)<br \/>\n\t{<br \/>\n\t\tif(this.Config == null || this.Config.Keys.Count == 0) throw new InvalidOperationException(&#8220;No valid configuration available.&#8221;);<\/p>\n<p>\t\tint nMinAge = FindValue(&#8220;MinAge&#8221;);<\/p>\n<p>\t\treturn nAge > nMinAge;<br \/>\n\t}<\/p>\n<p>\tprivate int FindValue(string strParameter)<br \/>\n\t{<br \/>\n\t\tif(string.IsNullOrEmpty(strParameter)) throw new ArgumentNullException(&#8220;strParameter&#8221;);<br \/>\n\t\tif(!this.Config.ContainsKey(strParameter)) throw new ArgumentException(string.Format(&#8220;No configuration for the key &#8216;{0}&#8217; found.&#8221;, strParameter));<\/p>\n<p>\t\treturn this.Config[strParameter];<br \/>\n\t}<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>What I advise junior software developers is to follow the Fail Fast principle, disciplined use of preconditions and if it adequate the use of postconditions. But all these advices don&#8217;t replacing unit testing.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently I received a NullReference-Exception when I called another method from a foreign component. Fortunately I had the source code of this component and I found the following code: [code language=&#8221;csharp&#8221;] public class AgeValidator { public Dictionary Config { get; set; } public bool Validate(int nAge) { int nMinAge = FindValue(&#8220;MinAge&#8221;); return nAge > nMinAge; } private int FindValue(string strParameter) { int nValue = -1; if(this.Config.ContainsKey(strParameter)) nValue = this.Config[strParameter]; return nValue; } } [\/code] There are several problems with this&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/blog.eweibel.net\/?p=459\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5,14],"tags":[],"class_list":["post-459","post","type-post","status-publish","format-standard","hentry","category-good-practices","category-software-engineering"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/plOV9-7p","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":942,"url":"https:\/\/blog.eweibel.net\/?p=942","url_meta":{"origin":459,"position":0},"title":"Enums and inheritance in .Net","author":"Patrick","date":"9. Feb 2011","format":false,"excerpt":"In one of my current projects I had the following code (I simplified the code a bit): public string ConnectionString { get { switch(this.Importer) { case Importer.SqlServer: return \"Server=localhost;Database=Northwind\"; case Importer.SqlServerOleDb: return\"Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=Northwind\"; default: throw new NotSupportedException( string.Format(\"Importer {0} is not supported yet.\", this.Importer)); } } } After running\u2026","rel":"","context":"In &quot;.NET&quot;","block_context":{"text":".NET","link":"https:\/\/blog.eweibel.net\/?cat=13"},"img":{"alt_text":"CodeCoverage","src":"https:\/\/i0.wp.com\/blog.eweibel.net\/wp-content\/uploads\/CodeCoverage_thumb.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/blog.eweibel.net\/wp-content\/uploads\/CodeCoverage_thumb.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/blog.eweibel.net\/wp-content\/uploads\/CodeCoverage_thumb.png?resize=525%2C300 1.5x"},"classes":[]},{"id":1182,"url":"https:\/\/blog.eweibel.net\/?p=1182","url_meta":{"origin":459,"position":1},"title":"Anti-Pattern &#8216;Validation by Execute &#8216;n&#8217; Rollback&#8217;","author":"Patrick","date":"21. Feb 2012","format":false,"excerpt":"Recently in some reviews I saw an anti-pattern. First you have to know, in the code, there was a validation of the data before it was stored in the database. So far so good. But when I looked at the validation code, I saw the following: public void Validate() {\u2026","rel":"","context":"In &quot;Anti patterns&quot;","block_context":{"text":"Anti patterns","link":"https:\/\/blog.eweibel.net\/?cat=8"},"img":{"alt_text":"Fotolia_20233238_S","src":"https:\/\/i0.wp.com\/blog.eweibel.net\/wp-content\/uploads\/Fotolia_20233238_S_thumb1.jpg?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":17,"url":"https:\/\/blog.eweibel.net\/?p=17","url_meta":{"origin":459,"position":2},"title":"Get the size of your tables","author":"Patrick","date":"17. Jan 2008","format":false,"excerpt":"Recently my boss asked me, why the databases (Microsoft SQL Server 2005) of our customers are so big. With the following SQL-Statements I could give more or less an answer to my boss. CREATE TABLE PW_SPACE( name varchar(255), rows int, reserved varchar(255), data varchar(255), index_size varchar(255), unused varchar(255) ) GO\u2026","rel":"","context":"In &quot;First experiencies&quot;","block_context":{"text":"First experiencies","link":"https:\/\/blog.eweibel.net\/?cat=7"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":376,"url":"https:\/\/blog.eweibel.net\/?p=376","url_meta":{"origin":459,"position":3},"title":"Mocking frameworks in .Net","author":"Patrick","date":"30. Mar 2010","format":false,"excerpt":"A few month ago I played with some mocking frameworks in .Net. There are already some comparisons available (here, here or here). In this blog post I want to show which frameworks are available and which one fits best for agile development. You could download the source code from github.com.\u2026","rel":"","context":"In &quot;.NET&quot;","block_context":{"text":".NET","link":"https:\/\/blog.eweibel.net\/?cat=13"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":622,"url":"https:\/\/blog.eweibel.net\/?p=622","url_meta":{"origin":459,"position":4},"title":"Branching practices","author":"Patrick","date":"6. Apr 2010","format":false,"excerpt":"In an environment where several developers work at the same code base or several features have to be implemented, then branching will be a topic. I was asked to create a branching guide for my current employer which use TFS as source code repository. Motivation In our projects I found\u2026","rel":"","context":"In &quot;Good practices&quot;","block_context":{"text":"Good practices","link":"https:\/\/blog.eweibel.net\/?cat=5"},"img":{"alt_text":"image","src":"https:\/\/i0.wp.com\/blog.eweibel.net\/wp-content\/uploads\/image_thumb3.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/blog.eweibel.net\/wp-content\/uploads\/image_thumb3.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/blog.eweibel.net\/wp-content\/uploads\/image_thumb3.png?resize=525%2C300 1.5x"},"classes":[]},{"id":34,"url":"https:\/\/blog.eweibel.net\/?p=34","url_meta":{"origin":459,"position":5},"title":"Are 100% code coverage reasonable?","author":"Patrick","date":"2. Mar 2009","format":false,"excerpt":"When you use a code coverage tool one of the first question is what is a good code coverage. Recently I listened to different podcasts (stackoverflow, scott hanselman) where they discuss this topic. I wasn't really surprised that there wasn't one unique opinion.One opinion was that 100% is a good\u2026","rel":"","context":"In &quot;Software architecture&quot;","block_context":{"text":"Software architecture","link":"https:\/\/blog.eweibel.net\/?cat=4"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=\/wp\/v2\/posts\/459","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=459"}],"version-history":[{"count":12,"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=\/wp\/v2\/posts\/459\/revisions"}],"predecessor-version":[{"id":471,"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=\/wp\/v2\/posts\/459\/revisions\/471"}],"wp:attachment":[{"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.eweibel.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}