<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Coding: Reduce fields, delay calculations</title>
	<atom:link href="http://www.markhneedham.com/blog/2009/09/02/coding-reduce-fields-delay-calculations/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.markhneedham.com/blog/2009/09/02/coding-reduce-fields-delay-calculations/</link>
	<description>Thoughts on Software Development</description>
	<lastBuildDate>Sat, 11 Feb 2012 23:17:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Scala: val, lazy val and def at Mark Needham</title>
		<link>http://www.markhneedham.com/blog/2009/09/02/coding-reduce-fields-delay-calculations/comment-page-1/#comment-124267</link>
		<dc:creator>Scala: val, lazy val and def at Mark Needham</dc:creator>
		<pubDate>Wed, 22 Jun 2011 23:08:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1602#comment-124267</guid>
		<description>[...] In Java, C# or Ruby I would definitely favour the 3rd option because it reduces the amount of state that an object has to hold. [...]</description>
		<content:encoded><![CDATA[<p>[...] In Java, C# or Ruby I would definitely favour the 3rd option because it reduces the amount of state that an object has to hold. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Testability: How much logic belongs in the constructor? &#171; Alexandre Gazola</title>
		<link>http://www.markhneedham.com/blog/2009/09/02/coding-reduce-fields-delay-calculations/comment-page-1/#comment-22932</link>
		<dc:creator>Testability: How much logic belongs in the constructor? &#171; Alexandre Gazola</dc:creator>
		<pubDate>Wed, 23 Sep 2009 22:35:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1602#comment-22932</guid>
		<description>[...] should do no real work. Mark Needham, in turn, advocates the simplification of constructors by delaying calculations. He points out the idea of reducing field calculation in the constructor, only calculating those [...]</description>
		<content:encoded><![CDATA[<p>[...] should do no real work. Mark Needham, in turn, advocates the simplification of constructors by delaying calculations. He points out the idea of reducing field calculation in the constructor, only calculating those [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Coding: Watch out for mutable code at Mark Needham</title>
		<link>http://www.markhneedham.com/blog/2009/09/02/coding-reduce-fields-delay-calculations/comment-page-1/#comment-22636</link>
		<dc:creator>Coding: Watch out for mutable code at Mark Needham</dc:creator>
		<pubDate>Wed, 16 Sep 2009 13:44:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1602#comment-22636</guid>
		<description>[...] been doing some more work recently on trying to reduce the number of fields in some of our classes and moving any logic related to calculations into... but managed to break part of our application recently by doing that a bit too casually and not [...]</description>
		<content:encoded><![CDATA[<p>[...] been doing some more work recently on trying to reduce the number of fields in some of our classes and moving any logic related to calculations into&#8230; but managed to break part of our application recently by doing that a bit too casually and not [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Coding: An abstract class/ASP.NET MVC dilemma at Mark Needham</title>
		<link>http://www.markhneedham.com/blog/2009/09/02/coding-reduce-fields-delay-calculations/comment-page-1/#comment-22466</link>
		<dc:creator>Coding: An abstract class/ASP.NET MVC dilemma at Mark Needham</dc:creator>
		<pubDate>Sat, 12 Sep 2009 14:20:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1602#comment-22466</guid>
		<description>[...] previously described a refactoring that we have been working on to reduce the number of fields and delay calculations and the actual goal behind this refactoring was to get the code into shape so that we could add in [...]</description>
		<content:encoded><![CDATA[<p>[...] previously described a refactoring that we have been working on to reduce the number of fields and delay calculations and the actual goal behind this refactoring was to get the code into shape so that we could add in [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mark Needham</title>
		<link>http://www.markhneedham.com/blog/2009/09/02/coding-reduce-fields-delay-calculations/comment-page-1/#comment-22462</link>
		<dc:creator>Mark Needham</dc:creator>
		<pubDate>Sat, 12 Sep 2009 11:58:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1602#comment-22462</guid>
		<description>@Dave Didn&#039;t see your second reply until today! Coupling wise I was thinking that if you stored it that meant a greater coupling but I think you&#039;re right it knows about the Dependency so there&#039;s no difference.</description>
		<content:encoded><![CDATA[<p>@Dave Didn&#8217;t see your second reply until today! Coupling wise I was thinking that if you stored it that meant a greater coupling but I think you&#8217;re right it knows about the Dependency so there&#8217;s no difference.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave Cameron</title>
		<link>http://www.markhneedham.com/blog/2009/09/02/coding-reduce-fields-delay-calculations/comment-page-1/#comment-22291</link>
		<dc:creator>Dave Cameron</dc:creator>
		<pubDate>Sun, 06 Sep 2009 19:15:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1602#comment-22291</guid>
		<description>Very much agree on the Law of Demeter violation. From the example, CalculateValueFrom() really looks like it wants to live on dependency1.</description>
		<content:encoded><![CDATA[<p>Very much agree on the Law of Demeter violation. From the example, CalculateValueFrom() really looks like it wants to live on dependency1.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave Cameron</title>
		<link>http://www.markhneedham.com/blog/2009/09/02/coding-reduce-fields-delay-calculations/comment-page-1/#comment-22289</link>
		<dc:creator>Dave Cameron</dc:creator>
		<pubDate>Sun, 06 Sep 2009 19:14:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1602#comment-22289</guid>
		<description>@Mark

In what way does storing the object increase the coupling? In both cases, SomeObject knows about Dependency1 and calls one of its methods. The difference is in when it is called, but not what is called.

There is some run-time relationship difference, since the reference to Dependency1 means it will be garbage collected sometime after SomeObject is collected. Before, it may have been collected after SomeObject&#039;s constructor ran. This isn&#039;t what I normally think of as coupling though.

@bogdan

Thinking about it a bit more, your comment shows a type of coupling between the objects none of us have called out yet. By requiring SomeObject to know that Dependency1.Calculation1() always returns the same value, the version which stores the value in a field creates an implicit coupling between SomeObject and Dependency1. SomeObject needs to know that Calculation1() is implemented in such a way that the value does not change on repeated calls.</description>
		<content:encoded><![CDATA[<p>@Mark</p>
<p>In what way does storing the object increase the coupling? In both cases, SomeObject knows about Dependency1 and calls one of its methods. The difference is in when it is called, but not what is called.</p>
<p>There is some run-time relationship difference, since the reference to Dependency1 means it will be garbage collected sometime after SomeObject is collected. Before, it may have been collected after SomeObject&#8217;s constructor ran. This isn&#8217;t what I normally think of as coupling though.</p>
<p>@bogdan</p>
<p>Thinking about it a bit more, your comment shows a type of coupling between the objects none of us have called out yet. By requiring SomeObject to know that Dependency1.Calculation1() always returns the same value, the version which stores the value in a field creates an implicit coupling between SomeObject and Dependency1. SomeObject needs to know that Calculation1() is implemented in such a way that the value does not change on repeated calls.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave Cameron</title>
		<link>http://www.markhneedham.com/blog/2009/09/02/coding-reduce-fields-delay-calculations/comment-page-1/#comment-22288</link>
		<dc:creator>Dave Cameron</dc:creator>
		<pubDate>Sun, 06 Sep 2009 19:08:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1602#comment-22288</guid>
		<description>@bogdan, @mark, @mike

I have been misled by assuming a repeated calculation will take more time than storing a calculated value in a field. Memory is not uniformly speedy. Even if a calculation always returns the same value, it can be faster to repeat a calculation thousands of times rather than fetching it from memory. It depends on caching, CLR behaviour, memory layout and so many other factors that predicting it ahead of time is quite difficult. 

At the same time, predictions of some accuracy can be made by looking at the amount of code involved. If a calculation is a whole class on its own with hundreds of lines, that could make sense to save in a field. If the calculation is only a handful of operations, like adding and multiplying two numbers, it may not make sense.

I try to strive for code that only expresses the functional goal. Afterwards, if necessary, optimizations like caching calculations in fields can be made.

In the specific example Mark is discussing, SomeObject is intended to be an adapter or aggregate that exposes several information sources to a consumer. It&#039;s not intended to be an information holder for the information. And, there is already caching underneath the Dependency1 and Dependency2 classes, as they do abstract away some network calls.</description>
		<content:encoded><![CDATA[<p>@bogdan, @mark, @mike</p>
<p>I have been misled by assuming a repeated calculation will take more time than storing a calculated value in a field. Memory is not uniformly speedy. Even if a calculation always returns the same value, it can be faster to repeat a calculation thousands of times rather than fetching it from memory. It depends on caching, CLR behaviour, memory layout and so many other factors that predicting it ahead of time is quite difficult. </p>
<p>At the same time, predictions of some accuracy can be made by looking at the amount of code involved. If a calculation is a whole class on its own with hundreds of lines, that could make sense to save in a field. If the calculation is only a handful of operations, like adding and multiplying two numbers, it may not make sense.</p>
<p>I try to strive for code that only expresses the functional goal. Afterwards, if necessary, optimizations like caching calculations in fields can be made.</p>
<p>In the specific example Mark is discussing, SomeObject is intended to be an adapter or aggregate that exposes several information sources to a consumer. It&#8217;s not intended to be an information holder for the information. And, there is already caching underneath the Dependency1 and Dependency2 classes, as they do abstract away some network calls.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike King</title>
		<link>http://www.markhneedham.com/blog/2009/09/02/coding-reduce-fields-delay-calculations/comment-page-1/#comment-22209</link>
		<dc:creator>Mike King</dc:creator>
		<pubDate>Fri, 04 Sep 2009 12:55:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1602#comment-22209</guid>
		<description>There&#039;s a nice combination of the two as well.  Avoid the up front work of the constructor oriented approach, especially since it&#039;s possible those values will never be needed, and the performance impact of repeating the same calculations over and over if that Property is used a lot and the value doesn&#039;t change.  Calculate it on demand but cache the value in a field, use a WeakReference if memory is a concern.</description>
		<content:encoded><![CDATA[<p>There&#8217;s a nice combination of the two as well.  Avoid the up front work of the constructor oriented approach, especially since it&#8217;s possible those values will never be needed, and the performance impact of repeating the same calculations over and over if that Property is used a lot and the value doesn&#8217;t change.  Calculate it on demand but cache the value in a field, use a WeakReference if memory is a concern.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Reflective Perspective - Chris Alcock &#187; The Morning Brew #426</title>
		<link>http://www.markhneedham.com/blog/2009/09/02/coding-reduce-fields-delay-calculations/comment-page-1/#comment-22201</link>
		<dc:creator>Reflective Perspective - Chris Alcock &#187; The Morning Brew #426</dc:creator>
		<pubDate>Fri, 04 Sep 2009 07:38:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1602#comment-22201</guid>
		<description>[...] Coding: Reduce fields, delay calculations - Mark Needham talks about avoiding doing calculations in constructors and storing the values in fields, instead opting for a more lazy loaded approach [...]</description>
		<content:encoded><![CDATA[<p>[...] Coding: Reduce fields, delay calculations &#8211; Mark Needham talks about avoiding doing calculations in constructors and storing the values in fields, instead opting for a more lazy loaded approach [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>

