{"id":2791,"date":"2009-03-20T06:16:00","date_gmt":"2009-03-20T06:16:00","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/so-if-youre-not-going-to-use-design-patterns\/"},"modified":"2016-07-28T10:49:34","modified_gmt":"2016-07-28T10:49:34","slug":"so-if-youre-not-going-to-use-design-patterns","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/so-if-youre-not-going-to-use-design-patterns\/","title":{"rendered":"So if you&#8217;re not going to use design patterns?"},"content":{"rendered":"<p>I realised my last post was a bit negative, so I thought I&#8217;d do a quick one with my 2c about what you should do instead of following patterns. There obviously needs to be a way by which you can judge how good code is, a &#8220;design principle&#8221; if you like.<\/p>\n<p>In my world, there is only one design principle, from which all others can be derived:<\/p>\n<p><b>Encapsulation<\/b><\/p>\n<p>My definition of encapsulation is minimising the amount of code that has to know about a particular concept. &#8220;Concept&#8221;, here, is a very broad term, which can mean anything from<\/p>\n<ul>\n<li>a single rule about the behaviour of a program (some might call a piece of business logic)<\/li>\n<li>the existence of another component of the program, or a library<\/li>\n<li>the use of particular language features (threading is a good example here, I&#8217;ll blog about how one day)<\/li>\n<li>a use case of the program<\/li>\n<\/ul>\n<p>I&#8217;d say that code &#8220;knows about&#8221; a concept if it could ever interact with its behaviour. So if two methods in a class obviously use a concept, the rest of the class &#8220;knows about&#8221; it, because one day they might interact with it. Badly designed classes can leak interaction possibility, obviously we need to try to avoid that.<\/p>\n<p>Optimising a single measure, that sounds easy, right? Absolutely not! Like a good game of whack-a-mole, reducing the scope of one concept tends to increase the scope of another, and only finger-in-the-air style judgement can get you anywhere near the best overall.<\/p>\n<p>Like I say, all other design principles stem from encapsulation:<\/p>\n<ul>\n<li>Concise code reduces the amount of code the concepts in it are spread over (over-architecting can often reduce encapsultation in this way)<\/li>\n<li>Loosely coupled code splits concepts so less of them have to be in both places<\/li>\n<li>Repeated code obviously increases the number of places a concept is known about<\/li>\n<\/ul>\n<p>I&#8217;m sure there are more. Can you think of any principles you follow, but don&#8217;t come under encapsulation?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I realised my last post was a bit negative, so I thought I&#8217;d do a quick one with my 2c about what you should do instead of following patterns. There obviously needs to be a way by which you can judge how good code is, a &#8220;design principle&#8221; if you like. In my world, there&#8230;&hellip;<\/p>\n","protected":false},"author":5844,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[],"class_list":["post-2791","post","type-post","status-publish","format-standard","hentry","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2791","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/5844"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=2791"}],"version-history":[{"count":1,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2791\/revisions"}],"predecessor-version":[{"id":24760,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2791\/revisions\/24760"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=2791"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=2791"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=2791"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=2791"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}