{"id":4967,"date":"2014-03-30T17:18:43","date_gmt":"2014-03-30T17:18:43","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/do-you-put-your-calculations-on-your-sets-or-your-gets-collection-of-common-programming-errors\/"},"modified":"2014-03-30T17:18:43","modified_gmt":"2014-03-30T17:18:43","slug":"do-you-put-your-calculations-on-your-sets-or-your-gets-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/do-you-put-your-calculations-on-your-sets-or-your-gets-collection-of-common-programming-errors\/","title":{"rendered":"do you put your calculations on your sets or your gets .-Collection of common programming errors"},"content":{"rendered":"<p>I would go with Charles Graham&#8217;s hybrid suggestion, but I want to add my two cents as to why.<\/p>\n<p>A lot of the above suggestions talk about complexity and optimization, but forget this all goes out the window when you factor in the consumer of your class. If <i>you<\/i> are the only consumer, and you used the first implementation, chances are you would remember to:<\/p>\n<pre><code>double subTotal = myOrder.TotalCash;\ndouble tax = subTotal * 0.05;\ndouble shipping = subTotal &gt; 100 ? 0 : 5.95;\ndouble grandTotal = subTotal + tax + shipping;\nOutputToUser(subTotal, tax, shipping, grandTotal);\n<\/code><\/pre>\n<p>Other people might not. Seeing that <code>myOrder.TotalCash<\/code> is a property, not a method, at least I would assume it is a cached value. That is, accessing <code>subTotal<\/code> in the above example is comparable in efficiency as accessing <code>myOrder.TotalCash<\/code>. Not realizing there&#8217;s a calculation going on behind the scenes, they write:<\/p>\n<pre><code>double tax = myOrder.TotalCash * 0.05;\ndouble shipping = myOrder.TotalCash &gt; 100 ? 0 : 5.95;\ndouble grandTotal = myOrder.TotalCash + tax + shipping;\nOutputToUser(myOrder.TotalCash, tax, shipping, grandTotal);\n<\/code><\/pre>\n<p>leaving <code>myOrder.TotalCash<\/code> to stand for the subtotal. Now, it has been calculated 4 times instead of 1.<\/p>\n<p>In summary, I&#8217;m sure I&#8217;m not the only one who believes that <b>a property represents a variable or cached value<\/b> and <b>a method processes something and returns a value<\/b>. It makes sense to store <code>CalculateTotalCash()<\/code> and only call it once, because you expect it to be a performance hit. On the other hand, you expect <code>TotalCash<\/code> to be a cached value and can access it at will. Thus it is important to only recalculate <code>TotalCash<\/code> when it changes.<\/p>\n<p>The hybrid suggestion wins in the case of multiple sets between reads. That way you&#8217;re not wasting time calculating a value to be thrown away.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I would go with Charles Graham&#8217;s hybrid suggestion, but I want to add my two cents as to why. A lot of the above suggestions talk about complexity and optimization, but forget this all goes out the window when you factor in the consumer of your class. If you are the only consumer, and you [&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-4967","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4967","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=4967"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4967\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}