tag:blogger.com,1999:blog-2944517286278650704.post72007408357581434..comments2024-03-12T11:19:33.349+00:00Comments on Life's too short for blogging...: Taking the pain out of parameter validationAnonymoushttp://www.blogger.com/profile/05091436116360365279noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-2944517286278650704.post-60337246400060846152015-05-29T16:50:44.146+01:002015-05-29T16:50:44.146+01:00Great solution Matt. I hope you don't mind tha...Great solution Matt. I hope you don't mind that I've created an online public git repository of the code in this post: https://bitbucket.org/Coilz/argument-validationAnonymoushttps://www.blogger.com/profile/18189392357897421594noreply@blogger.comtag:blogger.com,1999:blog-2944517286278650704.post-63490206735506652842014-10-23T09:21:01.850+01:002014-10-23T09:21:01.850+01:00Nice solution, have it found itself into any GitHu...Nice solution, have it found itself into any GitHub repo possibly?Anonymoushttps://www.blogger.com/profile/01349652462905703533noreply@blogger.comtag:blogger.com,1999:blog-2944517286278650704.post-16661855449987200892010-07-02T16:50:17.624+01:002010-07-02T16:50:17.624+01:00Hi Simon,
Yes I have heard of code contracts afte...Hi Simon,<br /><br />Yes I have heard of code contracts after the event and too late to introduce into our .net 3.5 projects. We may consider using it in any .net 4 projects.<br /><br />You may not have read my whole post but this API is in response to the repetitive and unreadable guard clause, that is exactly what you want to get away from. Public API methods are always littered with if null throw ArgumentNullException if that throw ArgumentException. It is much easier to have a static helper with with extension methods to help you do fluent validation.<br /><br />Surley it is much nicer to read:<br /><br />Validate.Argument(() => xyz)<br /> .IsNotNull()<br /> .Satisfies(param => param.ShouldHaveSomeValue);<br /><br />Than:<br /><br />if (xyz == null)<br />{<br /> throw new ArgumentNullException("xyz", "Parameter xyz cannot be null");<br />}<br /><br />if (xyz.ShouldHaveSomeValue)<br />{<br /> throw new ArgumentException("Parameter xyz should have some value", "xyz");<br />}<br /><br />With regard to unit testing and code coverage it is unaffected as the methods still throw ArgumentNull/Argument/ArgumentOutOfRangeExceptions.<br /><br />MattAnonymoushttps://www.blogger.com/profile/05091436116360365279noreply@blogger.comtag:blogger.com,1999:blog-2944517286278650704.post-22724786170201518372010-07-01T15:37:41.647+01:002010-07-01T15:37:41.647+01:00Hi Matt,
Have you heard of code contracts designe...Hi Matt,<br /><br />Have you heard of code contracts designed by Microsoft Research: http://research.microsoft.com/en-us/projects/contracts/<br /><br />This essentially allows static code analysis of parameters against a pre-determined contract and also supports runtime code analysis which makes the guard clauses redundent.<br /><br />You might also want to look at a simple Guard clause instead of a condition as this will make testing easier with better code coverage.<br /><br />SimonSimon Harthttps://www.blogger.com/profile/08526568682625258690noreply@blogger.com